我必须使用一个函数来创建一个包含25个随机数的数组来定义随机数,但是要让它一次只显示一个数字(而不是逐个单元地显示所有数字),或者只是显示不正确的数字,即0这是我到目前为止所做的。
编辑代码更改为纠正愚蠢的错误我错过了冲动,但仍然不确定如何调用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;
}
答案 0 :(得分:1)
您有一些语法错误 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
答案 1 :(得分:0)
答案 2 :(得分:0)
//I think here's what you want to do.
#include <stdio.h>
#include <time.h>
int main()
{
srand(time(NULL));
int temp, i, j, h;
int num_array[40];
int i_need_25[25];
//here's how im getting random numbers without repeating a number.
//first, fill the array with numbers between
for(i = 0; i < 41; i++)
{
num_array[i] = i + 60;
}
//then here's how we shuffle it
for(i = 0; i < 41; i++)
{
j = rand() % 41;
temp = num_array[j];
num_array[j] = num_array[i];
num_array[i] = temp;
}
//final process is to print the first 25 elements as you have said you need 25.
for(i = 0; i < 25; i++)
{
printf("%d ", num_array[i]);
}
printf("\n\n");
//or you can also store the first 25 elements on separate array variable.
for(i = 0; i < 25; i++)
{
i_need_25[i] = num_array[i];
printf("%d ", i_need_25[i]);
}
return 0;
}