array help

• 02-07-2006
manoncampus
array help
I'm writing a program with a two dimmensional array and it goes up to [8][8](81 variables). For one part I need to check weather any of the variables are equal to 0 and instead of checking each one seperately(which would take forever) I need to know if theres a way to check the whole array.
Thanks for any help. :)
• 02-07-2006
Daved
Just use a nested loop. This won't take very long to code (certainly not forever) and it won't take long to run (certainly not forever). Each loop could run from 0 to less than 9.

Also don't forget that if you declare your array like this:
Code:

`int myarray[8][8];`
then it only has 64 spots, not 81. I assume you are using:
Code:

`int myarray[9][9];`
• 02-07-2006
manoncampus
Well if it starts at [0][0] and goes to [8][8] then it's 81.
• 02-07-2006
CornedBee
Yes, but if you declare it as
int ar[8][8];
it doesn't go up to [8][8], only [7][7].
• 02-07-2006
Kurisu
well anyways here is a way you could do it.. though not sure why you would unless it somehow saves computing time compared to the for loop method. In this example two arrays are created so it requires twice as much memory as a for loop, but maybe less CPU cycles to complete? I don't know, you would have to ask someone else as I've never attempted using memcmp() before. There might be a way to memory compare the entire array x with a 0, but I am not aware of how to do it.

Code:

```#include <iostream> int main() {         int x[8][8] = {0};  // Your array.         int y[8][8] = {0};        // Array of all zeroes.         std::cout << memcmp(x, y, sizeof(x)); // Should output 0 since x array is all zeroes. MEMCMP() stands for memory compare.         x[5][5] = 5;         std::cout << memcmp(x, y, sizeof(x)); // Should output 1 since x array is not all zeroes anymore.         return 0; }```
• 02-07-2006
CornedBee
No, that's no faster. Not really, anyway.

There is no better way to find zeros in an array than looping through it.

81 comparisons aren't that much, though.
• 02-07-2006
Daved
>> Well if it starts at [0][0] and goes to [8][8] then it's 81.

Just so you are clear, in Kurisu's example x and y only hold 64 values each. When you define the array variable, the number in the brackets is the size, not the highest legal index.

And I don't think memcmp would help unless you knew exactly what the non-zero values would be ahead of time.