我试着编写一个返回像素颜色的随机数组的函数,所以当我调用时randomPalette(i),该函数将创建一个随机i颜色数组.以下是我的代码.它表示random[colors]该表达式的错误必须具有恒定值.我不知道为什么.怎么解决?
pixel* randomPalette(int colors){
pixel random[colors] ;
int i, x;
srand(time(NULL)); //generate a random seed
for (i = 0; i < colors; i++){
x = rand() % 256;
random[i].r = x; random[i].g = x; random[i].b = x;
}
return random;
}
Run Code Online (Sandbox Code Playgroud) 我写了一个简单的程序来生成素数.素数印得很好.我还尝试将每个素数放在一个向量中以便进一步处理,但不知何故,数字似乎没有进入(即push_back)向量,因为它打印出奇怪的数字而不是素数.简而言之,整个程序工作正常,只有向量有问题.请帮忙.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
const int NUM = 300;
int main()
{
int i, j ;
int counter = 0;
bool arr[NUM] = {false}; //false == 0
vector<int> aVector;
...
cout << "\nNumber of prime numbers is " << counter << endl;
for (j=0; j<aVector.size() ; j++)
{
cout << "aVector[" << j << "] is " << aVector[j] << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)