c - Generate proper output for student id and score and find max and min? -


i'm working through programming in c book , part added hw, i'm not sure how display these quite code comments suggest. below comments in output(), used statement printf("%d\t%d\n", students.id, students.score); i'm not getting id , score correctly. i'm not sure about. it's getting generated , later printed in main, not quite how it's supposed to.

then i'm not sure how find minimum, max , avg scores in summary() because randomly generated. @ students[i].id , students[i].score?

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <conio.h> #include <assert.h>  struct student{     int id;     int score; };  struct student* allocate(){     /*allocate memory ten students*/     struct student* s = malloc(10 * sizeof(struct student));     assert (s != 0);     /*return pointer*/     return s; }  void generate(struct student* students){     /*generate random id , scores 10 students, id being between 1 , 10,   scores between 0 , 100*/     int i;    (i = 0; < 10; i++) {            students[i].id = (rand()%10+1);            students[i].score = (rand()%(100 - 0 + 1) + 0);            printf("%d, %d\n", students[i].id, students[i].score);    } }  void output(struct student* students){    /*output information ten students in format:    id1 score1               id2 score2               id3 score3               ...               id10 score10*/    printf("%d\t%d\n", students.id, students.score);   }  void summary(struct student* students){     /*compute , print minimum, maximum , average scores of ten    students*/ }  void deallocate(struct student* stud){    /*deallocate memory stud*/    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; } 

an explanation on how use students[i].id , students[i].score need because i've had trouble using values created in 1 function, in function.

everything looks me except:

void output(struct student* students){     /*output information ten students in format:           id1 score1           id2 score2           id3 score3           ...           id10 score10*/    printf("%d\t%d\n", students.id, students.score); } 

that should be:

void output(struct student* students){     /*output information ten students in format:           id1 score1           id2 score2           id3 score3           ...           id10 score10*/    int i;    ( = 0; < 10; ++i )    {       printf("%d\t%d\n", students[i].id, students[i].score);    } } 

suggestion improvement

don't hard code 10 in functions. pass argument.

struct student* allocate(int num){    struct student* s = malloc(num * sizeof(struct student));    assert (s != 0);    return s; }  void generate(struct student* students, int num){    int i;    (i = 0; < num; i++) {       students[i].id = (rand()%num+1);       students[i].score = (rand()%(100 - 0 + 1) + 0);       printf("%d, %d\n", students[i].id, students[i].score);    } }  void output(struct student* students, int num){    int i;    (i = 0; < num; i++) {       printf("%d\t%d\n", students[i].id, students[i].score);    } }  void summary(struct student* students, int num){    /*compute , print minimum, maximum , average scores of ten    students*/ }  void deallocate(struct student* stud){    /*deallocate memory stud*/    free(stud); }  int main() {    struct student* stud = null;     int num = 10;    /*call allocate*/    stud = allocate(num);    /*call generate*/    generate(stud, num);    /*call output*/    output(stud, num);    /*call summary*/    summary(stud, num);    /*call deallocate*/    deallocate(stud);    return 0; } 

this makes easier change number of students 1 place.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -