multi-dimension array not working

Thank you, human

#include <iostream>
#include <string>

using namespace std;

int main()
{
const int numItems = 5;
int arr1[numItems];
int arr2[numItems];

for (int i = 0; i < numItems; ++i)
{
for (int j = 0; j < numItems; ++j)
cout << i * j << " ";
cout << endl;
}


system("pause");
return 0;

}
Last edited on
This -> for (int i = 0; i < arr1; ++i) should be -> for (int i = 0; i < numItems; ++i)

Also, this -> for (int j = 0; j < arr2; ++j) should be -> for (int j = 0; j < numItems; ++j)
I don't get that error but anyway... I found several things...

Firstly you haven't given the array elements any values - though minor for an example.

I think for (int i = 0; i < arr1; ++i) and for (int j = 0; j < arr2; ++j) is ment to be for (unsigned int i = 0; i < sizeof(arr1)/sizeof(int); ++i) and for (unsigned int j = 0; j < sizeof(arr2)/sizeof(int); ++j). This gets the number of elements in the array, where as before it was returning the memory address of the array (starting location). I gather not the desired result? This would have been the likely cause of the error you stated.

And finally, int arr3[numItems][numItems]; in the above example is not used, though minor. :)

Happy Coding... :P

EDIT: m4ster r0shi's edit is also valid, but it's personal preference to how you do it.
Last edited on
Awesome! Thank you for your time, it works now. Ive updated my original post to the correct code. Also, my code is outputted as so:
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16

Id like it to output like this (without the unnecessary zeros):
0 1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
Try cout << (i+1) * (j+1) << " ";
Topic archived. No new replies allowed.