I'm trying to write a program where the user gives a size of an array. myArray(5) (below) fills and prints a 5x5 array with numbers 1-5 and prints out 5 numbers on a line.
The output would look something like this...
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
The code of the program is as follows...
#include <iostream>
using namespace std;
int main()
{
int size=1;
cout << "enter an array size: ";
cin >> size; cout << '\n';
myArray(size);
// disregard next two lines: stops program from flashing closed
int c=9;
cin>>c;
}
void myArray(int sizex)
{
// fill array
int a[sizex][sizex];
for(int i=1; i<sizex; i++)
{
for(int j=1; j<sizex; j++)
{
if(j%1==0)
a[i][j] = i;
else
a[i][j] = j;
}
}
// print array
int n=1;
for(int i=1; i<6; i++)
{
for(int j=1; j<6; j++)
{
cout << a[i][j];
if((n % 5) == 0 )
cout << "\n";
else
cout << ' ';
n++;
}
}
}
The compiler keeps giving me errors saying that it cant make an array with a size of 0, which makes sense; however, the array should not be initialized till a user enters a value.
I am sure this is some simple fix, but I am just not experienced enough to figure this one out. Can someone help me please?