| | |
Trying to create a 2D array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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)
#include <iostream> #include <ctime> using namespace std; const int MAX = 4; void constructArray (int a [][MAX], int); int main() { int a [MAX]; srand (time (NULL)); constructArray (a [][MAX], MAX); } void constructArray (int a [][], int size) { for (int i = 0; i < size; i++) a [i][size] = rand () % 10; }
As it is only 3 lines of code do you need a function?
C++ Syntax (Toggle Plain Text)
const int MAXROW = 4; const int MAXCOL = 10; int main() { int a [MAXROW][MAXCOL]; for (int row = 0; row < MAXROW; row++) for (int col = 0; col < MAXCOL; col++) a[row][col] = rand () % 10; }
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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?
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;
}
} 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 :
:
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
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:
c Syntax (Toggle Plain Text)
#include <iostream> #include <time.h> using namespace std; const int MAXROW = 4; const int MAXCOL = 10; int main() { srand(time(NULL)); int a [MAXROW][MAXCOL]; int col=0 ,row =0; for (row = 0; row < MAXROW; row++) { for (col = 0; col < MAXCOL; col++) { a[row][col] = rand () % 10; } cout << a[row][col] << "\t"; cout << endl; } return 0; }
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.
![]() |
Similar Threads
- C++ complete binary tree using an array. Unexpected end file (C++)
- array program, dont know where to even begin! (C)
- array question (C++)
- Problem in finding out an array (Java)
- loop to create arrays when reading a file (Java)
- Array without twice the same number? (C)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- array of files in C (C)
Other Threads in the C++ Forum
- Previous Thread: Visual C++ Express Questions
- Next Thread: folderBrowserDialog1
Views: 876 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






