-
Populating an Array
Code:
int main()
{
const double array[5]={};
sumValues(array,5);
int number,value;
for(int i=0;i<number;i++){
value = (rand()%100)+1;
value = array[i];
cout<<value<<endl;
}
return 0;
}
Hi all. I am trying to populate an array using random numbers; the above code has no errors, but when compiling it does not output any numbers on the screen.
Have I gone wrong somewhere? Or am I on the right track?
I would like to thank you in advance for any advice or help given. Really appreciated.
-
You are not initializing "numbers" before using it.
I assume you're initializing "array[5]" in the "sumValues()" function.
[edit]
This code seems wrong. You are overwriting the random number placed in "value" immediately after with a value from "array." Also, if "number" is greater than 5, you will overrun your "array" array.
-
In the following code:
Code:
for(int i=0;i<number;i++){
value = (rand()%100)+1;
value = array[i];
cout<<value<<endl;
}
You are not actually ever using your random number. You put your random number into the variable value then immediately replace that number with whatever array[i] contains. What does array[i] contain?
Jim
-
How many random numbers are there supposed to be?
Do you know that the = symbol in C++ only copies the thing from the right hand side into the left hand side?
You should post the sumValues function as well, because from what we can see here, it looks like there will be some issues with it too.