Sorting by comparing characters problem

My program accept the name, grade, age and so on of the students and stores them in a structure called student.
Students are in a array named array
names grade age are the members of the structure.
name is the array of characters of the length 50
I am trying to sort in the alphabetical order. But i program fails.I am also trying to check the second letters so I am using bool. But it is not working. I need help please. Below is my sorting by name function.It is bubble sort. I know there are lot of problem. But no syntax error. Please give me the solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void sorter_name (student array[], int length)
{ 
     char letter[length];
     for (int m =0; m < length; m++)
     {
         for (int n =0; n < length-1-m; n++)
         {
             bool swapped = true;
             for (int k=0; array[n].name != NULL, swapped == true  ; k++)
             {
                 if (array[n]. name[k] > array[n].name [k+1])
                 {
                  student holder = array[n+1];  // swapping
                  array[n+1] = array[n];// swapping
                  array[n] = holder;// swapping
                  swapped = false;
                 }
                 else 
                 {
                     swapped = true;
                 }
             }
         }
     }
} 
The problem is in line 11. You're accessing the name of the same student, and checking for its consecutive letters in its name. Also, use less than sign, because I'm pretty sure, as far as characters are concerned, the compiler considers a<b<c..... So modify it such as:
1
2
3
4
if ((array[n].name[k])>(array[n+1].name[k])
{
    //Swapping...
}

If the rest of your logic is correct, I'm guessing this should work.. Try it and let me know! :)
i changed it but I am afraid, it is still not working.
I know there is a problem in how I use bool and not sure about others.
bump!
Ohk, as far as I can see, line 18-21 look redundant... Also, scrap bool and use break statements... This will finally ensure whether the problem is in the logic, or in the use of bool...
Aassuming your student struct looks like :

struct student
{
char name[30];
int grade;
int age;
......
};

I in your bubble sort you don't need 3 level "for loop" to sort by name - two loops will do this.
Also, for student names compare (char [] or char *) simple relational operators > or < don't work -- you must use strcmp(...) function

for ( m=0; m < length; m++)
{
for (n=0; n < length - m -1; n++)
{
if (strcmp(array[n].name, array[n+1].name) > 0 )
{
student temp = array[n+1] ;
array[n+1] = array[n] ;
array[n] = temp ;
}
}
}

Hope this helps.
Last edited on
sorry but it still does not work. ANd i cannot use strmcp as my teacher does not let me do it

Then build your own strcmp function that compare two strings.
how would you do that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void sorter_name (student array[], int length)
{ 
     char letter[length];
     for (int m =0; m < length; m++)
     {
         for (int n =0; n < length-1-m; n++)
         {
             bool swapped = false;
             for (int k=0; array[n].name != NULL, swapped != true  ; k++)
             {
                 if ((makesmall(array[n].name[k]))>(makesmall(array[n+1].name[k])))
                 {
                  student holder = array[n+1];  // swapping
                  array[n+1] = array[n];// swapping
                  array[n] = holder;// swapping
                  swapped = false;
                 }
                 else if ((array[n].name [k]) == array[n+1].name[k])
                 {
                     swapped = true;
                 }
                 else
                 {
                     swapped = false;
                 }
             }
         }
     }
} 

this program sorts with the first letter. But i wanted it to sort by everything. and it should be sorting but it does not why?
for example
is someone named Henry and Henish are there it is not sorting. How could i make that work ?
Last edited on
array[n].name != NULL, swapped != true doesn't do what you think it does.
You should use the and operator &&: array[n].name != NULL && swapped != true
ok i will try that
nope it still does not work. the bool is there for this:
if
yaa
and aay
i dont want to compare the last letter and make my program wrong. so when it swaps i want to end it.
help! its urgent
K now it works. thanks everybody. but
makesmall is not working not sure why
char makesmall(char chr)
1
2
3
4
5
6
7
{
     if (chr  >= 'A' &&  chr <= 'Z')
     {
            chr = chr- ('A' - 'a'); 
     }
     return chr;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void sorter_name (student array[], int length)
{ 
     char letter[length];
     for (int m =0; m < length; m++)
     {
         for (int n =0; n < length-1-m; n++)
         {
             bool swapped = false;
             for (int k=0; array[n].name != NULL && swapped != true  ; k++)
             {
                 if (makesmall(array[n].name[k])>(makesmall(array[n+1].name[k])))
                 {
                  student holder = array[n+1];  // swapping
                  array[n+1] = array[n];// swapping
                  array[n] = holder;// swapping
                  swapped = true;
                 }
                 else if ((array[n].name [k]) == array[n+1].name[k])
                 {
                     swapped = false;
                 }
                 else
                 {
                     swapped = true;
                 }
             }
         }
     }
} 
 
help please urgent
for makesmall, do this:

1
2
3
4
5
6
7
{
     if (chr  >= 'A' &&  chr <= 'Z')
     {
            chr = chr + 32; 
     }
     return chr;
}


And does your sort work now, or no? If not, I'll post help for that...
Topic archived. No new replies allowed.