473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Comparing Elements in an Array

20
I have a 2 dimensional boolean array and i need to compare some elements. I need to compare the neighbors of all the elements. One element has 8 neighbors, top and bottom, right and left, and then the diagonals would make 8. I also need a counter to count how many trues there are and then return the value some how. Im completely lost on how to do this so any help would be nice.
Dec 7 '08 #1
4 2489
Dököll
2,364 Expert 2GB
Greetings, nickels!

Do you have anything working thus far? I think there's a lot here you can use including of mine, but you'd need to search bit.

Post what you have, see what we can help you with. Thi might also give us a clear idea what you're hoping to achieve.

I'm just passing through, others may see your post if you dd here...

In a bit!

Dököll
Dec 8 '08 #2
hsn
237 100+
can you at least post you code please or your algorithm. so we could help you
Dec 8 '08 #3
nickels
20
ok i have to put where im counting the neighbors in the countNeighbors method. The genNextGrid is where i have to create a temporary two dimensional array and use the countNeighbors method. Depending on how many true and falses are around an element will change it to true or false or keep it where its at. Heres my code...


Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4.  
  5. public class test
  6. {
  7.  
  8.     // the size of the grid (GRIDSIZE x GRIDSIZE)
  9.     final private static int GRIDSIZE = 18;
  10.  
  11.     /********************************************************************************/
  12.     public static void main ( String args[] )
  13.     {
  14.         boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
  15.         char choice;
  16.         int x = 1;
  17.         Scanner sc = new Scanner ( System.in );
  18.  
  19.         do
  20.         {
  21.             System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? ");
  22.             choice = sc.next().charAt(0);
  23.         } while ( choice != 'r' && choice != 'q' && choice != 'g' );
  24.  
  25.         clearGrid (board);
  26.         setup(board,choice);
  27.  
  28.         do
  29.         {
  30.             System.out.printf ("Viewing generation #%d:\n\n", x++);
  31.             displayGrid(board);
  32.             genNextGrid(board);
  33.             System.out.print ("\n(q)uit or any other key + ENTER to continue: ");
  34.             choice = sc.next().charAt(0);
  35.         } while ( choice != 'q' );
  36.  
  37.     }
  38.  
  39.     /********************************************************************************/
  40.     public static void setup (boolean[][] board, char which )
  41.     {
  42.         Random randomNumbers = new Random();
  43.  
  44.         clearGrid(board);
  45.  
  46.         if ( which == 'q' )
  47.         {
  48.             // Set up the Queen Bee Shuttle pattern
  49.             board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true; 
  50.             board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true;
  51.             board[11][1] = true;        
  52.         }
  53.         else if ( which == 'g' )
  54.         {
  55.             // Set up a Glider
  56.             board [17][0] = true; board[16][1] = true; board[15][1] = true;
  57.             board[16][2] = true;
  58.             board [17][2] = true;
  59.         }
  60.         else
  61.         {
  62.             // set up random
  63.             for (int row = 0; row < board.length; row++ )
  64.             {
  65.                 for (int col = 0; col < board[row].length; col++ )
  66.                 {
  67.                     if ( randomNumbers.nextInt() % 2 == 0 )
  68.                         board[row][col] = true;
  69.                 }
  70.             }
  71.         }
  72.  
  73.     }
  74.  
  75.     /********************************************************************************/
  76.     public static void displayGrid (boolean[][] grid)
  77.     {
  78.         // Start printing the top row of numbers
  79.         System.out.print ("   ");
  80.         for (int x = 1; x <= grid.length; x++)
  81.         {
  82.             if ((x / 10) != 0)
  83.                 System.out.printf ( "%d", x / 10 );
  84.             else
  85.                 System.out.print ( " " );
  86.         }
  87.  
  88.         System.out.println();
  89.         System.out.print( "   " );
  90.  
  91.         for (int x = 1; x <= grid.length; x++)
  92.         {
  93.             System.out.printf ( "%d", x % 10 );
  94.         }
  95.         System.out.println();
  96.  
  97.         for (int r = 0; r < grid.length; r++)
  98.         {
  99.             System.out.printf ( "%d", r+1 );
  100.             if (r + 1 < 10)
  101.                 System.out.print ( "  " );
  102.             else
  103.                 System.out.print ( " " );
  104.             for (int c = 0; c < grid.length; c++)
  105.             {
  106.                 if (grid[r][c] == true)
  107.                     System.out.print ( "*" );
  108.                 else
  109.                     System.out.print ( " " );
  110.             }
  111.             System.out.println();
  112.         }
  113.     }
  114.  
  115.  
  116.     /*******************************************************************************/
  117.  
  118.  
  119.  
  120.     public static void clearGrid ( boolean[][] board )
  121.     {
  122.  
  123.      for (int i = 0; i < boolean.length; i++)
  124.      {
  125.      for( j = 0; j < boolean.length; j++)
  126.      {
  127.      boolean[i][j] = false;
  128.      }
  129.      }
  130.  
  131.     }
  132.  
  133.     public static void genNextGrid ( boolean[][] board )
  134.     {
  135.     }
  136.  
  137.     public static int countNeighbors ( final boolean[][] board, final int row, final int col )
  138.     {
  139.     }
  140. }
Dec 8 '08 #4
JosAH
11,448 Expert 8TB
For any point at index (x,y) jot down the index values in terms of x and y of all of its neighbours. Do you notice any regularity?

kind regards,

Jos
Dec 9 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chaz | last post by:
Hello, I hope someone can help me out. I am going to be taking the third step in a programming class soon(I took the previous two a while ago at a different school) and in an effort to get back up...
9
by: Varun Sinha | last post by:
I have an array of chars. I want to loop through it, compare each element to different chars and depending on which character it matches, take some action. My problem is that I can't figure out...
1
by: Donald Grove | last post by:
If I have two arrays, what is a good paradigm for comparing what is in them, to determine what elements they share, or don't share? Specifically, each array could potentially contain the integers...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
0
by: ccshine via DotNetMonster.com | last post by:
I'm working on an app that implements a Structure to store a recordset in an ArrayList. I used this setup to bind to a DataGrid and it worked out so well, I thought it might be a better solution...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
11
by: subodheee | last post by:
i have a problem in implementing script,i have to extract from below data op,offset,and i need to compare the if same offset,same op repeats in other process i need to make it as same,else no .please...
17
by: fgh.vbn.rty | last post by:
I know that std::vector<boolis specialized to store individual bools as single bits. I have a question about the comparison operation on a pair of vector<bool>s. Is the comparison done bit by...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.