我写了一些代码来生成一个结构数组。 id变量旨在是唯一的并随机生成。然而,似乎正在发生的事情是,如果生成函数(生成并填充结构数组)遇到数组中的匹配数字,则将flag变量设置为0并退出do循环而不创建新的随机数重新检查比赛。然后当循环退出时,代码继续进行并将匹配的随机数分配给数组中的空白点。作为一个警告,我意识到只需要获取所有10个可能的整数,移动它们并填充数组会更简单,但我正试图使用一个小样本来获取rand()的挂起,这样我就可以看到它是什么了在调试器中做。我怀疑我只是盯着这个太长时间并尝试了太多的东西,但任何建议都会受到赞赏。感谢。
编辑:只是为了澄清我的问题,特别关注do循环和我需要做什么来确保找到匹配时,程序生成一个新的随机数并开始再次搜索匹配。应该对数组中的每个位置重复此操作,直到每个id元素都是唯一的。目前,当我运行程序时,我仍然会收到重复的数字。
#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 ten students, ID being between 1 and 10, scores between 0 and 100*/
int i, j;
int flag;
int randNum = 0;
for (i = 0; i < 10; i++) {
flag = 1;
do {
randNum = (rand()%10 + 1); //generate random ID for each student
for (j = 0; j < 10 && flag == 1; j++) { //search array for matching numbers
if (students[j].id == randNum) {
flag = 0;
}
if (j == 9 && flag == 1) {
flag = 0;
}
}
}
while (flag == 1); //set condition
students[i].id = randNum;
students[i].score = (rand()%(100 - 0 + 1) + 0); //generate random score for each student
}
}
void output(struct student* students){
/*Output information about the ten students in the format:
ID1 Score1
ID2 score2
ID3 score3
...
ID10 score10*/
int i;
printf("Student scores: \n\n");
for (i = 0; i < 10; i++) {
printf("\t%d, %d\n", students[i].id, students[i].score);
}
}
void summary(struct student* students){
/*Compute and print the minimum, maximum and average scores of the ten students*/
int sumS, minS, maxS, avgS, i, j, tempID, tempS;
printf("Sorted students by scores: \n");
for (i = 0; i < 10; i++) {
sumS += students[i].score;
for (j = 0; j <10; j++) {
if (students[i].score < students[j].score) {
tempS = students[j].score;
tempID = students[j].id;
students[j].score = students[i].score;
students[j].id = students[i].id;
students[i].score = tempS;
students[i].id = tempID;
}
}
}
for (i = 0; i < 10; i++) {
printf("\t%d, %d\n", students[i].id, students[i].score);
}
printf("Minimum score: %d\n", minS = students[0].score);
printf("Maximum score: %d\n", maxS = students[9].score);
printf("Average score: %d", avgS = sumS/10);
}
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;
}
答案 0 :(得分:3)
如果已经选择了数字,则将标记设置为0
,因此您应该测试while(flag == 0)
,并在循环开始时将标记重新设置为1
:< / p>
do {
flag = 1;
randNum = (rand()%10 + 1); //generate random ID for each student
for (j = 0; j < i && flag == 1; j++) { //search array for matching numbers
if (students[j].id == randNum) {
flag = 0;
}
}
}
while (flag == 0); //set condition
现在,flag == 0
表示“已经看过,请再试一次”,flag == 1
表示“这是一个新号码,请继续将其写入阵列”。
此外,您只填充了索引< i
的数组槽,因此比较循环不应转到9
,而应转到i-1
。