Thread: Help with array Hw

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Help with array Hw

    Hi i cant seem to get this program to work, i think the problem might be with the arrays but i cant seem to figure it out :/
    This is the hw problem

    Write a program that stores lists of names (the last name first) and ages in parallel arrays and sorts the names into alphabetical order keeping the ages with the correct names. The original arrays of names and ages should remain no changes. Therefore, you need to create an array of character pointers to store the addresses of the names in the name array initially. Apply the selection sort to this array of pointers so that the corresponding names are in alphabetical order. See the sample program of Fig. 9.14 from pages 474 to 476 in the textbook. You should use another array of pointers to age array to make sure the age is corresponding to the correct name.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define CLASS_SIZE 3
    #define NAME_LEN 15
    
    int strncmp(const char *s1, const char *s2, size_t n);
    int main (void) {
    
        char lname[CLASS_SIZE][NAME_LEN];   
        char fname[CLASS_SIZE][NAME_LEN];
        int age[CLASS_SIZE];
        char *temp;
        
        
        
        for(int i=1; i<=CLASS_SIZE; i++){
        
            printf("Enter first name, last name and age please:\n");
            scanf("%s %s %d", fname[i], lname[i], &age[i]);
    
        }
    
        for (int x=0; x<CLASS_SIZE; x++) {
    
            for (int y=1; y<NAME_LEN; y++){
    
                if (strncmp(lname[y-1], lname[y],NAME_LEN)>0){
                  temp = lname[y];
                  lname[y] = lname[y-1];
                  lname[y-1] = temp;
        
                }
            }
        }
    
        for(int a=1; a<=CLASS_SIZE; a++)
        
            printf("%s\n", lname[a] );
        
        system(pause);
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
            scanf("%s %s %d", fname[i], lname[i], &age[i]);
    The [] syntax dereferences the array pointer so you need to convert it back to an address...

    Code:
            scanf("%s %s %d", &fname[i], &lname[i], &age[i]);
    Also when doing your sort...
    1)You need to swap all 3 arrays on the same indexes at the same time... otherwise the data will become misaligned.
    2)C will not assign strings (or any other array) across the equals sign. You need to use strcpy() or memcpy() to shuffle them around.
    Last edited by CommonTater; 04-07-2011 at 01:09 AM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    so like
    Code:
    strcpy (temp, lname[y]);
    strcpy (lname[y], lname[y-1]);
    strcpy (lname[y-1], temp);
    and where would these go if they are right? im new to c :P

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 123456tacos View Post
    so like
    Code:
    strcpy (temp, lname[y]);
    strcpy (lname[y], lname[y-1]);
    strcpy (lname[y-1], temp);
    and where would these go if they are right? im new to c :P
    More like....

    Code:
    char temp[namelen];  
    int itemp;
    
        for (int x=0; x<CLASS_SIZE; x++) {
            for (int y=1; y < x; y++){
    
                if (strncmp(lname[x], lname[y],NAME_LEN)>0){
                 strcpy (temp, lname[y]);
                 strcpy (lname[y], lname[x]);
                 strcpy (lname[x], temp);
                 strcpy (temp, fname[y]);
                 strcpy (fname[y], fname[x]);
                 strcpy (fname[x], temp);
                 itemp = age[y];
                 age[y] = age[x];
                 age[x] = itemp;
                }
            }
        }
    Note this is untested... for concept only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM