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
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! :)
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...
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 ;
}
}
}
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;
}
elseif ((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 ?
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.