gpt4 book ai didi

c - 在c中生成随机数并将其存储在数组中

转载 作者:行者123 更新时间:2023-11-30 14:59:06 25 4
gpt4 key购买 nike

我必须使用一个函数来定义随机数,创建一个包含 25 个随机数的数组,但始终让它一次只显示一个数字(而不是逐个单元格地显示所有数字),或者简单地显示不正确,即 0 .这是我到目前为止所拥有的。

编辑代码已更改以纠正愚蠢的错误,我错过了冲刺,但仍然不确定如何调用 funct,因为我收到“funct 'get_value'的参数太少,抱歉,如果这看起来微不足道,但我是对编码非常陌生,感谢您抽出时间。

int get_value (int t);
int main()
{
srand(time(NULL));

int temp[25], n;
for(n=0; n<25; n++)
{temp[n] = rand() %(40)+60;
printf("value of %d at cell %d \n", n, temp[n]);}
return 0;
}

//function get_value()
//needs to return rand # <-> 60 and 100 seed rand
//use rand %40+60 to ensure value is <-> 60 and 100

int get_value (int t)
{
return rand() %(40)+60;
}

最佳答案

您有一些语法错误
for循环应该是这样的

for(n=0; n<25; n++)
{
temp[n] = get_value(n); //in your prog u have written temp[t], t isnt defined
printf("value at cell %d is %d \n", n, temp[n]);
} // you also missed the braces

关于c - 在c中生成随机数并将其存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43005924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com