Thread: array help

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    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.
    Last edited by manoncampus; 02-07-2006 at 03:54 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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];

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Well if it starts at [0][0] and goes to [8][8] then it's 81.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, but if you declare it as
    int ar[8][8];
    it doesn't go up to [8][8], only [7][7].
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    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;
    }
    Last edited by Kurisu; 02-07-2006 at 04:59 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM