Hello, I am having trouble with a problem in my C language class. I am trying to
ID: 3877315 • Letter: H
Question
Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements:
1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10
2. Use typedef to define the following struct type:
struct {
char name[MAX_SIZE1];
int scores[MAX_SIZE2];
}
3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the input will always be valid.
4. Dynamically allocate memory for an array of n entries; each entry is a structure defined in step 2.
5. Store student name and randomly generate 10 scores in the range between 50 and 100 for each student in the dynamically allocated memory. You may declare and initialize an array of 30 names in an array, and use them as student names.
6. Use qsort to sort the array based on the total scores in the descending order.
7. Output the sorted grade book in a format of your choice.
8. Output the elapsed time of your program execution.
My problem is that I dont know how to use qsort to sort the array based on the total scores in descending order. Can you help me? I have pasted my current code below:
#include<stdio.h>
#include <time.h>
#include<stdlib.h>
#define MAX_SIZE1 20
#define MAX_SIZE2 10
typedef struct student
{
char name[MAX_SIZE1];
int score[MAX_SIZE2];
};
/*comparison function "comp()" start here*/
int comp(struct student *s1,struct student *s2)
{
if(sum(s1)>sum(s2))
return 1; /* if total score of s1 is greater than total score of s2 return 1 */
else
return 0;
}
int sum(struct student *s) /* it find the sum of grade */
{
int i,su=0;
for ( i=0;i<10;i++)
su-su+s->score[1];
return su;
}
/* comparison function "comp()" end */
int main()
{
int n,i,j;
time_t t;
printf("Enter number of students: ");
scanf("%d",&n);
struct student s[n]; /*dynamic allocation of array*/
for(i=0; i<n; i++)
{
printf(" Enter the name of student:");
scanf("%s",s[i].name);
srand((unsigned) time(&t));
for(j=0;j<10;j++)
s[i].score[j]=50+rand()%50;
}
/*qsort(s);*/
/* output sorted result*/
printf("Name Subject1 Subject2 Subject3 Subject4 Subject5 Subject6 Subject7 Subject8 Subject9 Subject10/t Total Score");
for(i=0;i<n;i++)
{
printf(" %s ",s[i].name);
for(j=0;j<10;j++)
printf("%d ",s[i].score[j]);
printf("%d ",sum(&s[i]));
}
return 0;
}
Explanation / Answer
Basically qsort means quick sort. Qsort uses the quicksort algorithm to sort the given array. so i m first giving the algorithm of quick sort and then i will give the algorithm of Qsort.
as in quicksort the main function is the partition function and the quicksort function. Thats why i m just writing these two for explaination.
CODE
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
So basically the abve algorithm is used in Qsort but the most important part of Qsort is the Comparator function.
the prototype of Qsort() is:-
void qsort (void* base, size_t num, size_t size,
int (*comparator)(const void*,const void*));
COMPARATOR FUNCTION:-
The comparator function takes two arguments and contains logic to decide their relative order in sorted output. The idea is to provide flexibility so that qsort() can be used for any type (including user defined types) and can be used to obtain any desired order (increasing, decreasing or any other).
CODE with the help of an example:-
For example, let there be an array of students where following is type of student.
struct Student
{
int age, marks;
char name[20];
};
Lets say we need to sort the students based on marks in descending order. The comparator function will look like:
int comparator(const void *p, const void *q)
{
int l = ((struct Student *)p)->marks;
int r = ((struct Student *)q)->marks;
return (r - l);
}
void qsort (void* base, size_t num, size_t size,
int (*comparator)(const void*,const void*));
COMPARATOR FUNCTION:-
The comparator function takes two arguments and contains logic to decide their relative order in sorted output. The idea is to provide flexibility so that qsort() can be used for any type (including user defined types) and can be used to obtain any desired order (increasing, decreasing or any other).
CODE with the help of an example:-
For example, let there be an array of students where following is type of student.
struct Student
{
int age, marks;
char name[20];
};
Lets say we need to sort the students based on marks in descending order. The comparator function will look like:
int comparator(const void *p, const void *q)
{
int l = ((struct Student *)p)->marks;
int r = ((struct Student *)q)->marks;
return (r - l);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.