Help with arrays

I am kinda new, and having problem in figuring out my mistake. I have to get random numbers 1-25 and my code is down, when i run it, it gives me error. Can anyone please tell me where i am mistaking so that i can fix it up. Thank you.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <ctime>

using namespace std;
void FillArray(int ar[][10], int size);
void OutputArray(int ar[][10], int size);

int main()
{
	int ar[9][10];
	FillArray(ar, 10);
	OutputArray(ar, 10);
	return 0;
}
void FillArray(int ar[][10], int size)
{
	const int COLS = 10;
	for (int row = 0; row < size; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			ar[row][col] = (rand() % 25 + 1);
		}
	}
}
void OutputArray(int ar[][10], int size)
{
	const int COLS = 10;
	for (int row = 0; row < size; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			cout << ar[row][col] << '\t';
		}
		cout << endl;
	}
}

Last edited on
Well the issue is that you are trying to read and write past the array, which is corrupting it. Looking inside int main where ar is declared we see that it is a 9x10 array. However, when you call functions FillArray and OutputArray you pass in 10 as the size. To which, the loop will cycle through the array as if it were a 10x10 array. To fix this simply change edit to arr to hold 10 rows. Or you could pass in 9 instead of 10 to functions FillArray and OutputArray.

Here is the fix:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <ctime>

using namespace std;
void FillArray(int ar[][10], int size);
void OutputArray(int ar[][10], int size);

int main()
{
	constexpr size_t rows = 10U;
	constexpr size_t columns = 10U;

	int arr[rows][columns];
	FillArray(arr, rows);
	OutputArray(arr, rows);

	cin.get();
	return 0;
}
void FillArray(int ar[][10], int size)
{
	const int COLS = 10;
	for (int row = 0; row < size; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			ar[row][col] = (rand() % 25 + 1);
		}
	}
}
void OutputArray(int ar[][10], int size)
{
	const int COLS = 10;
	for (int row = 0; row < size; row++)
	{
		for (int col = 0; col < COLS; col++)
		{
			cout << ar[row][col] << '\t';
		}
		cout << endl;
	}
}

I want 9 x 10 array. So, will it be fine if i change the 10 in my function to 9?
Yes, it will be fine so long as you don't read/write pass the array.
Last edited on
When i change my rows to [10] then it runs good but when i try to change it back to [9](that is what i want) it gives me debug error!
Last edited on
Did you make sure to pass in the correct size to the functions you are calling?

1
2
3
int arr[9][10];
FillArray(arr, 9);
OutputArray(arr, 9);


The numbers being passed into the functions should be less than or equal to the amount of rows in the array.
Oh!! Yes i got it thank you very much for bearing with me. Have a great day!!
Topic archived. No new replies allowed.