你可以帮我解决用随机数填充5个圆圈阵列的问题.随机数将是圆的半径.这是我的代码:
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
// Array 2, below section is to populate the array with random radius
float CircleArrayTwo [5]; // store the numbers
const int NUM = 5; // Display 5 random numbers
srand(time(NULL)); // seed the generator
for(int i = 0; i < NUM; ++i)
{
CircleArrayTwo[i] = rand()%10;
}
cout << "Below is the radius each of the five circles in the second array. " << endl;
cout << CircleArrayTwo << endl;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
目前输出如下:
下面是第二个数组中五个圆圈的半径.002CF878
我哪里错了?
任何帮助深表感谢
您正在打印数组的第一个元素的地址.您可以遍历数组并打印每个元素:
for(int i = 0; i < NUM; ++i)
{
std::cout << CircleArrayTwo[i] << ", ";
}
std::cout << "\n";
Run Code Online (Sandbox Code Playgroud)
或者,如果您有C++ 11支持,
for (auto& x : CircleArrayTwo) {
std::cout << x << ", ";
}
std::cout << "\n";
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2044 次 |
最近记录: |