I can’t figure out the syntax on how to generate random numbers for two members of a struct. It seems so simple because I know what I want to do, I just can’t make it work. I have the random generators I want but I don’t know how to pull it over into my struct.
Code:
#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 *r = malloc(sizeof(struct student) * 10);
/*return the pointer */
return r;
}
void generate(struct student *students, int *rid, int *rscore)
{
/*Generate random ID and scores for ten students, ID being between 1 and 10, scores between 0 and 100 */
*rid = rand() % 10 + 1; //ids
*rscore = rand() % 101; //scores
}
void output(struct student *students)
{
/*Output information about the ten students in the format:
ID1 Score1
ID2 score2
ID3 score3
...
ID10 score10 */
/*int rid, rscore;
gernerate(students, &rid, &rscore);
(*students).id = rid; (*students).score = rscore;
students->id = rid;
students->score = rscore; */
}
void summary(struct student *students)
{
/*Compute and print the minimum, maximum and average scores of the ten students */
output(students);
//printf("%d", *students);
}
void deallocate(struct student *stud)
{
/*Deallocate memory from stud */
free(stud);
}
int main()
{
struct student *stud = NULL;
/*call allocate */
allocate();
/*call generate */
//generate(stud, &rid, &rscore); //I am aware that these are also ........ty. How do I properly call them
/*call output */
output(stud);
/*call summary */
summary(stud);
/*call deallocate */
deallocate(stud);
return 0;
}