C programming: So this is what I have and here are the requirements as well: My
ID: 3854720 • Letter: C
Question
C programming:
So this is what I have and here are the requirements as well:
My only problem I can't figure out how to sort the data by only the final score which is in the Void insrtingsort() Function.
task:
1. Write function named printList() that displays the list to the screen and call it in main() to print the unsorted data
2. Write a second sort function that sorts the array in descending order
by the final exam score, then call it from main().
3. Call the printList() function again to show the results of the second sort.
4. Replace the "Sample Output" below with the new results
*/
#include <stdio.h>
#include <string.h>
#define NUM_STU 7
typedef struct
{
char name[31];
int midterm[2];
int final;
} STUDENT;
void insertionSort (STUDENT list[], STUDENT *pLast);
void printList (STUDENT list[], STUDENT *pLast, STUDENT *stuList, STUDENT *pStu);
int main (void)
{
STUDENT *pStu, *pLast;
STUDENT stuList[NUM_STU] =
{
{"Taylor, Noah", {85, 94}, 92},
{"Smith, Olivia", {91, 89}, 86},
{"Brown, Liam", {87, 88}, 90},
{"Davis, Emma", {96, 88}, 97},
{"Garcia, Mason", {79, 93}, 92},
{"Stringer, James", {97, 91}, 96},
{"Lopez, Sophia", {83, 78}, 95}
};
pLast = stuList + NUM_STU - 1;
printList(stuList, pLast, stuList, pStu);
insertionSort (stuList, pLast);
printf("Sorted data: ");
for (pStu = stuList; pStu <= pLast; pStu++)
{
printf("%-30s %4d %4d %4d ",
pStu->name,
pStu->midterm[0],
pStu->midterm[1],
pStu->final);
}
printf(" ");
return 0;
}
/* ========================================================
Sort list using Insertion Sort.
Pre list[] must contain at least one element
size is index to last element in list
Post list has been rearranged.
*/
void insertionSort (STUDENT list[], STUDENT *pLast)
{
STUDENT temp;
STUDENT *pCurr;
STUDENT *pWalk;
/*for (pCurr = list - 1; pCurr <= pLast; pCurr++)
{
temp = *pCurr;
pWalk = pCurr - 1;
while (pWalk >= list && strcmp(temp.final, pWalk->final) > 0)
{
*(pWalk + 1) = *pWalk;
pWalk--;
}
*(pWalk + 1) = temp;
}
int i, j;
for (i = 0; i < list; ++i)
{
for (j = i + 1; j < (list-1); ++j)
{
if (final[i] < final[j])
{
list = final[i];
final[i] = number[j];
number[j] = list;
}
}
}
}
*/
void printList(STUDENT list[], STUDENT *pLast, STUDENT *stuList, STUDENT *pStu){
printf("Unsorted data: ");
for (pStu = stuList; pStu <= pLast; pStu++)
{
printf("%-30s %4d %4d %4d ",
pStu->name,
pStu->midterm[0],
pStu->midterm[1],
pStu->final);
}
printf(" ");
}
Explanation / Answer
The corrected program is given below:
#include <stdio.h>
#include <string.h>
#define NUM_STU 7
typedef struct
{
char name[31];
int midterm[2];
int final;
} STUDENT;
void bubbleSort (STUDENT list[]);
void printList (STUDENT list[], STUDENT *pLast, STUDENT *stuList, STUDENT *pStu);
int main (void)
{
STUDENT *pStu, *pLast;
STUDENT stuList[NUM_STU] =
{
{"Taylor, Noah", {85, 94}, 92},
{"Smith, Olivia", {91, 89}, 86},
{"Brown, Liam", {87, 88}, 90},
{"Davis, Emma", {96, 88}, 97},
{"Garcia, Mason", {79, 93}, 92},
{"Stringer, James", {97, 91}, 96},
{"Lopez, Sophia", {83, 78}, 95}
};
pLast = stuList + NUM_STU - 1;
printList(stuList, pLast, stuList, pStu);
bubbleSort (stuList);
printf("Sorted data: ");
for (pStu = stuList; pStu <= pLast; pStu++)
{
printf("%-30s %4d %4d %4d ",
pStu->name,
pStu->midterm[0],
pStu->midterm[1],
pStu->final);
}
printf(" ");
return 0;
}
void bubbleSort (STUDENT list[])
{
STUDENT temp;
int i, j;
for (i = 0; i < NUM_STU; ++i)
{
for (j=i+1;j<=NUM_STU;++j)
{
if (list[i].final < list[j].final)
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
}
void printList(STUDENT list[], STUDENT *pLast, STUDENT *stuList, STUDENT *pStu){
printf("Unsorted data: ");
for (pStu = stuList; pStu <= pLast; pStu++)
{
printf("%-30s %4d %4d %4d ",
pStu->name,
pStu->midterm[0],
pStu->midterm[1],
pStu->final);
}
printf(" ");
}
In the program bubblesort is used to sort the data in descending order by the final marks of the students.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.