I am trying to write a code that will generate 100000 random numbers in double type and assign them to an array. This should be done in a separate function to be called from main and I also cannot use any global variables. The function should be in the format of double createRandomNumbers()
. The array created however should be available to use from main function also. I have written the code below for my function double createRandomNumbers()
.
我正在尝试编写一个代码,它将生成100,000个双类型的随机数,并将它们分配给一个数组。这应该在一个单独的函数中完成,我也不能使用任何全局变量。该函数的格式应该是double createRandomNumbers()。不过,创建的数组也应该可以从main函数中使用。我已经为我的函数double createRandomNumbers()写了下面的代码。
` int main(void)
createRandomNumbers();
printf("%.2f",randomNumberset[1]);
return(0); `
` double createRandomNumbers()
{
double randomNumberset[100000] ;int i;
for(i=0;i<100000;i++)
{
randomNumberset[i]=(double)(10000*((float)rand()/(float)RAND_MAX));
}
}`
Arrays are pointers right? So in theory I should be able to use the array in the main without declaring it once more. Yet I get the error: [Error] 'randomNumberset' was not declared in this scope.
数组是指针对吧?所以理论上,我应该能够在main中使用数组而不用再声明它。然而,我得到了错误:[错误]“随机数集”没有在这个范围内声明。
I feel like I should be using a pointer somewhere but cannot really figure out where.
我觉得我应该在某个地方使用指针,但却不能真正弄清楚其中的位置。
1 个解决方案
#1
3
Solving under your given conditions is not possible.
在你的条件下解决是不可能的。
If you can't use globals, then to fill the array in the function and use it from main, you must do one of two things: (1) allocate the array in main, and pass it to the function to fill, or (2) allocate the array in the filling function and return it to main. There are no other options, and a function declared double createRandomNumbers()
does neither. For option 1, it should be void createRandomNumbers(double *buf)
; for option 2 it should be double * createRandomNumbers()
.
如果你不能使用全局变量,然后填充该数组的函数,从主要使用它,你必须做两件事:(1)在主要分配数组,并将其传递给函数,或(2)分配的数组填充并返回到主函数。没有其他选项,函数声明双createRandomNumbers()没有。对于选项1,它应该是void createRandomNumbers(double *buf);对于选项2,应该是double * createRandomNumbers()。
Personally, I would do something like this:
就我个人而言,我会这样做:
#include <stdio.h>
#include <time.h>
double *randomArray(int count) {
double *dp = malloc(count * sizeof(double));
if (dp) {
for (int i = 0; i < count; i += 1) {
dp[i] = ((double)rand()) / RAND_MAX;
}
}
return dp;
}
int main(int ac, char *av[]) {
srand(time(0));
double *dp = randomArray(100000);
if (dp) {
. . .
free(dp);
}
}
#1
3
Solving under your given conditions is not possible.
在你的条件下解决是不可能的。
If you can't use globals, then to fill the array in the function and use it from main, you must do one of two things: (1) allocate the array in main, and pass it to the function to fill, or (2) allocate the array in the filling function and return it to main. There are no other options, and a function declared double createRandomNumbers()
does neither. For option 1, it should be void createRandomNumbers(double *buf)
; for option 2 it should be double * createRandomNumbers()
.
如果你不能使用全局变量,然后填充该数组的函数,从主要使用它,你必须做两件事:(1)在主要分配数组,并将其传递给函数,或(2)分配的数组填充并返回到主函数。没有其他选项,函数声明双createRandomNumbers()没有。对于选项1,它应该是void createRandomNumbers(double *buf);对于选项2,应该是double * createRandomNumbers()。
Personally, I would do something like this:
就我个人而言,我会这样做:
#include <stdio.h>
#include <time.h>
double *randomArray(int count) {
double *dp = malloc(count * sizeof(double));
if (dp) {
for (int i = 0; i < count; i += 1) {
dp[i] = ((double)rand()) / RAND_MAX;
}
}
return dp;
}
int main(int ac, char *av[]) {
srand(time(0));
double *dp = randomArray(100000);
if (dp) {
. . .
free(dp);
}
}