Results 1 to 20 of 25
Thread: arrays with booleans
 10012010, 01:55 AM #1Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
arrays with booleans
ok so i am writing this program. the user inputs 4 boolean values in the array b. i then have another boolean called onlyone its value will be true only if exactly one of the 4 inputed values in the array is true.
Any help would be appreciated on what to do, i am completely clueless
 10012010, 02:06 AM #2
List the steps the code must do to solve this problem.
What do you need for this: the user inputs 4 boolean values
Then when you have all for values,
what do you need to do for this: if exactly one of the 4 inputed values in the array is true.
Describe the steps you'd take if you were doing it manually. Say you had 4 buckets and the user was to put a black or white ball in each bucket. What would you have to do to determine if there was exactly one black ball in the four buckets?
 10012010, 02:12 AM #3Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
ok would i have to use if else statements
 10012010, 02:28 AM #4
Yes, the if statement could be used.
 10012010, 02:37 AM #5Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
or would it just be easier to use conditional statements
 10012010, 02:47 AM #6
An if statement and conditional statement are the same thing. (There are a few other types of conditional statements, such as switchcase, but ignore those for now.)
Norm suggested describing the steps to do it manually, and provided a nice analogy. Instead of worrying about making it a computer program right now, write out (on paper, if you have to) how you do this type of process in your own mind.
 10012010, 03:16 AM #7Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
this will be the code for once the values in the array are given, am i on the right track?
Java Code:if a[0]= true && a[1]= true onlyOne= false; else if a[0]= true; && a[1]= false; && a[2]= true; onlyOne= false;
there has got to be an easier wayLast edited by hoosierfan24; 10012010 at 03:19 AM.
 10012010, 03:19 AM #8
No, you need to design the program BEFORE trying to code it.
What are the steps needed to solve the problem. When you have those, then worry about how to code it.
until i cover all 4 of the slots with all 24 possibilities.
 10012010, 03:26 AM #9Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
the step is if only one value is true then onlyone is true, otherwise its false.
Also i have not learned loops yet, i actually am not supossed to know if statements in the part of the class im in, i just know them because i have written an ipod application in c++.
In my class i think im expected to write this program using only arrays and the && or  or != statements
 10012010, 03:40 AM #10
Writing if tests for all the combinations is an unusual way of doing it.
What is the purpose of having an array if you don't use loops.
Why not have four variables that you read the user data into?
I can't understand the assignment.
Are you allowed to look at and remember the user's input as it is read in? Then you'd just count the trues that were entered.
 10012010, 03:44 AM #11Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
here is the exact ?
In the following code fragment, b is a fourelement array of booleans and onlyone is a boolean. Write code that assigns true to onlyone if and only if exactly one element of b is true.
 10012010, 03:49 AM #12
No mention of loops, but that is the natural way to do it.
 10012010, 03:52 AM #13Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
yes i know its weird, im sure i could do it with loops we just wont learn it for another 2 chapters in the book
 10012010, 04:04 AM #14Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
from my brief understanding of loops, i would have to use a for loop as opposed to a while loop, correct?
but i would appreciate it if you could just tell me a code that would work since i have already dont 6 labs on arrays tonight and this is the last and i just cant figure it out.
 10012010, 04:05 AM #15
Just had an inspiration while brushing my teeth. Programmers tend to generalize problems.
For this problem let's restate it so we can't generalize. We'll use playing cards and deal some hands of cards.
The problem is to determine if one and only one suit is void (no cards in that suit) in a hand.
Here there are only 4 suits to work with and the "true" condition is the suit being void.
This goes back to the code you posted in #7. But there are not 24 possibilities.
What would be the test to determine if the hand is void only in spades?
There are clubs and there are diamond and there are hearts and there are NO spades.Last edited by Norm; 10012010 at 01:56 PM.
 10012010, 04:14 AM #16Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
i still dont have a clue.
the only thing in my head that makes sense is to extract the number of values that equal true and if that number is greater than 1 or euqual to zero then the condition is false.
I just dont know what method i will use to extract the number of values
 10012010, 01:56 PM #17extract the number of values
Look at the last sentence in post#15 and think how to write a boolean expression that returns true for that statement. Then add on for the other 3 suits
 10012010, 03:44 PM #18Member
 Join Date
 Oct 2010
 Posts
 45
 Rep Power
 0
I understand the boolean expression part, im just not sure how I will get the program to be able to determine which values are true, and which false/ which cards are which.
 10012010, 03:55 PM #19
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Let 'count' be the number of 'true' values in your array; let 'i' be an index value in your array; the following code snippet updates 'count' according to the value at index 'i':
Java Code:if (array[i]) count++;
kind regards,
Jos
 10012010, 04:57 PM #20
The OP said he was NOT allowed to use loops to solve this.
Try this: write a boolean expression with two boolean variables that returns true if the first boolean is true and the other one is false.
boolean firstOnlyTrue = ????
Here is a sample that uses two boolean variables that returns true if BOTH of the boolean variables are true:
boolean bothTrue = (bVar1 && bVar2);Last edited by Norm; 10012010 at 05:02 PM.
Similar Threads

store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04192010, 10:32 AM 
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03232010, 02:40 AM 
Finding Strings, booleans and Integers
By Pez in forum SWT / JFaceReplies: 1Last Post: 07192009, 02:24 PM 
Help!! With arrays
By ookie833 in forum New To JavaReplies: 8Last Post: 12142008, 01:57 AM 
Arrays
By TheRocket in forum New To JavaReplies: 6Last Post: 12102008, 07:00 PM
Bookmarks