Reputation: 17
I have declared a few arrays, in these array we have the following values:
{0, 1, 1, 0, 0}
{1, 0, 0, 0, 1}
{0, 1, 0, 0, 0}
I have multiple arrays with the same number of values.
For my Android application I am going to have a set of buttons.
If Button 1 is pushed, I want to know if any 1's are in positions 1, 3, and 5 and how many. If Button 2 is pushed, I want to know if any 1's are in positions 1, 2, 3, and 4 and how many.
Everything I have searched shows how to find out if 1's exist in the array, but not at a specific position. Any suggestions/help?
Details on what I am trying to accomplish if you wish to read, though I should be able to figure everything out with a helpful solution to my above question:
I have 64 buttons arranged in an 8x8 grid to represent sections of the human torso, and 64 numbers (0's or 1's) in my 13 arrays. The arrays identify if an organ is present in one of the aforementioned torso sections (1 if the organ is present, 0 if not present).
I want to push a button, and search all of the arrays for a 1 at that position. If I push button 35, I want to know if the liver is present in that section.
My ultimate output will tell the user what percentage of the organ is likely to be in that section. If a section contains a piece of the organ it will be a 1, and then divided by the total number of sections containing that organ.
If you've read this far, am I attacking this problem from the correct angle, do you have additional ideas?
Upvotes: 0
Views: 76
Reputation: 5092
From what you have describe in your question, you can do it just like this:
Lets say you have an array:
int[][] numArray = {{0, 1, 1, 0, 0},
{1, 0, 0, 0, 1},
{0, 1, 0, 0, 0}};
you just need to have method like this:
int countOne(int[] num){
int count=0;
for(int i=0;i<numArray.length;i++){
for(int j=0;j<num.length;j++){
if(numArray[i][num[j]]==1){
count++;
}
}
}
return count;
}
If Button 1 is pushed, I want to know if any 1's are in positions 1, 3, and 5 and how many:
call the method:
int[] button1 = {0,2,4};
System.out.println("the number of 1's:"+countOne(button1));
If Button 2 is pushed, I want to know if any 1's are in positions 1, 2, 3, and 4 and how many.
int[] button2 = {0,1,2,3};
System.out.println("the number of 1's:"+countOne(button2));
Upvotes: 1
Reputation: 613
I suggest you might want to do this using binary functions. here is an example for when button 2 is pressed first we declare the arrays as columns instead of rows
byte col1 = 8; // equivalent of 0 1 0 0 0
byte col2 = 1; // 0 0 0 0 1
byte col3 = 2; // 0 0 0 1 0
// this makes your original array look like
// 0 0 0
// 1 0 0
// 0 0 0
// 0 0 1
// 0 1 0
System.out.println(bitcount(col1) + bitcount(col2) + bitcount(col3));
/* to set or unset bytes
my_byte = my_byte | (1 << pos);
To un-set a bit:
my_byte = my_byte & ~(1 << pos);
*/
}
static private int bitcount(byte n) {
int count = 0 ;
while (n != 0) {
count++ ;
n &= (n - 1) ;
}
return count ;
}
then we count by counting the bits in of each of the columns we want to count
Construction an logical expression which will count bits in a byte
Upvotes: 1