c++ - Populating a vector with random numbers -


i'll dive right in: given piece of code professor that's supposed generate random numbers , compiler (g++) keeps throwing these errors: "warning: pointer function used in arithmetic [-wpointer-arith] rand[i]=((double) rand() / (static_cast(rand_max) + 1.0))* (high - low) + low;" "error: invalid cast type 'std::vector' type 'double' rand[i]=((double) rand() / (static_cast(rand_max) + 1.0))* (high - low) + low;" both point function generates random numbers. trouble i've used exact same function before , worked fine. have no idea wrong. appreciated. note still new c++.

i've included: cstdlib, stdio.h, cstdio, time.h, vector, iomanip, fstream, iostream, cmath. code have now:

int main() { int n=20000;  std::srand((unsigned)time(0));  for(int = 0; i<(n+1); ++i) {     double high = 1.0, low = 0.0;     rand[i]=((double) rand()/(static_cast<double>(rand_max) + 1.0))*(high - low) + low;     }  return 0; } 

you using name rand both array write , standard library function call. that's bad.

declare array other name, , write instead. eg:

int main() {   int n=20000;    std::srand((unsigned)time(0));   std::vector<double> a(n+1);    for(int = 0; i<(n+1); ++i) {     double high = 1.0, low = 0.0;     a[i]=((double) rand()/(static_cast<double>(rand_max) + 1.0))*(high - low) + low;   }    return 0; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -