0
I need help fixing a error
Every time I run this code it gives me a “timeout:the monitored command dumped core” error and I can’t find the issue. #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int b{}; int c{} ; int a[c]; cin >> b; c = (rand() % 4) ; srand(time(0)); a[c] = {b}; cout << a[c]; return 0; } https://code.sololearn.com/cxul741j3RwC/?ref=app
5 odpowiedzi
+ 2
Okay, save the code as new code bit, and share its link here so I can see the updated version. Leave the one in post Description as is, as a reference of where we started.
Here's how to share code link in case you didn't know how
https://www.sololearn.com/post/75089/?ref=app
+ 1
int c{};
This initializes <c> by zero
int a[ c ];
This creates an array, but size is zero
a[ c ] = { b };
This assigns { b } for element in array <a> at index <c>. But array <a> is zero size, you are pushing something into a flattened box? how would it work?
One more thing, for better result, seed the RNG before generating a random number, not after. Call srand() before calling rand()
+ 1
You need to specify a non zero value for <c>. Since your rand() call ranges from 0 ~ 3, you specify value of <c> around 4 or 5 to be safe.
int c{ 5 };
And move the line that calls srand() above the line that calls rand()
0
So what do i do to fix it
0
I did that but now it inly outputs the first number in the array