Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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. #3
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    ok would i have to use if else statements

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Yes, the if statement could be used.

  5. #5
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    or would it just be easier to use conditional statements

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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. #7
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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 04:19 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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. #9
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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. #11
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

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

  13. #13
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #14
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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 02:56 PM.

  16. #16
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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. #18
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hoosierfan24 View Post
    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. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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 06:02 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. Finding Strings, booleans and Integers
    By Pez in forum SWT / JFace
    Replies: 1
    Last Post: 07-19-2009, 03:24 PM
  4. Help!! With arrays
    By ookie833 in forum New To Java
    Replies: 8
    Last Post: 12-14-2008, 01:57 AM
  5. Arrays
    By TheRocket in forum New To Java
    Replies: 6
    Last Post: 12-10-2008, 07:00 PM

Posting Permissions

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