C++: load random numbers into an array with a for loop?
hi,
i want to load random integers into an array. i was thinking of using a for loop to increment through the array and generate a random number for each element.
but, i don't know how i'd go about doing that thought....
Quote:Original post by Antheus
You start by mentioning which language you're using...
lol, sorry, i was just about to edit the topic to C++ :P
Quote:Original post by laserbeak43
hi,
i want to load random integers into an array. i was thinking of using a for loop to increment through the array and generate a random number for each element.
but, i don't know how i'd go about doing that thought....
What are you currently able to do?
Can use make an array? Can you use a for loop to visit each element? Can you set each element to a constant? Can you create a single random number?
Have you any code written yet?
so far, i have this
-edit-
it compiles fine, just nothing is shown in the console window.
[source = 'c++']#include <iostream>#include <stdlib.h>using namespace std;int main(){ int xInc; //gonna try to load this into x[] float x[511]; //The signal is held in x[0] to x[511] string meanStr; //display the result string SdStr; //display the result(standard deviant) n = 512; //we'll start with a constant value like the book //call mythical subroutine here for(i = 0; i > 12; i++) { xInc = rand(); x[xInc++]; cout << x << endl; } return 0;}
-edit-
it compiles fine, just nothing is shown in the console window.
Quote:Original post by rip-off
What are you currently able to do?
Can use make an array?
yes.
Quote:Can you use a for loop to visit each element?
like this?
int x[5];for(i = 0; i>5; i++){ cout << x;}
Quote:Can you set each element to a constant?
would that be:
int x[5];for(i = 0; i>5; i++){ const myNum; x = myNum; cout << x;}
Quote:Can you create a single random number?
not exactly sure if i'm doing that part right...
Quote:Have you any code written yet?
yes.
As Sicrane said...
std::generate(x, x+length, std::rand);std::copy(x, x+length, std::ostream_iterator<int>(std::cout, " "));
Quote:
float x[511]; //The signal is held in x[0] to x[511]
I think you want float x[512] here.
You're never assigning values to x, and your for loop condition was wrong.
for(i = 0; i < 12; i++) { x = rand(); cout << x << endl; }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement