PDA

View Full Version : error with array



cooka
16th May 2017, 16:11
Hi guys so this part of code is to display 2d array . The first 2 arrays are working great but the last one which is the one post is not working and I can't find out why !! I will post the output also so u can see the problem . thx for ur help .




int V [][12] = {0};

int colm = 5 , row = 12;

cout<<" V area is :\n"
<<"----------------\n\n";

for( int i = 0; i < colm ; ++i ) {

for (int k = 0 ; k < row ; ++k) {

cout<< V [5][12];
}
cout<<endl;
}
cout<<"\n";
}



V Area :
------------------
151515151515151515151515
151515151515151515151515
151515151515151515151515
151515151515151515151515
151515151515151515151515

scgrant327
16th May 2017, 16:15
cout<< Parking_S [5][12];

should be:

cout<< Parking_S [i][k];

cooka
16th May 2017, 16:22
I only got the first line of "0" then another weird output then last line "o" again??

high_flyer
16th May 2017, 16:57
you are not initializing the whole array properly, you initialized only one dimension, the other dimension has garbage in it, which is what you see in the output.


const int colm = 5 , row = 12;
int Parking_S [colm][row] = {0};

cooka
16th May 2017, 17:35
not working its says undeclared row and colm . I don't understand the first 2 are written just same way and there are working . And I noticed something If I take only this piece of code to another c++ file and I compile it work great u see ??

Added after 20 minutes:

I solved it I had to use const int row and column and cout<< Parking_B [i][k] like u said

high_flyer
16th May 2017, 18:16
I had to use const int row and column
ah yes, missed that one, sorry.

cooka
16th May 2017, 19:24
NO prob dude it happens.