In: Computer Science
Please paraphrase this c code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sortGrades(int arr[], int size, int status, char
names[][20]);
void printer(int grades[], int size, char names[][20]);
void sortNames(char arr[][20], int size, int status, int
grades[]);
void nameSearch(int grades[], int size, char names[][20]);
void numSearch(int grades[], int size, char names[][20]);
int main()
{
int i;
int size;
int option;
do
{
printf("\n\nInput Number of Students or 0 to exit : ");
scanf("%d", &size);
if (size == 0)
{
break;
}
char names[size][20];
int grades[size];
for (i = 0; i < size; i++)
{
printf("\nEnter the Name of Student %d : ", i + 1);
scanf("%s", &names[i]);
printf("Enter the Grades of Student %d : ", i + 1);
scanf("%d", &grades[i]);
}
do
{
printf("\n\nEnter\n1 for sort by Name(A-Z)\n2 for sort by
Name(Z-A)\n3 for sort by Grades(Ascending)\n4 for sort by
Grades(Descending)\n5 for search by Name\n6 for search by Grade's
limit\n0 to re enter data\n");
scanf("%d", &option);
switch (option)
{
case 1:
sortNames(names, size, 1, grades);
printer(grades, size, names);
break;
case 2:
sortNames(names, size, 0, grades);
printer(grades, size, names);
break;
case 3:
sortGrades(grades, size, 0, names);
printer(grades, size, names);
break;
case 4:
sortGrades(grades, size, 1, names);
printer(grades, size, names);
break;
case 5:
nameSearch(grades, size, names);
break;
case 6:
numSearch(grades, size, names);
break;
default:
break;
}
} while (option != 0);
} while (size != 0);
return 0;
}
void sortGrades(int arr[], int size, int status, char
names[][20])
{
int i, j; /*counter*/
int temp;
char temp2[100];
for (j = 0; j < size - 1; j++)
/*outer for loop to repeat to repeat inner loop without the largest
number selected before*/
{
for (i = 0; i < size - 1 - j; i++) /*inner for loop to swap the
largest number to the end*/
if (status == 0)
{
if (arr[i] < arr[i + 1])
{
// swapping using temp
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
strcpy(temp2, names[i]);
strcpy(names[i], names[i + 1]);
strcpy(names[i + 1], temp2);
}
}
else
{
if (arr[i] > arr[i + 1])
{
// swapping using temp
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
strcpy(temp2, names[i]);
strcpy(names[i], names[i + 1]);
strcpy(names[i + 1], temp2);
}
}
}
}
void sortNames(char arr[][20], int size, int status, int
grades[])
{
int i, j; /*counter*/
char temp[100]; /*temporary value for swap*/
int temp2;
for (j = 0; j < size - 1; j++)
/*outer for loop to repeat to repeat inner loop without the largest
number selected before*/
{
for (i = 0; i < size - 1 - j; i++) /*inner for loop to swap the
largest number to the end*/
if (status == 0)
{
if (strcmp(arr[i], arr[i + 1]) < 0)
{
// swapping using temp
strcpy(temp, arr[i]);
strcpy(arr[i], arr[i + 1]);
strcpy(arr[i + 1], temp);
temp2 = grades[i];
grades[i] = grades[i + 1];
grades[i + 1] = temp2;
}
}
else
{
if (strcmp(arr[i], arr[i + 1]) > 0)
{
// swapping using temp
strcpy(temp, arr[i]);
strcpy(arr[i], arr[i + 1]);
strcpy(arr[i + 1], temp);
temp2 = grades[i];
grades[i] = grades[i + 1];
grades[i + 1] = temp2;
}
}
}
}
void nameSearch(int grades[], int size, char names[][20])
{
int i;
char key[20];
int fail = 0;
printf("\nInput a Name that you want to find : ");
scanf("%s", &key);
for (i = 0; i < size; i++)
{
if (strcmp(names[i], key) == 0)
{
printf("\nMatch Found\nName : %s\nGrade : %d\nIndex Number : %d",
names[i], grades[i], i);
fail++;
}
}
if (fail == 0)
{
printf("\nMatch NOT Found");
}
}
void numSearch(int grades[], int size, char names[][20])
{
int j; /*counter*/
int key, condition, option;
printf("Enter a grade as limit/pivot to filter data : ");
scanf("%d", &key);
printf("Enter condition:\n1 for Greater than %d\n2 for Less than
%d\n", key, key);
scanf("%d", &option);
printf(" Name");
printf(" grades\n");
for (j = 0; j < size; j++)
{
switch (option)
{
case 1:
if (grades[j] > key)
{
printf("%10s", names[j]);
printf("%10d\n", grades[j]);
}
break;
case 2:
if (grades[j] < key)
{
printf("%10s", names[j]);
printf("%10d\n", grades[j]);
}
break;
default:
break;
}
}
}
void printer(int grades[], int size, char names[][20])
{
int j; /*counter*/
printf(" Name");
printf(" grades\n");
for (j = 0; j < size; j++)
{
printf("%10s", names[j]);
printf("%10d\n", grades[j]);
} /* for loop to print all of reversed array content*/
}
so basically in these code thay are sorting the student according to there grades and num.
I will first explain main to you then Sortgrade yhen sortname .
lets come to main() first ok->
so i hope you that all the program runs from main function so in this main function yhey will first initilize i then size here and option.i is declare for the looping ,size is declare for the number of student in the class and option is declare becoz of the switch case.than they use the do whie loop in which they first take the user infor for the no. of student in the class if the user enter the student <0 then it will terminate the loop and program will end automatically.if user enter a valid value which means >0 that it will show you the option from where you should have to choose one option . if you choose option 1 it will sort the student list in staring from [A-Z].if you choose option2 then it will sort thr name from[Z-A]
if you choose option 3 then it will sort the grade inthe assending order. of you choose otion 4 than it will sort the grdein assending order/if you choose the otion 5 it will search with resecpt to name and if you choose option 6 it will search with respect to grade.
Now we will see what sortgrade() do-->
This function will take 4 argument 1 is the array second is the
size of the arrayand the 3 one is the ststus which decide how to
sort array decending or assending if thr status is 0 it will sort
yhe array in thje assending order if the status is 1 then will sort
the array in desending order.in the for loop iy will check the
status of the ststus is 0 then it will check the if (arr[i] <
arr[i + 1])
{
// swapping using temp
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
strcpy(temp2, names[i]);
strcpy(names[i], names[i + 1]);
strcpy(names[i + 1], temp2);
}
this will execute this means if a[i]<a[i+1] it will swap that two value name thre name respectively.
and if the ststus is 1 then it will excute this
if (arr[i] > arr[i + 1])
{
// swapping using temp
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
strcpy(temp2, names[i]);
strcpy(names[i], names[i + 1]);
strcpy(names[i + 1], temp2);
}
in this code if the a[i]>a[i+1] that means if a[i] is greater than a[i+1] then it wil swap there value and there name respectively thats all here the gradescore is doing.
no we will move to the nameSearch()
in this function it will take 3 argument which are name array grade array and the size of the arrays . Then in the function it will take a input from the user what is the name to be searched then it will go in the for loop in the for llop they use strcmp() which will compare the name that user searched and the the name array if the strcmp gives 0 that means there is a match if it will five any value other than 0 that means there is no name present in that array . If the name is present in the name array than it will show you the garde of that student and the name of that student.
no we will move to the numSearch()
In this function the user enter the garde or marks of the student and then the user have to choose a option between either he want to see grade of that student who marks less than the user given grade or more than the user given grade from these basic the witch case work if user chhose the 1 option than case 1 will execute and show all the student whose grade is more than the user give grade and if the user choose the second option than it will shoe the details of that student whose grade is less tha user given grade.
The printer() function normally print the details.
NOW WE WILL SEE SortName()
so basically in sort name function he is using the string method called strcmp so strcmp will do return the differnece between the fisrt unmatch charcter Ascii value using ASCII value he will sort the the array from [A-Z] or from[Z-A] if the status is 0 it will sort from [A-Z] and if the status is 1 it will sort from [Z-A] simply using strcpy function and the swap between the array and it swap grade respectively.
I HOPE YOU UNDERSTAND WHAT IS HAPPEING HERE THANK YOU IF ANY QUERY POST IN THE COMMENT I WILL REPLAY DEFINITELY THANK YOU!!