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?

edit...

such as myArray(int[][] a)?

#include<iostream>

using namespace std;

void printarray(int);

int main()
{
    int num;
    cout<<"enter a number\n";
    cin>>num;
    printarray(num);
    return 0;
}

void printarray(int n)
{
    int array[n][n];

    for(int x=0, i=1; x<n; x++, i++)
    {
        for(int y=0; y<n; y++)
        {
            array[x][y]=i;
        }
    }

    for(int y=0; y<n; y++)
    {
        for(int x=0; x<n; x++)
        {
            cout<<array[x][y];
        }
        cout<<"\n";
    }
}

That should work...

> int a[sizex][sizex];
The first problem is standard C++ doesn't allow variable sized arrays.
Though some compilers offer it as an extension.

> for(int i=1; i<sizex; i++)
The second problem is that array subscripts start at 0, not 1.

Yeah, sturm it still does not work. I am pretty sure I have tried a setup earlier almost exactly like that and got the same results.

Salem is right. I think the problem could be solved by using vectors. I have not studied those yet, but i think that is what they do.

um on my compiler it does...I am using g++.... What compiler are you using?

I'm using VC++ express edition and I get the following errors:

1>------ Build started: Project: TestProj, Configuration: Debug Win32 ------
1>Compiling...
1>source.cpp
1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2057: expected constant expression
1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2466: cannot allocate an array of constant size 0
1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2057: expected constant expression
1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2466: cannot allocate an array of constant size 0
1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2087: 'array' : missing subscript
1>d:\documents and settings\owner\my documents\visual studio 2005\projects\testprojexe\testproj\source.cpp(14) : error C2133: 'array' : unknown size

Well, you can use a variable for array size, but it has to be a const value, I believe...
On the other hand, you can do dynamically sized arrays like this:

main(){
int **myArray; // a run-time sized 2d has trickier initialization syntax
int c, r; // number of columns and rows for our array
 
cout << "Simple Two Dimensional Array Demo" << endl;
cout << "Enter number of columns:"; cin >> c;
cout << "Enter number of rows:"; cin >> r; cout << endl;
 
// initialize a run-time sized two dimensional array
myArray = new int*[r]; // create first dimension (of pointers to 2nd dimension)
for (int i=0; i<r; i++){
myArray[i] = new int[c]; // create second dimension(s) 
}
 
// assign test values
for(int i=0; i<r; i++)
for(int j=0; j<c; j++)
myArray[i][j] = i*j;
 
print2dArray(myArray, c, r);
}

Hope that helps!

commented: code poorly indented and somewhat contrived, freeing of memory omitted? -2

Thanks geek! I just figured out the way to do it ( with the utilization of the modifiers new and delete to access free store) and was heading here to see if anyone posted that.

You beat me to it, but thanks again!

> um on my compiler it does...I am using g++.... What compiler are you using?
Like I said, some compilers allow it as an extension.

Compiling your example with various flags.

$ g++ foo.cpp
$ g++ -ansi -pedantic foo.cpp
foo.cpp: In function `void printarray(int)':
foo.cpp:18: error: ISO C++ forbids variable-size array `array'
foo.cpp:18: error: ISO C++ forbids variable-size array `array'

Salem I am using the Visual C++ 2005 Express Edition compiler and it forbids the creating of variable sized arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.