# arrays with booleans

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-01-2010, 02:55 AM
hoosierfan24
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
• 10-01-2010, 03:06 AM
Norm
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?
• 10-01-2010, 03:12 AM
hoosierfan24
ok would i have to use if else statements
• 10-01-2010, 03:28 AM
Norm
Yes, the if statement could be used.
• 10-01-2010, 03:37 AM
hoosierfan24
or would it just be easier to use conditional statements
• 10-01-2010, 03:47 AM
Zack
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.
• 10-01-2010, 04:16 AM
hoosierfan24
this will be the code for once the values in the array are given, am i on the right track?

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
• 10-01-2010, 04:19 AM
Norm
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.
Quote:

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
• 10-01-2010, 04:26 AM
hoosierfan24
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-01-2010, 04:40 AM
Norm
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.
• 10-01-2010, 04:44 AM
hoosierfan24
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.
• 10-01-2010, 04:49 AM
Norm
No mention of loops, but that is the natural way to do it.
• 10-01-2010, 04:52 AM
hoosierfan24
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
• 10-01-2010, 05:04 AM
hoosierfan24
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.
• 10-01-2010, 05:05 AM
Norm
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.
• 10-01-2010, 05:14 AM
hoosierfan24
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
• 10-01-2010, 02:56 PM
Norm
Quote:

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
• 10-01-2010, 04:44 PM
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.
• 10-01-2010, 04:55 PM
JosAH
Quote:

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':

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
• 10-01-2010, 05:57 PM
Norm
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);
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last