Assigning a 2d array random numbers

Hey guys I am stuck here with my code. I am trying to assign the temp array (represents the temperature 7 days a week 24 hours of the day) random temperatures. I thought I had it figured out but now i'm getting the error : Unhandled exception at 0x776d15fe in close lab 1.exe: 0xC0000005: Access violation. Any help is appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
	int temp[7][24];

	srand((unsigned)time(NULL));

	for (int i = 1; i < 7; i++)
{
	for (int j = 1; i < 24; j++)
	{
temp[i][j] = 20+ rand() % 100;
cout << temp[i][j] << endl;
	}
}
	
	system ("PAUSE");
	return 0;

}
Though indexes for array elements in C++ start from 0 I do not see any other problem with your code.

EDIT: I am sorry. You have a typo in the internal loop

for (int j = 1; i < 24; j++)

Chamge i < 24 to j < 24

And start indexing from 0.:)
Last edited on
vlad from moscow:
Though indexes for array elements in C++ start from 0 I do not see any other problem with your code.

+1

Moreover, your program runs fine on my end.
I feel stupid, thanks guys!
Topic archived. No new replies allowed.