Please complete the c++ code. The code that needs completing is outlined in the
ID: 3781930 • Letter: P
Question
Please complete the c++ code. The code that needs completing is outlined in the comments.
#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 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
// C code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
struct student
{
int id;
int score;
};
struct student* allocate()
{
/*Allocate memory for ten students*/
struct student* s = malloc(10 * sizeof(struct student));
/*return the pointer*/
return s;
}
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, j;
int new;// To check for new initials
for (i = 0; i < 10; i++)
{
new = 1;
students[i].id = ((rand() % 10) + 1);
for (j = 0; j < i; j++)
{
if (students[i].id == students[j].id)
{
new = 0;
break;
}
}
if(new == 0)
{
i--;
continue;
}
students[i].score = ((rand() % 100));
}
}
void output(struct student* students)
{
/*Output information about the ten students in the format: ID1 Score1 ID2 score2 ID3 score3 ... ID10 score10*/
int i;
for (i = 0;i < 10; i++)
{
printf("ID: %d ", students[i].id);
printf("Score: %d ", students[i].score);
}
}
void summary(struct student* students)
{
/*Compute and print the minimum, maximum and average scores of the ten students*/
int i, min, max;
float avg, sum = 0;
min = students[0].score;
max = students[0].score;
for (i = 0; i<10; i++)
{
if (students[i].score < min)
{
min = students[i].score;
}
if (students[i].score > max)
{
max = students[i].score;
}
sum = sum + students[i].score;
}
avg = (sum / 10);
printf("Minimum score: %d ", min);
printf("Maximum score: %d ", max);
printf("Average score: %g ", avg);
}
void deallocate(struct student* s)
{
/*Deallocate memory from stud*/
assert(s != NULL); // Break off program if null, so we don't get a segmentation fault in freeing the memory.
free(s);
}
int main()
{
srand(time(NULL));
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:
ID: 1 Score: 35
ID: 8 Score: 71
ID: 2 Score: 33
ID: 7 Score: 49
ID: 4 Score: 88
ID: 3 Score: 6
ID: 6 Score: 19
ID: 10 Score: 48
ID: 9 Score: 5
ID: 5 Score: 15
Minimum score: 5
Maximum score: 88
Average score: 36.9
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.