Question

In: Computer Science

Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...

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*/
}

Solutions

Expert Solution

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!!


Related Solutions

#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size);...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size); int main(){ int arraySize, limit, count, srand(time(0)); print f("Enter the size of array\n"); scanf("%d", arraySize); int array[arraySize]; printf("Enter the upper limit\n"); scanf("%d", &limit); count = 0; while(count <= arraySize){ array[count] = (rand() % (limit + 1)); count++; } printArray(array, &arraySize); sort(array, arraySize); printArray(array, arraySize); Return 0; } void printArray(int a[], int size){ int i = 0; printf("Array: ["); while(i < size){ if(i != size...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; };...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; }; int n; void accept(struct Bank_Account_Holder[], int); void display(struct Bank_Account_Holder[], int); void save(struct Bank_Account_Holder[], int); void load(struct Bank_Account_Holder[], int); int search(struct Bank_Account_Holder[], int, int); void deposit(struct Bank_Account_Holder[], int, int, int); void withdraw(struct Bank_Account_Holder[], int, int, int); int lowBalenquiry(int,int); void main(void) { clrscr(); struct Bank_Account_Holder data[20]; int choice, account_no, amount, index; printf("NHU Banking System\n\n"); printf("Enter the count of records: "); scanf("%d", &n); accept(data, n); do {...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements...
#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements one by one for (i = 0; i < c; i++) { // Checking if the picked element is already printed for (j = 0; j <= i; j++) { // If current element is already there in the array, break from j loop if (arr[i] == arr[j]) { break; } } // If it is not printed earlier and is within 10-100, then...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */ void printArray(int *arr, int size){ int i; for( i = 0; i < size; i++) { // Print each element out printf("%d ", *(arr+i)); //Print addresses of each element printf("%p", (arr+i)); //Printing a new line printf("\n"); } } int main() { // Allows user to specify the original array size, stored in variable n1. printf("Enter original array size: "); int n1 = 0; scanf("%d",...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT