Modify the example select.c from the class examples so that it sorts two paralle
ID: 3530171 • Letter: M
Question
Modify the example select.c from the class examples so that it sorts two parallel arrays. Use the existing array of scores and add a parallel array of floating-point data to track a GPA that corresponds to the score in the other array. Sort and print the set of parallel arrays first by score, highest to lowest, then by GPA, highest to lowest. The inputs will be the number of pairs of entries (n) to be made, followed by n pairs of score & GPA. Code: /* FILE: grades.c */ /* Loads an array with up to 50 values. Sorts the values into descending order. */ #include #define SIZE 50 int main( ) { int scores[SIZE]; int i, n, pass, item, position, temp; /* Get number of values to read */ printf("Please enter number of scores (%d or less): ", SIZE); scanf("%d", &n); /* Validate number entered by user. */ if (n<=SIZE && n>0){ /* Read score values into array */ for(i=0; i< scores[item]) position = item; if(pass != position){ temp = scores[pass]; scores[pass] = scores[position]; scores[position] = temp; } } /* Display scores in sorted order */ printf(" The scores in order. "); for(i=0; iExplanation / Answer
Hey i have corrected the program as per you want..so check it out..And let me know..It is printing the valuses in parrallel to each other ..
Code :
#include<stdio.h>
#define SIZE 50
int main( )
{ int scores[SIZE];
float GPA[SIZE];
int i, n, pass, item, position, temp;
int x,y,index_of_max1,index_of_max2;
float temp1;
/* Get number of values to read */
printf("Please enter number of scores (%d or less): ", SIZE);
scanf("%d", &n);
/* Validate number entered by user. */
if (n<=SIZE && n>0)
{
/* Read score values into array */
printf("Enter %d no of scores :",n);
for(i=0;i<n;i++)
{
scanf("%d",&scores[i]);
}
//Reading n values for GPA
printf("Enter %d value for GPA in the same order as score :",n);
for(i=0;i<n;i++)
{
scanf("%f",&GPA[i]);
}
//sorting the score arrays
for(x=0; x<n; x++)
{
index_of_max1 = x;
index_of_max2=x;
for(y=x; y<n; y++)
{
if(scores[index_of_max1] < scores[y])
{
index_of_max1 = y;
}
if(GPA[index_of_max2] < GPA[y])
{
index_of_max2=y;
}
}
//replaceing teh largest element in Scores
temp = scores[x];
scores[x] = scores[index_of_max1];
scores[index_of_max1] = temp;
//replacing the largest element in GPA array
temp1 = GPA[x];
GPA[x]=GPA[index_of_max2];
GPA[index_of_max2] = temp1;
}
printf("Printing the scores in decending Order along with there GPA values ");
//printlng scores array after sorting
printf("***SCORE*********GPA******");
for(i=0;i<n;i++)
{
printf(" %d %f ",scores[i],GPA[i]);
}
}
}
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.