Thread: assigning an string of arrays by loop

  1. #31
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Salem's (obviously untested) code started the loop at 0 instead of 1, an easy mistake to make from "muscle" memory. It was supposed to be something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SIZE 10
    
    int main(void) {
        char a[SIZE][SIZE + 1];
    
        // fill array
        strcpy(a[0], "a"); // set first string to "a"
        for (int i = 1; i < SIZE; i++) { // start at second string
            strcpy(a[i], a[i - 1]); // copy previous string to this string
            strcat(a[i], "a");      // postpend another a
        }
    
        // print array
        for (int i = 0; i < SIZE; ++i)
            printf("%s\n", a[i]);
    
        return 0;
    }
    As an alternative to rstanley's last offering, instead of (implicitly) memsetting the array to 0's and adding the a's, you could memset it to a's and add the 0's! Overall I prefer rstanley's code, but there are many ways of doing it. Of course, you can obtain the output without the array at all, but I assume OP wants to fill the array for some unspecified (possibly unfathomable) reason.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define SIZE 10
    
    int main(void) {
        char a[SIZE][SIZE + 1];
        
        // fill array with a's
        memset(a, 'a', sizeof a);
    
        // This adds a "diagonal" of 0's to the array of a's
        for (int i = 0; i < SIZE; ++i)
            a[i][i + 1] = '\0';
    
        // print it
        for (int i = 0; i < SIZE; ++i)
            printf("%s\n", a[i]);
    
        return 0;
    }
    
    It fills the bytes of the array like this (0 means '\0')
    a0aaaaaaaaa
    aa0aaaaaaaa
    aaa0aaaaaaa
    aaaa0aaaaaa
    aaaaa0aaaaa
    aaaaaa0aaaa
    aaaaaaa0aaa
    aaaaaaaa0aa
    aaaaaaaaa0a
    aaaaaaaaaa0
    A little inaccuracy saves tons of explanation. - H.H. Munro

  2. #32
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    Quote Originally Posted by rstanley View Post
    Salem's code was wrong. She admitted it. I don't know what you mean by "looks more construct".
    so, "looks more construct"

    is basically, code which is more logical which takes less memory space and runs more efficiently, example:

    instead of

    Code:
    printf("%d\n",inta1[0]);
    printf("%d\n",inta1[1]);
    printf("%d\n",inta1[2]);
    printf("%d\n",inta1[3]);
    printf("%d\n",inta1[4]);
    printf("%d\n",inta1[5]);
    printf("%d\n",inta1[6]);
    printf("%d\n",inta1[7]);
    printf("%d\n",inta1[8]);
    printf("%d\n",inta1[9]);
    you write

    Code:
        for(int i=0;i<10;i++){
            
            printf("%d\n",inta1[i]);
        }
    This is 1 example
    Last edited by WaterSerpentM; 1 Week Ago at 05:44 PM.

  3. #33
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by WaterSerpentM View Post
    so, "looks more construct"

    is basically, code which is more logical which takes less memory space and runs more efficiently, example:
    Maybe you mean "more compact"?

    adjective

    1. Closely packed, i.e. packing much in a small space.
    2. Having all necessary features fitting neatly into a small space.

  4. #34
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    Quote Originally Posted by christop View Post
    Maybe you mean "more compact"?
    ye

    i think they are the same word


    lol

  5. #35
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by WaterSerpentM View Post
    ye

    i think they are the same word


    lol
    construct
    adjective

    1. Formed by, or relating to, construction, interpretation, or inference.
    2. That of a noun used before another which has the genitive relation to it.
    Doesn't seem applicable with the way you're using it.

  6. #36
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    is that google?

    their definitions aren't exactly correct if you ask me

  7. #37
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Quote Originally Posted by WaterSerpentM View Post
    i think they are the same word
    Clearly you've never had a thought in your life.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #38
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    Quote Originally Posted by john.c View Post
    Clearly you've never had a thought in your life.
    maybe you don't know, but most English words has other words like it with 's' in it that means the same thing. that's why there are so many 's' words in the dictionary =]



    wonderful or fantastic

    boat or ship

    glide or soar
    Last edited by WaterSerpentM; 1 Week Ago at 07:50 PM.

  9. #39
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    hello all,

    for my next calculation practice, I want to output sequence of arrays like this

    0
    01
    012
    0123
    01234
    012345
    0123456
    01234567
    012345678
    0123456789

    Code:
        int i;
    
    
        int int1[10];
        char chara1[10][10];
        int1[0] = 0;
    
    
        for(i=0;i<10;i++){
            itoa(int1[i],chara1[i];
            strcpy(chara1[i],chara1[i-1]);
            printf("%s",chara1[i]);
        }
    at the moment, this is my code, any help?

  10. #40
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by WaterSerpentM View Post
    boat or ship

    glide or soar
    You can also make valid words by removing the "s": hip and oar. I don't see the point you're trying to make. "More construct" still doesn't make any sense in English.

    I've found only two online dictionaries that have a definition for the adjective form of that word (and not for variants like "constructive" or "constructable"): construct - definition and meaning and Oxford English Dictionary. (The latter requires a subscription so I can't see its definition, but it seems to be a form of the word that is rarely if ever used today.)

  11. #41
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    the output of this code is scrambled, any help?

    Code:
    int i;
    
    
        int int1[10];
        char chara1[10][10];
        char chara2[10][10];
        //int1[0] = 0;
    
    
        for(i=0;i<10;i++){
    
    
    
    
            itoa(int1[i],chara1[i],30);
            strcpy(chara2[i],chara1[i-1]);
            strcat(chara2[i],chara1[i]);
    
    
            printf("%s\n", chara2[i]);
    
    
    
    
        }
    its supposed to output:

    0
    01
    012
    0123
    01234
    012345
    0123456
    01234567
    012345678
    0123456789

  12. #42
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Base 30.
    Uninitialized input.
    Out-of-bounds access.
    The trifecta!
    A little inaccuracy saves tons of explanation. - H.H. Munro

  13. #43
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    is that what that means?

    change it to 10?

    or 26?

  14. #44
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    so, at the moment


    Code:
        int i;
    
    
        int int1[10];
        char chara1[10][10];
        char chara2[10][10];
    
    
        for(i=0;i<10;i++){
    
    
            int1[i] = i;
            itoa(int1[i],chara1[i],26);
    
    
            strcat(chara1[i],chara1[i-1]);
    
    
    
    
    
    
    
    
            printf("%s\n",chara1[i]);
    
    
    
    
        }
    outputs

    0
    10
    210
    3210
    43210
    543210
    6543210
    76543210
    876543210
    9876543210




    which is backwards lol

    its supposed to output

    0
    01
    012
    0123
    01234
    012345
    0123456
    01234567
    012345678
    0123456789
    Last edited by WaterSerpentM; 1 Week Ago at 04:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with assigning character arrays
    By KSG XII in forum C Programming
    Replies: 7
    Last Post: 04-05-2015, 04:04 PM
  2. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  3. assigning 2D arrays to pointers
    By rakeshkool27 in forum C Programming
    Replies: 5
    Last Post: 01-19-2010, 12:34 AM
  4. trouble assigning arrays
    By shintaro in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2008, 08:31 AM
  5. Assigning values to arrays
    By napkin111 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2003, 08:52 PM

Tags for this Thread