为学生ID和分数生成正确的输出并找到最大值和最小值?

时间:2015-10-02 22:29:54

标签: c pointers

我正在编写C语言编程,这部分是为HW添加的,但我不知道如何显示这些代码注释建议。在output()中的评论下方,我使用了此语句printf("%d\t%d\n", students.id, students.score);,但我没有正确获得ID和得分。这是我不确定的。所以它已经生成并且后来在main中打印,但并不完全是如此。

然后我不确定如何在summary()中找到最小,最大和平均分数,因为它们是随机生成的。我会看一下students[i].id and 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 for ten students*/
    struct student* s = malloc(10 * sizeof(struct student));
    assert (s != 0);
    /*return the pointer*/
    return s;
}

void generate(struct student* students){
    /*Generate random ID and scores for 10 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()%(100 - 0 + 1) + 0);
           printf("%d, %d\n", students[i].id, students[i].score);
   }
}

void output(struct student* students){
   /*Output information about the ten students in the format:
   ID1 Score1
              ID2 score2
              ID3 score3
              ...
              ID10 score10*/
   printf("%d\t%d\n", students.id, students.score);


}

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*/
   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;
}

关于如何使用students[i].id and students[i].score的解释是我真正需要帮助的,因为我总是在另一个函数中使用在一个函数中创建的值时遇到了麻烦。

1 个答案:

答案 0 :(得分:1)

除了:

之外,一切看起来都不错
void output(struct student* students){
    /*Output information about the ten students in the format:
          ID1 Score1
          ID2 score2
          ID3 score3
          ...
          ID10 score10*/
   printf("%d\t%d\n", students.id, students.score);
}

应该是:

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("%d\t%d\n", students[i].id, students[i].score);
   }
}

建议改进

不要在所有功能中硬编码10。将其作为论据传递。

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;
   for (i = 0; i < 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;
   for (i = 0; i < num; i++) {
      printf("%d\t%d\n", students[i].id, students[i].score);
   }
}

void summary(struct student* students, int num){
   /*Compute and print the minimum, maximum and average scores of the ten    students*/
}

void deallocate(struct student* stud){
   /*Deallocate memory from 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;
}

这样可以更轻松地从一个地方更改学生人数。