Trying to create a 2D array

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Trying to create a 2D array

 
0
  #1
Feb 11th, 2008
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 31
Reputation: Passmark is an unknown quantity at this point 
Solved Threads: 0
Passmark's Avatar
Passmark Passmark is offline Offline
Light Poster

Re: Trying to create a 2D array

 
0
  #2
Feb 11th, 2008
As it is only 3 lines of code do you need a function?

  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Trying to create a 2D array

 
0
  #3
Feb 11th, 2008
And array cannot contain 2 empty dimensions in a function. You must fill in at least the left dimension.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Re: Trying to create a 2D array

 
0
  #4
Feb 11th, 2008
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;
  }    
  

}
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,989
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Trying to create a 2D array

 
0
  #5
Feb 11th, 2008
Originally Posted by number87 View Post
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.

Niek
Last edited by niek_e; Feb 11th, 2008 at 8:16 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,989
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Trying to create a 2D array

 
0
  #6
Feb 11th, 2008
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)

Niek
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 876 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC