User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,557 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,680 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 5630 | Replies: 11
Reply
Join Date: Jun 2006
Posts: 29
Reputation: Woobag is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Woobag Woobag is offline Offline
Light Poster

User define 2D array

  #1  
Jun 1st, 2007
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation: Sturm is on a distinguished road 
Rep Power: 4
Solved Threads: 23
Sturm's Avatar
Sturm Sturm is offline Offline
Veteran Poster

Re: User define 2D array

  #2  
Jun 1st, 2007
edit...
Last edited by Sturm : Jun 1st, 2007 at 4:58 pm.
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Jun 2006
Posts: 29
Reputation: Woobag is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Woobag Woobag is offline Offline
Light Poster

Re: User define 2D array

  #3  
Jun 1st, 2007
such as myArray(int[][] a)?
Reply With Quote  
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation: Sturm is on a distinguished road 
Rep Power: 4
Solved Threads: 23
Sturm's Avatar
Sturm Sturm is offline Offline
Veteran Poster

Re: User define 2D array

  #4  
Jun 1st, 2007
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. void printarray(int);
  6.  
  7. int main()
  8. {
  9. int num;
  10. cout<<"enter a number\n";
  11. cin>>num;
  12. printarray(num);
  13. return 0;
  14. }
  15.  
  16. void printarray(int n)
  17. {
  18. int array[n][n];
  19.  
  20. for(int x=0, i=1; x<n; x++, i++)
  21. {
  22. for(int y=0; y<n; y++)
  23. {
  24. array[x][y]=i;
  25. }
  26. }
  27.  
  28. for(int y=0; y<n; y++)
  29. {
  30. for(int x=0; x<n; x++)
  31. {
  32. cout<<array[x][y];
  33. }
  34. cout<<"\n";
  35. }
  36. }
That should work...
Last edited by Sturm : Jun 1st, 2007 at 5:20 pm.
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Dec 2005
Posts: 3,257
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: User define 2D array

  #5  
Jun 1st, 2007
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Jun 2006
Posts: 29
Reputation: Woobag is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Woobag Woobag is offline Offline
Light Poster

Re: User define 2D array

  #6  
Jun 1st, 2007
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.
Reply With Quote  
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation: Sturm is on a distinguished road 
Rep Power: 4
Solved Threads: 23
Sturm's Avatar
Sturm Sturm is offline Offline
Veteran Poster

Re: User define 2D array

  #7  
Jun 1st, 2007
um on my compiler it does...I am using g++.... What compiler are you using?
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Jun 2006
Posts: 29
Reputation: Woobag is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Woobag Woobag is offline Offline
Light Poster

Re: User define 2D array

  #8  
Jun 1st, 2007
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
Reply With Quote  
Join Date: Mar 2007
Location: Aptos, CA
Posts: 118
Reputation: ft3ssgeek is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: User define 2D array

  #9  
Jun 1st, 2007
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:

  1. main(){
  2. int **myArray; // a run-time sized 2d has trickier initialization syntax
  3. int c, r; // number of columns and rows for our array
  4.  
  5. cout << "Simple Two Dimensional Array Demo" << endl;
  6. cout << "Enter number of columns:"; cin >> c;
  7. cout << "Enter number of rows:"; cin >> r; cout << endl;
  8.  
  9. // initialize a run-time sized two dimensional array
  10. myArray = new int*[r]; // create first dimension (of pointers to 2nd dimension)
  11. for (int i=0; i<r; i++){
  12. myArray[i] = new int[c]; // create second dimension(s)
  13. }
  14.  
  15. // assign test values
  16. for(int i=0; i<r; i++)
  17. for(int j=0; j<c; j++)
  18. myArray[i][j] = i*j;
  19.  
  20. print2dArray(myArray, c, r);
  21. }
Hope that helps!
Last edited by ft3ssgeek : Jun 1st, 2007 at 10:24 pm. Reason: Forgot code tags...
Reply With Quote  
Join Date: Jun 2006
Posts: 29
Reputation: Woobag is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Woobag Woobag is offline Offline
Light Poster

Re: User define 2D array

  #10  
Jun 1st, 2007
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC