1. Member
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

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?

3. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
ok would i have to use if else statements

4. Yes, the if statement could be used.

5. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
or would it just be easier to use conditional statements

6. An if statement and conditional statement are the same thing. (There are a few other types of conditional statements, such as switch-case, 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.

7. Member
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;```
would i keep going until i cover all 4 of the slots with all 24 possibilities.
there has got to be an easier way
Last edited by hoosierfan24; 10-01-2010 at 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.
You do not want to write 24 possibilities. What about when the problem changes to having 10 buckets vs just the 4 in this example? Think of a loop

9. Member
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

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.

11. Member
Join Date
Oct 2010
Posts
45
Rep Power
0
here is the exact ?

In the following code fragment, b is a four-element 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.

12. No mention of loops, but that is the natural way to do it.

13. Member
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

14. Member
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.

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; 10-01-2010 at 01:56 PM.

16. Member
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

17. extract the number of values
That is the looping way.
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

18. Member
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.

19. Originally Posted by hoosierfan24
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.
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++;`
Now all you have to do is properly initialize the value of 'count' and use 'i' in a loop ... When the loop has finished check the value of 'count'. Enough spoonfeeding for today ...

kind regards,

Jos

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; 10-01-2010 at 05:02 PM.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•