On a previous question: "Write a C code that creates an honor list for the top six students at SMC. Your program does this by asking the user to enter the full names of all the top students, their majors, and their GPAs. Your program then prints the listing of the students in a sorted fashion based on their GPAs, in a three-column format (Name, Major, and GPA). " I received the below code, but I received errors when ran. Attached is a screenshot of the errors. Is there something I should change? #include #include int main() { int i,n=10,a,j; char NAME[n][30],MAJOR[n][30],str[10]; float GPA[n]; //execute for loop from 0 to number of top student for(i=0; iGPA[j+1] if(GPA[j]
On a previous question:
"Write a C code that creates an honor list for the top six students at SMC. Your
I received the below code, but I received errors when ran. Attached is a screenshot of the errors.
Is there something I should change?
#include <stdio.h>
#include <string.h>
int main()
{
int i,n=10,a,j;
char NAME[n][30],MAJOR[n][30],str[10];
float GPA[n];
//execute for loop from 0 to number of top student
for(i=0; i<n; i++)
{
//input student NAME
printf("Enter student NAME:");
scanf("%d",&a);
scanf("%[^\n]%*c",NAME[i]);
//input MAJOR name
printf("Enter MAJOR:");
scanf("%[^\n]%*c",MAJOR[i]);
//take GPA from user
printf("Enter GPA:");
scanf("%f",&GPA[i]);
}
//sort data in descending order based on GPA
//so first 6 student are top 6 students
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
//if you want to sort data in ascending order then use GPA[j]>GPA[j+1]
if(GPA[j]<GPA[j+1])
{
//swap GPA
float tmp=GPA[j];
GPA[j]=GPA[j+1];
GPA[j+1]=tmp;
//swap NAME
char s[30];
strcpy(s,NAME[j]);
strcpy(NAME[j],NAME[j+1]);
strcpy(NAME[j+1],s);
//swap MAJOR
char s1[30];
strcpy(s1,MAJOR[j]);
strcpy(MAJOR[j],MAJOR[j+1]);
strcpy(MAJOR[j+1],s1);
}
}
}
char sNAME[30]="Name";
char sMAJOR[20]="Major";
char sGPA[10]="GPA";
//print formated output after sorting
printf("\n%-30s %20s %10s\n",sNAME,sMAJOR,sGPA);
for(i=0; i<n; i++)
{
printf("%-30s %20s %10.2f\n",NAME[i],MAJOR[i],GPA[i]);
}
return 0;
}


Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 6 images









