Hello,
I am new here just thought I could get some help I have wrote this program and it all works fine but I get this crap in runtime:
"
In alphabetical order, the names are:
`� N�
"
like I said it works and prints them all in alphabetical but if someone can explain why this ........ is printing in my program would be great.
Here is my code:
insert
Code:
#include <stdio.h>
#include <string.h>
#define NUM 11 /* number in the list */
main()
{
int i, sorted, iter=0;
long swaps = 0;
char names[NUM][80];
char temp[80];
printf("enter (%i-1) names:\n", NUM);
for (i = 0; i < 10; i++)
scanf("%s", &names[i][80]);
/* Bubble sort: */
puts("in order, the names are:");
do {
sorted = 1;
for (i=1; i<10; i++)
{
if (strcmp(names[i-1], names[i]) > 0)
{
sorted = 0;
strcpy(temp, names[i-1]);
strcpy(names[i-1], names[i]);
strcpy(names[i], temp);
swaps++;
}
}
for (i=9; i>0; i--)
{
if (strcmp(names[i-1], names[i]) > 0)
{
sorted = 0;
strcpy(temp, names[i-1]);
strcpy(names[i-1], names[i]);
strcpy(names[i], temp);
swaps++;
}
}
iter++;
} while (!sorted);
/* Output sorted list: */
puts("\nIn alphabetical order, the names are:");
for (i = 0; i < NUM; i++)
printf("%s\n", names[i]);
printf("\n%li swaps were performed.\n", swaps);
printf("\n%i iterations were performed.\n", iter);
}