【发布时间】:2017-06-08 14:31:21
【问题描述】:
这是我的第一个 C 编程作业,我很困惑如何实现随机数。在我的程序中,我已经创建了一个 struct student,并创建了一个包含 10 个学生的数组。现在我必须为这 10 名学生生成随机 ID 号和考试成绩,但我的老师从来不知道如何准确地做到这一点。我也不允许更改变量或函数声明。到目前为止,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct student {
int id;
int score;
};
struct student *allocate() {
return calloc(sizeof(struct student), 10);
}
void generate(struct student* students){
/*
*Generate random ID and scores for 10 students, ID being between 0 and
* scores equal to (id* 10 % 50)
*/
}
int main() {
struct student *stud = allocate();
generate(stud);
return 0;
}
他也给出了这样的指示:
“编写一个函数void generate(struct student* students),填充作为参数传递的 10 个学生的数组的 id 和 score 字段。每个学生都应该有一个对应于他们在数组中的索引的 id(即数组中的第一个学生应该有 id 0). 如果每个学生的id都是x,那么学生的分数应该是(10 * x) % 50。"
【问题讨论】:
-
指令在哪里说“随机”? “与它们在数组中的索引相对应的 id”不是随机的,“分数应该是 (10 * x) % 50”也不是随机的。
-
指令没有说“随机数”。它特别说明数字将基于数组中的索引。
-
看到这也是我感到困惑的地方,因为他给学生的骨骼的 cmets 说是随机的,但实际的说明不是
-
@McGradyMan 所以这是要求的问题。 Stackoverflow 无法帮助您解决这个问题。只有您的教学人员才能做到这一点。去问问他们。
标签: c arrays memory-management random struct