使用嵌套循环,我们必须创建一个程序,该程序从用户获取0-99范围内的数字,并使用种子随机数生成器来尝试猜测数字。我们必须跟踪RNG在生成用户号码之前生成的数量。我们还必须执行此过程50次,然后计算RNG猜测我们的数字所需的平均尝试次数。 这就是我到目前为止所有的事情,我很难过:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
int userInput;
int randoms = rand() % 100;
int numCalls;
int i;
float average = 0.0;
printf("Please enter a number in between 0 and 99: ");
scanf(" %d", &userInput);
for( i = 0; i < 50; i++)
{
while (userInput != randoms);
{
numCalls = numCalls + 1;
if (userInput = randoms)
{
float average = numCalls/50.0;
}
}
printf("Number of iterations it took for a matching number: %d\n", numCalls);
printf("Average number of iterations to find a match: %.2f\n", average);
}
return;
}
答案 0 :(得分:0)
randoms
值if (userInput=randoms)
不是布尔函数,应该是if (userInput==randoms)
而不是以下是我的建议:
int main()
{
srand(time(NULL));
int userInput;
int randoms = rand() % 100;
int numCalls;
int i;
float average = 0.0;
int totalCalls=0;
printf("Please enter a number in between 0 and 99: ");
scanf(" %d", &userInput);
for( i = 0; i < 50; i++)
{
numCalls = 0; //reset iteration number
do{
randoms = rand()%100;
numCalls++
} while (randoms!=userInput);
totalCalls += numCalls;
printf("Number of iterations it took for a matching number: %d\n", numCalls);
average = (float)totalCalls/(float)i;
printf("Average number of iterations to find a match: %.2f\n", average);
}
return;
}
答案 1 :(得分:0)
在这里,您从用户那里获得1个输入,并且仅计算该输入所需的RNG呼叫数。将这个数字除以50将不会给你平均值。
您想要输入50次,即scanf语句应该在循环内。 如果numCalls [i]是第i个输入所需的随机数发生器调用次数,则平均值是所有50个输入的总和除以50.
答案 2 :(得分:0)
除了更新randoms
和相等比较的技术失败之外,您必须验证所有用户输入,验证提供的值在您的预期范围内。否则,如果发生匹配或输入失败,则可能会调用未定义行为。
scanf
有一个返回。您必须使用它来验证格式字符串中包含的每个转换说明符的成功转换(例如"%d"
是1
转换)。如果执行了成功转换,则必须验证该值是否在0 - 99
之间。您可以通过以下方式实现这一目标:
#define MAXN 99
...
if (scanf ("%d", &num) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
if (num < 0 || num > MAXN) { /* validate num is within range */
fprintf (stderr, "error: value out of range: %d\n", num);
return 1;
}
考虑需要变量的范围。是的,您可以将它们全部声明为最佳,但如果它们仅在循环中使用等等,则可以减少名称冲突的可能性,或者如果您在需要的范围内声明值,则可以减少以后无意中修改的值。 (对于C99或更高版本,您可以在循环声明中声明for
循环变量(例如for (int i = 0; ...)
)
将这些部分组合在一起,您可以通过验证输入来防止UB,如上所述,并使用以下内容整理变量声明的范围:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NTIMES 50 /* if you need a constant, declare one (or two) */
#define MAXN 99
int main (void) {
int i, num;
double avg = 0.0;
srand (time (NULL));
printf ("enter a number (0-%d): ", MAXN);
if (scanf ("%d", &num) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
if (num < 0 || num > MAXN) { /* validate num is within range */
fprintf (stderr, "error: value out of range: %d\n", num);
return 1;
}
for (i = 0; i < NTIMES; i++) { /* loop 50 time for average */
int nrands = 0,
randnum = rand() % (MAXN + 1); /* get 1st random */
/* loop until equal updating counter nrands */
for (; randnum != num; randnum = rand() % (MAXN + 1))
nrands += 1;
printf ("test[%2d] : %4d iterations to match.\n", i+1, nrands);
avg += nrands; /* keep sum in avg */
}
avg /= (double)NTIMES; /* divide for final average */
printf ("\nAverage number of iterations to find match: %.2f\n", avg);
return 0;
}
示例使用/输出
$ ./bin/iters2match
enter a number (0-99): 31
test[ 1] : 19 iterations to match.
test[ 2] : 30 iterations to match.
test[ 3] : 239 iterations to match.
test[ 4] : 116 iterations to match.
test[ 5] : 220 iterations to match.
test[ 6] : 198 iterations to match.
test[ 7] : 64 iterations to match.
test[ 8] : 77 iterations to match.
test[ 9] : 106 iterations to match.
test[10] : 94 iterations to match.
test[11] : 123 iterations to match.
test[12] : 77 iterations to match.
test[13] : 167 iterations to match.
test[14] : 23 iterations to match.
test[15] : 3 iterations to match.
test[16] : 226 iterations to match.
test[17] : 58 iterations to match.
test[18] : 190 iterations to match.
test[19] : 44 iterations to match.
test[20] : 204 iterations to match.
test[21] : 134 iterations to match.
test[22] : 30 iterations to match.
test[23] : 40 iterations to match.
test[24] : 59 iterations to match.
test[25] : 21 iterations to match.
test[26] : 218 iterations to match.
test[27] : 8 iterations to match.
test[28] : 21 iterations to match.
test[29] : 259 iterations to match.
test[30] : 227 iterations to match.
test[31] : 11 iterations to match.
test[32] : 22 iterations to match.
test[33] : 187 iterations to match.
test[34] : 90 iterations to match.
test[35] : 5 iterations to match.
test[36] : 43 iterations to match.
test[37] : 114 iterations to match.
test[38] : 38 iterations to match.
test[39] : 24 iterations to match.
test[40] : 53 iterations to match.
test[41] : 71 iterations to match.
test[42] : 148 iterations to match.
test[43] : 61 iterations to match.
test[44] : 78 iterations to match.
test[45] : 5 iterations to match.
test[46] : 30 iterations to match.
test[47] : 281 iterations to match.
test[48] : 18 iterations to match.
test[49] : 109 iterations to match.
test[50] : 31 iterations to match.
Average number of iterations to find match: 94.28
仔细看看,如果您有其他问题,请告诉我。