Write a program using C from the skeleton code provided below in which you consi
ID: 3710658 • Letter: W
Question
Write a program using C from the skeleton code provided below in which you consider the following structure:
struct student {
int id;
int score;
};
and the declaration in the main function:
struct student *st = 0;
Implement the following functions and demonstrate their functionality by calling them (in the order given) from the main function --
1. Write a function struct student* allocate() that allocates memory for ten students and returns the pointer.
2. Write a function void generate(struct student* students) that generates random IDs and scores for each of the ten students and stores them in the
array of students. You can only make use of the rand() function to generate random numbers( even though that will result in the same set of random
values being generated on each execution. Ensure that IDs are unique also and between 1 and 10 (both inclusive) and scores are between 0 and
100 (both inclusive). To generate unique IDs you can either use the brute-force random/check/repeat (generate a random integer between 1- 10 and
then confirm that it hasn't been used yet for a student ID ) or you can use the Fisher Yates shuffle -
(https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle).
3. Write a function void output(struct student* students) that prints the ids and scores of all the students. the output of the function need not to be sorted.
4. Write a function void summary(struct student* students) that prints the minimum score, maximum score and average score of the ten students.
5. Write a function void deallocate(struct student* stud) that frees the memory allocated to students. Check that students is not NULL (NULL == 0)
before you attempt to free it.
skeleton:
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
struct student{
int id;
int score;
};
struct student* allocate(){
/*Allocate memory for ten students*/
/*return the pointer*/
}
void generate(struct student* students){
/*Generate random and unique ID and random scores for ten students, ID being between 1 and 10, scores between 0 and 100*/
}
void output(struct student* students){
/*Output information about the ten students in the format:
ID1 Score1
ID2 score2
ID3 score3
...
ID10 score10*/
}
void summary(struct student* students){
/*Compute and print the minimum, maximum and average scores of the ten students*/
}
void deallocate(struct student* stud){
/*Deallocate memory from stud*/
}
int main(){
struct student* stud = NULL;
/*call allocate*/
/*call generate*/
/*call output*/
/*call summary*/
/*call deallocate*/
return 0;
}
Explanation / Answer
Hi Dear,
Please find my implementation.
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
struct student{
int id;
int score;
};
struct student* allocate(){
/*Allocate memory for ten students*/
struct student *p = (struct student *) malloc(10 * sizeof(struct student));
/*return the pointer*/
return p;
}
void generate(struct student* students){
/*Generate random and unique ID and scores for ten students, ID being between 1 and 10, scores between 0 and 100*/
int i;
for(i = 0; i < 10; i++)
{
(students + i) -> id = rand() % 10 + 1;
(students + i) -> score = rand() % 101;
}
}
void output(struct student* students){
/*Output information about the ten students in the format:
ID1 Score1
ID2 score2
ID3 score3
...
ID10 score10*/
struct student s;
int i;
for(i = 0; i < 10; i++)
{
s = students[i];
printf(" %d %d", s.id, s.score);
}
}
void summary(struct student* students){
/*Compute and print the minimum, maximum and average scores of the ten students*/
/*start by considering 1st student's marks as min and max and update as we process*/
int min=students[0].score, max = students[0].score, total = 0,i;
float avg;
for(i = 0; i < 10; i++)
{
if(students[i].score < min)
min = students[i].score;
if(students[i].score > max)
max = students[i].score;
total += students[i].score;
}
avg = total / 10;
printf(" Min = %d Max = %d Average = %.2f",min, max, avg);
}
void deallocate(struct student* stud){
/*Deallocate memory from stud*/
if(stud != NULL)
free(stud);
}
int main(){
struct student* stud = NULL;
/*call allocate*/
stud = allocate();
/*call generate*/
generate(stud);
/*call output*/
output(stud);
/*call summary*/
summary(stud);
/*call deallocate*/
deallocate(stud);
return 0;
}
output
8 65
4 41
1 15
5 11
4 69
1 23
3 63
8 4
8 49
1 99
Min = 4 Max = 99 Average = 43.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.