943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1020
  • C++ RSS
Feb 11th, 2008
0

Trying to create a 2D array

Expand Post »
im trying to create a 2D array using a random generator but the compiler keeps giving me compiling error no matter how i modify the calling function

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. const int MAX = 4;
  7.  
  8. void constructArray (int a [][MAX], int);
  9.  
  10.  
  11. int main()
  12. {
  13. int a [MAX];
  14.  
  15. srand (time (NULL));
  16.  
  17. constructArray (a [][MAX], MAX);
  18.  
  19.  
  20.  
  21. }
  22.  
  23. void constructArray (int a [][], int size)
  24. {
  25. for (int i = 0; i < size; i++)
  26. a [i][size] = rand () % 10;
  27. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Feb 11th, 2008
0

Re: Trying to create a 2D array

As it is only 3 lines of code do you need a function?

C++ Syntax (Toggle Plain Text)
  1. const int MAXROW = 4;
  2. const int MAXCOL = 10;
  3.  
  4. int main()
  5. {
  6. int a [MAXROW][MAXCOL];
  7. for (int row = 0; row < MAXROW; row++)
  8. for (int col = 0; col < MAXCOL; col++)
  9. a[row][col] = rand () % 10;
  10. }
Reputation Points: 36
Solved Threads: 0
Light Poster
Passmark is offline Offline
32 posts
since Jan 2008
Feb 11th, 2008
0

Re: Trying to create a 2D array

And array cannot contain 2 empty dimensions in a function. You must fill in at least the left dimension.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Feb 11th, 2008
0

Re: Trying to create a 2D array

oh i used a function coz the array would be part of a larger program....

so...if i wanna print the array on screen now...i added in a few more lines....but the compiler gives me an error msg at line 21....
whats wrong there? i filled up both dimensions of the array, why cant it work?

#include <iostream>
using namespace std;

const int MAXROW = 4;
const int MAXCOL = 10;



int main()
{
  srand(time(NULL));
  
  int a [MAXROW][MAXCOL];
  
  for (int row = 0; row < MAXROW; row++)
  {  
    for (int col = 0; col < MAXCOL; col++)
      {
             a[row][col] = rand () % 10;
      }
  cout << a[row][col] << "\t";
  cout << endl;
  }    
  

}
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Feb 11th, 2008
0

Re: Trying to create a 2D array

Click to Expand / Collapse  Quote originally posted by number87 ...
gives me an error msg at line 21....
You've declared 'col' inside the inner for loop. That means that when the loop is done, col is outside of scope.
To solve this problem you would have declare 'int col' in the same scope as where you use it. So right after this line : int a [MAXROW][MAXCOL]; for example. Just put a int col = 0; after it, and remove the 'int' delcaration from the for loop
:
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4.  
  5. const int MAXROW = 4;
  6. const int MAXCOL = 10;
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12. srand(time(NULL));
  13.  
  14. int a [MAXROW][MAXCOL];
  15. int col=0 ,row =0;
  16.  
  17. for (row = 0; row < MAXROW; row++)
  18. {
  19. for (col = 0; col < MAXCOL; col++)
  20. {
  21. a[row][col] = rand () % 10;
  22. }
  23. cout << a[row][col] << "\t";
  24. cout << endl;
  25. }
  26.  
  27. return 0;
  28. }

Please note the return 0; at the end of the program.
I didn't check your logic, so the program may not give you the correct output.
Last edited by Nick Evan; Mar 26th, 2010 at 9:00 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 11th, 2008
0

Re: Trying to create a 2D array

If you want to display the entire 'matrix' you might want to move line 23: cout << a[row][col] << "\t"; inside thee 'for'loop. (on line 21)
Last edited by Nick Evan; Mar 26th, 2010 at 9:00 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ Express Questions
Next Thread in C++ Forum Timeline: folderBrowserDialog1





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC