Thread: STRcomp(strl) arrays

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Exclamation STRcomp(strl) arrays

    Ok well im desperate this problem is due by 12 am today and i have sent the teacher messages with my problem but no reply so basically can u help, im having problem with the (strcmp) etc.

    This is what i have
    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];    /*Declare array of char*/
        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] );
        
    
        return 0;
    }
    i saw this in another forum post but i dont understand it what it means , im obviously a amateur but please help me

    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;
                }
            }
        }
    Problem Description
    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.
    Last edited by ineedhelp; 04-08-2011 at 03:31 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's how you swap strings. You cannot assign one string to another across the equals sign in C so you have to use library functions to copy strings from place to place.

    And please don't use huge stupidly coloured fonts... I can almost guarantee they have the exact opposite of the desired effect.
    Last edited by CommonTater; 04-08-2011 at 03:57 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    int strncmp(const char *s1, const char *s2, size_t n);
    Why are you prototyping a standard function?
    You prototype a function you are going to provide, YOU did NOT provide it.
    Why are you prototyping it?

    Tim S.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    What do you mean by prototyping? im a noob at this, and since im taking a online course the book is the only help i have and it doesnt even mention that -______-

    And yaaa i change the font lol :P um when u mean swaping strings you mean like doing multiple things iin the same function? yaaa i know im stupid

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Prototype is the function header without its body. Used to define the types of arguments and return type. strncmp() is a standard library function. You should not put in the prototype for those because they're already included in <string.h> in this case.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Ok so this is the code i got so far and i cant find out where to put the

    Code:
    system(pause);
    and still havent found out how to implement the age part of the problem :/

    Code:
    #include <stdio.h>
    #define STRSIZ 30  
    #define MAXAPP 50  
    
    int alpha_first(char *list[], int min_sub, int max_sub);
    void select_sort_str(char *list[], int n);
    
    int
    main(void)
    {
          char  applicants[MAXAPP][STRSIZ]; 
    
          char *alpha[MAXAPP];             
          int   num_app,                   
    	    i;
          char  one_char;
    
          /*  Gets applicant list	*/
          printf("Enter number of applicants (0 . . %d)\n> ", MAXAPP);
          scanf("%d", &num_app);
          
          do    
        	  scanf("%c", &one_char);
          while (one_char != '\n');
    
          printf("Enter names of applicants on separate lines\n");
          printf("in the order in which they applied\n");
          for  (i = 0;  i < num_app; ++i)
        	  gets(applicants[i]);
    
          /*  Fills array of pointers and sorts	*/
          for  (i = 0;  i < num_app;  ++i)
       	  alpha[i] = applicants[i];  /* copies ONLY address */
          select_sort_str(alpha, num_app);
    
          /*  Displays both lists	*/
          printf("\n\n%-30s%5c%-30s\n\n", "Application Order", ' ', "Alphabetical Order");
          for  (i = 0;  i < num_app;  ++i)
    	     printf("%-30s%5c%-30s\n", applicants[i], ' ', alpha[i]);
    
          return(0);
    }
    
    int
    alpha_first(char *list[],    	
    	    int   min_sub,	
    	    int   max_sub)	
    {
          int first, i;
    
          first = min_sub;
          for  (i = min_sub + 1;  i <= max_sub;  ++i)
              if (strcmp(list[i], list[first]) < 0)
                    first = i;
    
          return (first);
    
    }
    
    
    void
    select_sort_str(char *list[], 
    		int   n)      
    {
    
          int   fill,        
    	    index_of_min; 
          char *temp;
    
          for  (fill = 0;  fill < n - 1;  ++fill) {
              index_of_min = alpha_first(list, fill, n - 1);
    
              if (index_of_min != fill) {
                    temp = list[index_of_min];
                    list[index_of_min] = list[fill];
                    list[fill] = temp;
                    
              }
          }
          
    }

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    In this version you forgot about the parallel array 'age' altogether.
    Where to put system(pause)? Just before the return in main().

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    I know but i get a error -__- and i know but i dont know how to write it in the code like yaa its hard to explain but basically how do u write a parallel array? do u have to include all the other info or what ?
    this is the error i got for the system pause
    Code:
     `pause' undeclared (first use in this function) 
      (Each undeclared identifier is reported only once 
        for each function it appears in.)

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try system("pause") but it may not work in your system.
    Parallel array in this case just means keep using the age[] array... But it somehow disappeared in your 2nd example. Then when you do the swaps for the strings you just swap the corresponding age elements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

Tags for this Thread