Thread: Populate char array using another char array and int array

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    27

    Populate char array using another char array and int array

    This program is supposed to populate a char array with letters of the alphabet with a particular frequency that corresponds to an int array.

    Once this new char array is populated, a loop runs through and picks seven letters, NULL'ing each choice as it is picked, thus making it no longer an option.

    I am at a loss for why this won't run properly. Thanks.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    
    const int TOTAL_TILES = 98;
    char letters[7][1];
    char tiles[26][1] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    char tilesArray[98][1];
    int distribution[26][1]={9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1};
    
    
    srand(time(NULL));
    
    int num;
    int i, j, k = 0;
    
    while (i < TOTAL_TILES) //loop seeds array with letters according to distribution array
    
        {
    
        while (distribution[j] != 0)
    
            {
    
            strcpy(tilesArray[i], tiles[j]);
            distribution[j] = (distribution[j] - 1);
            i++;
    
            }
    
        j++;
    
        }
    
    while (k < 7)  //loop will randomly assign 7 letters to the letters array and NULL fields once they are used
    
        {
    
        num = rand()%97;
    
        if (tilesArray[num] != NULL)
    
            {
    
            strcpy(letters[k], tilesArray[num]);
            tilesArray[num] = NULL;
            k++;
    
            }
    
        }
    
    return 0;
    
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    strcpy(tilesArray[i], tiles[j]);
    tiles does not have '\0' (NUL) on the end.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, you should declare your arrays as
    Code:
    char arr[] = ...
    You have your rows and columns mixed up

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Why are you declaring all the arrays as 2-D arrays?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    
    const int TOTAL_TILES = 98;
    char letters[7][1];
    char tiles[26][1] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    char tilesArray[98][1];
    int distribution[26][1]={9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1};
    
    
    srand(time(NULL));
    
    int num;
    int i, j, k = 0;
    
    while (i < TOTAL_TILES) //loop seeds array with letters according to distribution array
    
        {
    
        while (distribution[j] != 0)             <<<<<---------what does distribution[j] contains? distribution is a 2D array
    
            {
    
            strcpy(tilesArray[i], tiles[j]);            <<<<<<<<<<<--------------same comment applies here, you declared all arrays as 2D arrays
            distribution[j] = (distribution[j] - 1);
            i++;
    
            }
    
        j++;
    
        }
    
    while (k < 7)  //loop will randomly assign 7 letters to the letters array and NULL fields once they are used
    
        {
    
        num = rand()%97;
    
        if (tilesArray[num] != NULL)
    
            {
    
            strcpy(letters[k], tilesArray[num]);
            tilesArray[num] = NULL;
            k++;
    
            }
    
        }
    
    return 0;
    
    }
    Change the code

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    I set some of the arrays to 2d in an effort to get the code to work.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I think you are confused about the difference between chars and strings (array of chars + terminating '\0').

    'a' is a char.
    "a" is a string literal which is stored as 2 chars: 'a' + '\0'.

    AFAIU you just want to operate on chars and don't need strings.

    Code:
    char arr[] = { 'a', 'b', 'c' };
    is an array of 3 chars but not a string.

    And you should include <time.h> if you want to use time() and <stdlib.h> if you want to use srand() and rand().

    Bye, Andreas
    Last edited by AndiPersti; 09-13-2012 at 02:19 AM. Reason: add stdlib.h

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by somniferium View Post
    I am at a loss for why this won't run properly. Thanks.
    Why is it when someone has a problem and wants help, they rarely explain what the problem is and expect us to figure it out psychically or spending an hour trying to understand all that code with the hopes of stumbling on what the problem is? Wouldn't it be easier and faster to actually explain the problem?

    After 20 posts, one would think you'd have figured this out by now.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Quote Originally Posted by WaltP View Post
    Why is it when someone has a problem and wants help, they rarely explain what the problem is and expect us to figure it out psychically or spending an hour trying to understand all that code with the hopes of stumbling on what the problem is? Wouldn't it be easier and faster to actually explain the problem?
    Agreed. It would be nice if the forum could be modified so that users requesting help would be asked to fill a predefined form (context, code, actual result, expected result) instead of a simple text field, any volunteer? ;-)
    Last edited by root4; 09-13-2012 at 02:54 AM.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Look at the comments and fix:

    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main(void) {
     
    const int TOTAL_TILES = 98;  /* change this to a #define */
    char letters[7][1]; /* this and all the other arrays should be 1d not 2d */
    char tiles[26][1] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    char tilesArray[98][1];
    int distribution[26][1]={9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1};
     
     
    srand(time(NULL)); /* you didn't include <time.h> */
     
    int num;
    int i, j, k = 0;
     
    while (i < TOTAL_TILES) /* i is uninitialized and could start at any value */
     
        {
     
        while (distribution[j] != 0) /* j is also uninitialized */
     
            {
     
            strcpy(tilesArray[i], tiles[j]); /* if they were 1d arrays, you wouldn't have to use strcpy */
            distribution[j] = (distribution[j] - 1); /* why not -- operator? */
            i++; 
     
            }
     
        j++;
     
        }
     
    while (k < 7)  //loop will randomly assign 7 letters to the letters array and NULL fields once they are used
     
        {
     
        num = rand()%97; /* try to not use magic numbers */
     
        if (tilesArray[num] != NULL) 
     
            {
     
            strcpy(letters[k], tilesArray[num]);
            tilesArray[num] = NULL;
            k++;
     
            }
     
        }
     
    return 0;
     
    }
    I really don't understand the point of this code either.

    Anyway, you should review arrays.

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by root4 View Post
    Agreed. It would be nice if the forum could be modified so that users requesting help would be asked to fill a predefined form (context, code, actual result, expected result) instead of a simple text field, any volunteer? ;-)
    Like a bug tracker? That's not a bad idea.

    Like some bug trackers I've used (I can't remember which), it could still be a simple text field, but the text field initially asks for each of those bug details. Can this forum include default text in the text field for a new thread post?

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Quote Originally Posted by christop View Post
    Can this forum include default text in the text field for a new thread post?
    That would be a solution too.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    The first While loop is supposed to populate tilesArray[] with 98 letters, based on the corresponding numbers in distribution [] (ex. 9 A's, 2 B's, etc.). After that, the second while loop is supposed to randomly pull seven letters from tilesArray[], putting a null value in the place of each letter chosen (in effect, not allowing that letter to be chosen again).

    My program is compiling after making your suggested changes. When I entered printf's lines to confirm the program was doing what I need it to do (as the code shows below) the program compiles, but has a Windows error.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <stdlib.h>
    
    int main(void) {
    
    char letters[7];
    char tiles[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char tilesArray[98];
    int distribution[] ={9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1,0};
    int num;
    int i, j, k = 0;
    
    
    srand(time(NULL));
    
    while (i < 98) //loop seeds tilesArray[] with tiles[] with a frequency based on numbers in distribution[]
    
        {
    
        while (distribution[j] != 0)
    
            {
    
            tilesArray[i] = tiles[j];
            printf ("%s\n", tilesArray[i]);
            distribution[j]--;
            i++;
    
            }
    
        j++;
    
        }
    
    while (k < 7)  //randomly assign seven letters to letter[], NULL'ing each choice after it is picked.
    
        {
    
        num = rand()%97;
    
        if (tilesArray[num] != '\0')
    
            {
    
            letters[k] = tilesArray[num];
            printf ("%s\n", letters[k]);
            tilesArray[num] = '\0';
            k++;
    
            }
    
        }
    
    return 0;
    
    }

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int i, j, k = 0;
    ...
    while (i < 98) //loop seeds tilesArray[] with tiles[] with a frequency based on numbers in distribution[]
    {
        while (distribution[j] != 0)
    You only initialize k to zero. i and j still have random (= junk) values. That's why you get a seg fault (I think it's called "general protection fault" in windows) because you are trying to access an element of distribution which doesn't exist. You have to set every variable to zero.

    Code:
    printf ("%s\n", tilesArray[i]);
    tilesArray[i] will be a char but with "%s" you are trying to print a string. "%c" is the specifier you want to use.

    Bye, Andreas

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    Ah! I was under the impression that i, j, and k were being set to zero on that line. Thanks

    And with those changes, it is working. Gracias.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char array doesn't print the char in the array
    By KiRa11Love in forum C Programming
    Replies: 4
    Last Post: 09-07-2012, 05:52 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 05:41 PM
  4. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  5. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07:19 PM