Thread: Sorting 3 arrays alphabetically

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    What's wrong with this code?
    Code:
      char tcognome[40];
      char tnome[40];
      int tnum;
      int sorted[STRMAX];
      int rank[STRMAX];
    for (j = 0; j <cnum; ++j)
      {
        rank[j] = 0;
        for (i = 0; i < cnum; ++i)
          rank[j]+=(punteggio[i]>punteggio[j]);
      }
      
    /* sort elements */
     for (i = 0; i < cnum; i++)
       {
        sorted[rank[i]]=punteggio[i];
        if (rank[i]>rank[i+1]){
             strcpy(tnome,nome[i]);
             strcpy(nome[i],nome[i+1]);
             strcpy(nome[i+1],tnome);
     
             strcpy(tcognome,cognome[i]);
             strcpy(cognome[i],cognome[i+1]);
             strcpy(cognome[i+1],tcognome);
    }
       }
    
     printf("4. Outputting contestents' ranking: [in descending order]\n");
     /*   output */
      for (i = 0; i < cnum; ++i)
        printf("Points:%d   First name:%s   Last name:%s\n",sorted[i],nome[i],cognome[i]);
    it sorts points right, but again, names aren't associated. That was my try. Any hints?

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have the outer for loop closing, before you start the second for loop. They need to be nested, one inside the other.

    There might be other issues as well. Messing around with "your own" sorting algorithm, is like trying to make your own medicine's, at home -- not a great idea unless you are experienced, and have lots of time, too.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Yeah that didn't fix it. I wonder why.
    I tried this also:
    Code:
    for ( i = 0; i <cnum; i++) {
       for (j = i+1; j<cnum; j++) {  
     if(rank[i]>rank[j])
       {
           strcpy(tnome,nome[i]);
             strcpy(nome[i],nome[j]);
             strcpy(nome[j],tnome);
       }
       }}
    It works for 5-6 but then it's wrong.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're just swapping names then, but keying on rank? That just scrambles your data. Study how the one I gave you, works.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is why I hate helping people that don't pay attention. Tater already told you what to do:
    Quote Originally Posted by 'tater
    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
    
    	    }
    That means take those three lines above that, and duplicate them RIGHT THERE but with your other two array names! You were told what to do like 10 posts ago!


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

  6. #21
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    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(tmp1,cognome[j]);                              
        strcpy(cognome[j],cognome[posMin]);
        strcpy(cognome[posMin],tmp1);
    
        strcpy(tmp1,nome[j]);                              
        strcpy(nome[j],nome[posMin]);
        strcpy(nome[posMin],tmp1);
     
         tnum=punteggio[i];
         punteggio[i]=punteggio[j];
         punteggio[j]=tnum;
       }
    You mean? Didn't work.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Nephilim View Post
    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(tmp1,cognome[j]);                              
        strcpy(cognome[j],cognome[posMin]);
        strcpy(cognome[posMin],tmp1);
    
        strcpy(tmp1,nome[j]);                              
        strcpy(nome[j],nome[posMin]);
        strcpy(nome[posMin],tmp1);
     
         tnum=punteggio[i];
         punteggio[i]=punteggio[j];
         punteggio[j]=tnum;
       }
    You mean? Didn't work.
    Really? I can't possibly imagine why.


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

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    I've been sitting on my laptop for 7 straight hours so excuse my blindness. I don't know.
    Code:
        tnum=punteggio[posMin]; 
        punteggio[posMin]=punteggio[i];
        punteggio[i]=tnum

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't even trying, so now neither am I.


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

  10. #25
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Again, I'm a beginner. I'll try again.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What else is there that you need? I thought you were done.

  12. #27
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    Just a part of this whole sorting algorithm. I sorted the points but i'm facing the same problem of associating their respective names. I made it work for 5-6 names as I told you before but past that it's wrong :/

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He needs to pay attention. That's what he needs.

    I even colored what he was doing wrong. He's just not even reading what we've been saying, or this thread would have been done on post 6 instead of post 26.


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

  14. #29
    Registered User
    Join Date
    Mar 2011
    Posts
    25
    You do realise this is a new request related to the original one in my 1st post, yet DIFFERENT?
    So how could it have been solved in post 6 when I asked for it a few posts ago?
    First I had to sort according to last name and then associate their points, now it's the opposite. Sort by points and associate respective names.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wasn't sure what was up with your requests - busy day.

    To sort by points we just change what we're comparing (the sorting key):

    Code:
    char tlast[40];
    char tfirst[40];
    int tnum;
    
    for ( i = 0; i < cnum -1; i++) {
       for (j = i+1; j < cnum; j++) {
          if (number[i] < number[j]) {
    
             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;
          }
       }
    }
    The pattern is the same:

    two nested for loops
    then one if statement to compare keys
    if they need to be swapped, then
    swap three items.

    Numbers get swapped directly,
    strings get strcpy'd.

    Both swaps follow this pattern:
    From [i] to tempValue
    from [j] to [i]
    from tempValue to [j]

    Using curly braces and good indentation helps make it clear and accurate.
    Last edited by Adak; 03-22-2011 at 07:57 PM.

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, 04:53 AM
  2. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM