Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示用户创建的数组c++; 这可能是一个很简单的问题,我对C++很陌生,我很难理解大部分的事情。我被要求创建一个存储随机数的数组,但用户需要定义数组的大小。然后我必须显示数组中的所有数字。我要做的是显示数组的所有元素。目前,我很确定我已经把数组部分放下了,但我似乎只能显示一个数字。以下是我目前的代码: #include <iostream> #include <cstdlib> #include <stdlib.h> #include <ctime> using namespace std; using std::srand; using std::rand; using std::time; void Display(int *, int); int main(void) { int userSize; cout << "Please enter the size of the array: "; cin >> userSize; int* randArray = new int[userSize]; srand(time(NULL)); for (int i = 0; i < userSize; i++) { randArray[i] = rand() % 20 + 1; } //Disregard the next few lines, I was just testing to see if anything was actually in the array. /*cout << randArray[0] << endl; cout << randArray[1] << endl; cout << randArray[2] << endl; cout << randArray[19] << endl;*/ Display(randArray, sizeof(randArray) / sizeof(int)); return 0; delete[] randArray; } void Display(int *arrayData, int numElements) { for (int counter = 0; counter < numElements; counter++) { std::cout << *(arrayData + counter) << std::endl; } return; } #包括 #包括 #包括 #包括 使用名称空间std; 使用std::srand; 使用std::rand; 使用std::时间; 无效显示(int*,int); 内部主(空) { int用户大小; cout>userSize; int*randArray=newint[userSize]; srand(时间(空)); for(int i=0;i(除非您需要使用这个分配的原始指针) SIZEOF(RANDARED)不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因此sizeof(randArray)/sizeof(int)始终返回1。请使用userSize作为Display函数调用中的第二个参数_C++_Arrays - Fatal编程技术网 userSize; int*randArray=newint[userSize]; srand(时间(空)); for(int i=0;i(除非您需要使用这个分配的原始指针) SIZEOF(RANDARED)不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因此sizeof(randArray)/sizeof(int)始终返回1。请使用userSize作为Display函数调用中的第二个参数,c++,arrays,C++,Arrays" /> userSize; int*randArray=newint[userSize]; srand(时间(空)); for(int i=0;i(除非您需要使用这个分配的原始指针) SIZEOF(RANDARED)不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因此sizeof(randArray)/sizeof(int)始终返回1。请使用userSize作为Display函数调用中的第二个参数,c++,arrays,C++,Arrays" />

显示用户创建的数组c++; 这可能是一个很简单的问题,我对C++很陌生,我很难理解大部分的事情。我被要求创建一个存储随机数的数组,但用户需要定义数组的大小。然后我必须显示数组中的所有数字。我要做的是显示数组的所有元素。目前,我很确定我已经把数组部分放下了,但我似乎只能显示一个数字。以下是我目前的代码: #include <iostream> #include <cstdlib> #include <stdlib.h> #include <ctime> using namespace std; using std::srand; using std::rand; using std::time; void Display(int *, int); int main(void) { int userSize; cout << "Please enter the size of the array: "; cin >> userSize; int* randArray = new int[userSize]; srand(time(NULL)); for (int i = 0; i < userSize; i++) { randArray[i] = rand() % 20 + 1; } //Disregard the next few lines, I was just testing to see if anything was actually in the array. /*cout << randArray[0] << endl; cout << randArray[1] << endl; cout << randArray[2] << endl; cout << randArray[19] << endl;*/ Display(randArray, sizeof(randArray) / sizeof(int)); return 0; delete[] randArray; } void Display(int *arrayData, int numElements) { for (int counter = 0; counter < numElements; counter++) { std::cout << *(arrayData + counter) << std::endl; } return; } #包括 #包括 #包括 #包括 使用名称空间std; 使用std::srand; 使用std::rand; 使用std::时间; 无效显示(int*,int); 内部主(空) { int用户大小; cout>userSize; int*randArray=newint[userSize]; srand(时间(空)); for(int i=0;i(除非您需要使用这个分配的原始指针) SIZEOF(RANDARED)不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因此sizeof(randArray)/sizeof(int)始终返回1。请使用userSize作为Display函数调用中的第二个参数

