This was a LOT harder than I thought it would be.
I want to be able to sort a list of names. I have been trying to do this and this is possibly the hardest problem I've had to solve.
I have heard of functions that can sort stuff for you, maybe library functions or maybe functions off of the internet, but I was adamant on writing my own with no help.
It took me a couple of days to get this right. Even then, it can only compare the first characters and sort accordingly.
If I can muster up the courage I'll try to make it so that it can sort the rest of the letters too; 'dictionary entry' like. But for now, this will have to do, because it represents a lot of hard work for me :).
It accepts 5 names and sorts them according to the first character. There is no check to ensure small letters or big letters, so THE PROGRAM WILL ALWAYS CONSIDER SMALL LETTERS TO BE HIGHER THAN BIG LETTERS: Eg. 'banana' will figure as higher than 'Zebra', so on the list 'banana' will be below 'Zebra', because to the program, small letter 'b' is larger than big letter 'Z'.
#include <stdio.h>
#include <strings.h>
int main()
{
int a,b=0,found=0;;
char names1[5][21],names2[5][21],sorted[5][21];
printf("Enter 5 names:\n");
for(a=0;a<5;a++)
{
gets(names1[a]);
}
/**Make a copy of the original array so as not to lose
the original input order**/
for(a=0;a<5;a++)
{
strcpy(names2[a],names1[a]);
}
/**Sort Here**/
while(b<5)
{
for(a=b;a+1<5;a++)
{
if(names1[b][0]>names1[a+1][0])
{
strcpy(sorted[b],names1[a+1]); //copy the smaller in order into sorted.
strcpy(names1[a+1],names1[b]); //copy what was being compared into the one already saved in sorted
strcpy(names1[b],sorted[b]); //start comparing the smaller one
found=1; //so that we know a smaller character has been found
}
}
if(found==0) //nothing was found
{
strcpy(sorted[b],names1[b]); //what was being compared is the smallest, copy into sorted
}
b++; //go on to the next item
found=0; //reset found
}
system("cls");
for(a=0;a<5;a++)
{
printf("\n%s\t%s",names2[a],sorted[a]);
}
return 0;
}
I want to be able to sort a list of names. I have been trying to do this and this is possibly the hardest problem I've had to solve.
I have heard of functions that can sort stuff for you, maybe library functions or maybe functions off of the internet, but I was adamant on writing my own with no help.
It took me a couple of days to get this right. Even then, it can only compare the first characters and sort accordingly.
If I can muster up the courage I'll try to make it so that it can sort the rest of the letters too; 'dictionary entry' like. But for now, this will have to do, because it represents a lot of hard work for me :).
It accepts 5 names and sorts them according to the first character. There is no check to ensure small letters or big letters, so THE PROGRAM WILL ALWAYS CONSIDER SMALL LETTERS TO BE HIGHER THAN BIG LETTERS: Eg. 'banana' will figure as higher than 'Zebra', so on the list 'banana' will be below 'Zebra', because to the program, small letter 'b' is larger than big letter 'Z'.
#include <stdio.h>
#include <strings.h>
int main()
{
int a,b=0,found=0;;
char names1[5][21],names2[5][21],sorted[5][21];
printf("Enter 5 names:\n");
for(a=0;a<5;a++)
{
gets(names1[a]);
}
/**Make a copy of the original array so as not to lose
the original input order**/
for(a=0;a<5;a++)
{
strcpy(names2[a],names1[a]);
}
/**Sort Here**/
while(b<5)
{
for(a=b;a+1<5;a++)
{
if(names1[b][0]>names1[a+1][0])
{
strcpy(sorted[b],names1[a+1]); //copy the smaller in order into sorted.
strcpy(names1[a+1],names1[b]); //copy what was being compared into the one already saved in sorted
strcpy(names1[b],sorted[b]); //start comparing the smaller one
found=1; //so that we know a smaller character has been found
}
}
if(found==0) //nothing was found
{
strcpy(sorted[b],names1[b]); //what was being compared is the smallest, copy into sorted
}
b++; //go on to the next item
found=0; //reset found
}
system("cls");
for(a=0;a<5;a++)
{
printf("\n%s\t%s",names2[a],sorted[a]);
}
return 0;
}
No comments:
Post a Comment