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

27th Nov 2022, 2:30 AM
Shabad Randhawa
Shabad Randhawa - avatar
5 Answers
+ 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
27th Nov 2022, 6:56 AM
Ipang
+ 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()
27th Nov 2022, 3:15 AM
Ipang
+ 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()
27th Nov 2022, 3:43 AM
Ipang
0
So what do i do to fix it
27th Nov 2022, 3:19 AM
Shabad Randhawa
Shabad Randhawa - avatar
0
I did that but now it inly outputs the first number in the array
27th Nov 2022, 3:45 AM
Shabad Randhawa
Shabad Randhawa - avatar