Hello,
I am new to C and taking a class. I need some help with an assignment.
//Build a program that uses a single-dimension array to store 10 names input by a user.
//After inputting the names, the user should see a menu with two options to sort and print the 10 names in ascending or descending order.
insert
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char names[10];
char temp[10];
int count,i,j;
int sort;
count=1;
for ((i=0);i<10;i++)
{
while (count<11)
{
printf("Please enter name %i\n",count);
gets(names);
count=count+1;
}
}
printf("Would you like to print in Ascending (press 1) or Descending (press 2) order?\n\n");
scanf("%d",&sort);
if (sort==1){
printf("\n Ascending order:\n ");
printf("%c\n", names[i+1]);
for (i=0;i<names-1;i++)
{
for (j=i+1;j<names;j++)
{
if (strcmp(&names[i],&names[j])>0)
{
strcpy(&temp,&names[i]);
strcpy(&names[i],&names[j]);
strcpy(&names[j],&temp);
}
}
}
for (i=0;i<names;i++){
printf("\n%c\n",names[i]);
}
}
else if (sort==2){
printf("\n Descending order:\n ");
printf("%c\n", names[i-1]);
for (i=0;i>names ;i--)
{
for (j=i-1;j>names;j--)
{
if (strcmp(&names[i],&names[j])>0)
{
strcpy(&temp,&names[i]);
strcpy(&names[i],&names[j]);
strcpy(&names[j],&temp);
}
}
}
for (j=i-1; i>names; j--) {
printf("\n%c\n",names[i]);
}
}
return 0;
}
I keep