显示用户创建的数组c++; 这可能是一个很简单的问题,我对C++很陌生,我很难理解大部分的事情。我被要求创建一个存储随机数的数组,但用户需要定义数组的大小。然后我必须显示数组中的所有数字。我要做的是显示数组的所有元素。目前,我很确定我已经把数组部分放下了,但我似乎只能显示一个数字。以下是我目前的代码: #include <iostream> #include <cstdlib> #include <stdlib.h> #include <ctime> using namespace std; using std::srand; using std::rand; using std::time; void Display(int *, int); int main(void) { int userSize; cout << "Please enter the size of the array: "; cin >> userSize; int* randArray = new int[userSize]; srand(time(NULL)); for (int i = 0; i < userSize; i++) { randArray[i] = rand() % 20 + 1; } //Disregard the next few lines, I was just testing to see if anything was actually in the array. /*cout << randArray[0] << endl; cout << randArray[1] << endl; cout << randArray[2] << endl; cout << randArray[19] << endl;*/ Display(randArray, sizeof(randArray) / sizeof(int)); return 0; delete[] randArray; } void Display(int *arrayData, int numElements) { for (int counter = 0; counter < numElements; counter++) { std::cout << *(arrayData + counter) << std::endl; } return; } #包括 #包括 #包括 #包括 使用名称空间std; 使用std::srand; 使用std::rand; 使用std::时间; 无效显示(int*,int); 内部主(空) { int用户大小; cout>userSize; int*randArray=newint[userSize]; srand(时间(空)); for(int i=0;i(除非您需要使用这个分配的原始指针) SIZEOF(RANDARED)不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因此sizeof(randArray)/sizeof(int)始终返回1。请使用userSize作为Display函数调用中的第二个参数,c++,arrays,C++,Arrays,问题是sizeof。它提供参数的类型的大小,而不是后面的大小。您应该将userSize传递给Display() 您还应该在return之前delete数组。return后面的代码永远不会执行。问题是sizeof的大小。它给您参数的类型的大小,而不是后面的大小。您应该将userSize传递给Display() 您还应该在返回之前删除数组。返回后的代码将永远不会执行。sizeof(randArray)不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因

问题是
sizeof
。它提供参数的类型的大小,而不是后面的大小。您应该将
userSize
传递给
Display()


您还应该在
return
之前
delete
数组。
return
后面的代码永远不会执行。

问题是
sizeof
的大小。它给您参数的类型的大小,而不是后面的大小。您应该将
userSize
传递给
Display()

您还应该在
返回之前
删除
数组。
返回后的代码将永远不会执行。

sizeof(randArray)
不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因此
sizeof(randArray)/sizeof(int)
始终返回1。请使用
userSize
作为
Display
函数调用中的第二个参数

另外,您在
返回0
之后删除[]randArray
。这是不正确的;在
返回0
之后不会执行任何操作。您希望改为在上面执行

进一步考虑使用<代码> STD::vector < /COD>(除非您需要使用这个分配的原始指针)

<代码> SIZEOF(RANDARED)

不会告诉您分配的字节数。相反,它会告诉您指针的大小,在您的系统上,指针的大小恰好与整数的大小相同,因此
sizeof(randArray)/sizeof(int)
始终返回1。请使用
userSize
作为
Display
函数调用中的第二个参数

另外,您在
返回0
之后删除[]randArray
。这是不正确的;在
返回0
之后不会执行任何操作。您希望改为在上面执行


进一步考虑使用<代码> STD::vector < /COD>(除非您需要为这个赋值使用原始指针)

什么意思?数组有用户选择的任意多个元素。^对,如果用户输入10个元素,那么数组中就有10个元素。你在问什么?
randary
是指针,所以
sizeof(randary)是一个指针的大小…抱歉,我对C++的理解不足,影响了我解释事物的方式。我的意思是,用户首先得到提示来选择数组的大小。因此如果用户选择了20,那么数组由20个“框”组成,每个都填充了1-20之间的随机数。更清楚。你的意思是什么?数组有用户选择的任意多的元素。^对,如果用户输入10,那么数组中就有10个元素。你在问什么?
randary
是指针,所以
sizeof(randary)是一个指针的大小…抱歉,我对C++的理解不足,影响了我解释事物的方式。我的意思是,用户首先得到提示来选择数组的大小。因此如果用户选择了20,那么数组由20个“框”组成,每个都填充了1-20之间的随机数。更清楚一些。我需要显示数组的所有元素。因此,假设用户输入20,那么数组中就有20个“框”,其中包含一个随机数(介于1-20之间)。当它应该显示所有20个时,我似乎只能显示一个数字。显然,这与我调用display()的方式有关。我还将用确切的问题更新OP。此外,我认为我不允许使用
std:vector
。正如我在回答中提到的,您调用的
Display
不正确。您要做的是
Display(随机数组,用户大小)
.ahhh我知道我现在做错了什么。非常感谢,当我看到全部20个数字显示时,你不知道这有多轻松。我需要显示数组的所有元素。因此,假设用户输入20,那么数组中就有20个带有随机数的“框”(在1-20之间)。我似乎只能显示一个数字,而它应该显示全部20个。显然,这与我如何调用display()有关。我还将用确切的问题更新OP。此外,我认为我不允许使用
std:vector
。正如我在回答中提到的,您调用的
Display
不正确。您要做的是
Display(随机数组,用户大小)
。啊,我明白我现在做错了什么。非常感谢,当我看到所有20个数字都显示出来时,你不知道这有多轻松。