Like Tree2Likes

Sorting 3 arrays alphabetically

This is a discussion on Sorting 3 arrays alphabetically within the C Programming forums, part of the General Programming Boards category; Greetings everyone, I am writing a program that allows the user to enter the first name, last name, and grade ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    25

    Sorting 3 arrays alphabetically

    Greetings everyone,
    I am writing a program that allows the user to enter the first name, last name, and grade of each student. The program then sorts all the names alphabetically by last name, and display a list of all the students names and grades in alphabetical order.
    I don't understand how can I associate to print out the first name and the points o that same array.
    I managed to make the output sort them by last name and output them, too. But the names remain unswapped.
    So when I input:
    Angelina Zellweger 11
    Suzy Applegate 55

    it should give:
    Suzy Applegate 55
    Angelina Zellwegerr 11

    but mine outputs this:
    Angelina Applegate
    Suzy Zellweger
    Code:
      for ( j = 0; j < cnum -1; j++){
        posMin = j;
    
        for (i = j+1; i < cnum; i++)
          if (strcmp(last[i], last[posMin]) < 0)
    	posMin = i;
    
        strcpy(tmp,last[j]);
        strcpy(last[j],last[posMin]);
        strcpy(last[posMin],tmp);
    	    }
    
      for (i = 0; i < cnum; i++)
        printf("Last name is [%i] = %s\n",i,last[i]);
    That's the code for sorting the last name array.

    Any help would be appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    When you swap the last name, you need to swap the first name as well.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to swap the entire set of arrays at the same time...

    Better plan would be to use a struct and simply swap structs around.
    ಠ_ಠ likes this.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    I agree, struct that contains first name, last name, and grade would be a better idea.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    bithub, I realise that. As i said; I don't understand how can I associate to print out the first name and the points of that same array.

    commontater, being new to C I would appreciate some easier commands :P is that in the family of strcpy and strcmp?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Nephilim View Post
    bithub, I realise that. As i said; I don't understand how can I associate to print out the first name and the points of that same array.

    commontater, being new to C I would appreciate some easier commands :P is that in the family of strcpy and strcmp?
    Code:
    Code:
      for ( j = 0; j < cnum -1; j++){
        posMin = j;
    
        for (i = j+1; i < cnum; i++)
          if (strcmp(last[i], last[posMin]) < 0)
    	posMin = i;
    
        strcpy(tmp,last[j]);                              
        strcpy(last[j],last[posMin]);
        strcpy(last[posMin],tmp);
    
    // you also need to swap the first name and grade here
    
    	    }
    
      for (i = 0; i < cnum; i++)
        printf("Last name is [%i] = %s\n",i,last[i]);
    ಠ_ಠ likes this.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    I managed to sort the 3rd array of points but only if the points are entered in a descending order.
    Code:
        int j;
          char tmp[20], tmp2[20];
          int posMin, tmp3;
          for (j=0; j<cnum-1; j++){
        posMin = j;
    
        for (i = j+1; i < cnum; i++)
          if (strcmp(cognome[i], cognome[posMin]) < 0)
    	posMin = i;
    
        strcpy(tmp,cognome[j]);
        strcpy(cognome[j],cognome[posMin]);
        strcpy(cognome[posMin],tmp);
    
        strcpy(tmp2,nome[j]);
        strcpy(nome[j],nome[posMin]);
        strcpy(nome[posMin],tmp2);
          
    for (i = j+1; i<cnum; i++)
      {
        if(punteggio[i]>punteggio[posMin])
        {
        tmp3=punteggio[j];
        punteggio[j]=punteggio[posMin];
        punteggio[posMin]=tmp3;
          }
      } 
          }
    But it doesn't work if it's not in any other order... help?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you even have that loop you added at the end? All you need to do was to add a third set of copy lines, like the first two blocks of strcmp.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Nephilim View Post
    I managed to sort the 3rd array of points but only if the points are entered in a descending order.
    Code:
        int j;
          char tmp[20], tmp2[20];
          int posMin, tmp3;
          for (j=0; j<cnum-1; j++){
        posMin = j;
    
        for (i = j+1; i < cnum; i++)
          if (strcmp(cognome[i], cognome[posMin]) < 0)
    	posMin = i;
    
        strcpy(tmp,cognome[j]);
        strcpy(cognome[j],cognome[posMin]);
        strcpy(cognome[posMin],tmp);
    
        strcpy(tmp2,nome[j]);
        strcpy(nome[j],nome[posMin]);
        strcpy(nome[posMin],tmp2);
          
    for (i = j+1; i<cnum; i++)
      {
        if(punteggio[i]>punteggio[posMin])
        {
        tmp3=punteggio[j];
        punteggio[j]=punteggio[posMin];
        punteggio[posMin]=tmp3;
          }
      } 
          }
    But it doesn't work if it's not in any other order... help?
    Specifically:
    1) add int i to the variables.
    2) Put curly braces around both your for loops, and your if statement.

    If you indent subordinate lines just 3 spaces or so, it will be MUCH clearer. It should look like this: (this is a Substitution sort)

    Code:
    for(i=0;i<SIZE;i++) {
       for(j=i+1;j<SIZE;j++) {
          if((strcmp(string1, string2)) > 0) {
            //add your swap code in here
          }
       }
    }
    3) The last for loop in bold type, you should delete.

    4) When a swap is made, the struct needs to be swapped, of course.

    If you don't use structs, then ALL that persons array data, needs to be swapped. Not just their names.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Quote Originally Posted by quzah View Post
    Why do you even have that loop you added at the end? All you need to do was to add a third set of copy lines, like the first two blocks of strcmp.
    strcmp or strcpy?
    You mean like:
    strcpy(tmp3,punteggio[j]);
    strcpy(punteggio[j],punteggio[posMin]);
    strcpy(punteggio[posMin],tmp3);

    Quzah.
    Quote Originally Posted by Adak View Post
    Specifically:
    1) add int i to the variables.
    2) Put curly braces around both your for loops, and your if statement.

    If you indent subordinate lines just 3 spaces or so, it will be MUCH clearer. It should look like this: (this is a Substitution sort)

    Code:
    for(i=0;i<SIZE;i++) {
       for(j=i+1;j<SIZE;j++) {
          if((strcmp(string1, string2)) > 0) {
            //add your swap code in here
          }
       }
    }
    3) The last for loop in bold type, you should delete.

    4) When a swap is made, the struct needs to be swapped, of course.

    If you don't use structs, then ALL that persons array data, needs to be swapped. Not just their names.
    How can that be done? That's what I've been trying to do all the time.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    This is what i got:
    Code:
     for ( j = 0; j < cnum -1; j++){
        posMin = j;
    
        for (i = j+1; i < cnum; i++)
          if (strcmp(cognome[i], cognome[posMin]) < 0)
    	posMin = i;
    
        strcpy(tmp,cognome[j]);
        strcpy(cognome[j],cognome[posMin]);
        strcpy(cognome[posMin],tmp);
    
        strcpy(tmp2,nome[j]);
        strcpy(nome[j],nome[posMin]);
        strcpy(nome[posMin],tmp2);
    
        tmp3=punteggio[j];
        punteggio[j]=punteggio[posMin];
        punteggio[posMin]=tmp3;
    }
    Still not right though...

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    char tlast[40];
    char tfirst[40];
    int tnum;
    
    for ( i = 0; i < cnum -1; i++) {
       for (j = i+1; j < cnum; j++) {
          if ((strcmp(last[i], last[j])) > 0) {
             strcpy(tlast,last[i]);
             strcpy(last[i],last[j]);
             strcpy(last[j],tlast);
    
             strcpy(tfirst,first[i]);
             strcpy(first[i],first[j]);
             strcpy(first[j],tfirst);
    
             tnum=number[i];
             number[i]=number[j];
             number[j]=tnum;
          }
       }
    }
    That should do it.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Thanks for replying So it looks like that's what I was missing. Works like a charm.
    Last edited by Nephilim; 03-22-2011 at 12:43 PM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an array alphabetically?
    By bluebob in forum C Programming
    Replies: 7
    Last Post: 03-09-2011, 03:53 AM
  2. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21