#include <stdio.h>
#include <string.h>
void bubble_sort_grades(char [], int);
int main(void)
{
int menuswitch=1;
int amountofstudents;
int i;
int z;
char studentinfo[100];
char fname[50][100];
char lname[50][100];
char grade[50][100];
printf("Enter Amount of Students: ");
scanf("%d ", &amountofstudents);
for (i=0;i<amountofstudents;i++)
{
fgets(studentinfo, sizeof(studentinfo), stdin);
strcpy(fname[i], strtok(studentinfo, " "));
strcpy(lname[i], strtok(NULL, " "));
strcpy(grade[i], strtok(NULL, " "));
}
while (menuswitch==1)
{
int answer;
printf("Enter 1 for Alphabetical Sort (First Name) \n");
printf("Enter 2 for Alphabetical Sort (Last Name) \n");
printf("Enter 3 for Score Sort \n");
printf("Enter 0 to exit \n");
printf("Enter choice now: ");
scanf("%d", &answer);
if (answer==1)
{
bubble_sort_grades(grade,amountofstudents);
printf("%s\n", grade[0]);
printf("%s\n", grade[1]);
}
if (answer==2)
{
printf("In 2 \n");
}
if (answer==3)
{
printf("In 3 \n");
}
if (answer==0)
{
printf("Ending Program \n");
menuswitch=0;
}
}
}
void bubble_sort_grades(char grades2[], int amount)
{
int c, d , t;
for (c=0; c<(amount); c++)
{
for (d=0; d<amount-1; d++)
{
if (grades2[c]>grades2[d+1])
{
t=grades2[d+1];
grades2[d+1]=grades2[d];
grades2[d]=t;
}
}
}
}
Sorry for asking another questions but I need help on bubble sorting. I created a function to bubble sort the grades of students from the input. Yet, when I do this I get only the first grade sorted instead of the array.
Input:
John Smith 86
Victor Jones 76
Output: 68 76
char grade[50][100];
. Did it build this way for you? I think the code you have here does not match what you are actually using. – ryyker Oct 15 '13 at 21:55