I'm learning C++ for my test where I learn java before, and now there's a lot of confusion. I'm trying to print 2D array of boolean, returning # if true, and ? if false. I've tried initializing the array first, which I think works, and I can print the value in certain array location if I don't use the iterator (printGrid). below is my code
#include<iostream>
void printGrid(bool a[][10]){ // why this method doesn't work?
for(int i = 0; i < sizeof(a)/ (sizeof(*a)) ; i++){
for(int j = 0; j < sizeof(a)/ (sizeof(*a)) ; j++) {
char x = a[i][j] ? '#' : '?'; // i've using the conditional in my std::cout before, still doesn't work
std::cout << x;
}
std::cout<< std::endl;
}
}
void main(){
bool grid[10][10];
for(int i = 0; i < sizeof(grid)/sizeof(*grid) ; i++){
for(int j = 0; j < sizeof(grid)/sizeof(*grid); j++){
grid[i][j] = 1;
}
}
printGrid(grid); // this doesn't work
std::cout<<( (grid[0][1]) ? "#" : "?" ) << std::endl; // this works fine
getchar();
} // main
where do I got my printGrid wrong? i've tried using a 2D array of INT instead with the same setup, with function outside main etc, and it worked just fine. any idea?
edit: thanks a lot for the answers. my question hasn't been answered though, what is wrong with my function? because i've tried using a method with the same setup, just different parameter, and it worked fine
void printint(int a[][10]){ // why does this work, and the above doesn't?
for(int i = 0; i < 10 ; i++){
for(int j = 0; j < 10 ; j++) {
std::cout <<( (a[i][j] == 10) ? "a" : "b");
}
std::cout << std::endl;
}
}
when I use printint to print my 2D array of int, it worked just fine?