Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java, cant figure this out

    i have the deitel 6th edition how to program java book.
    On page 227 example 5.12 it says:

    "Write an application that calculates the prduct of the odd integers from 1 to 15."



    That is all it says...does it mean multiply all the odd numbers from 1 to 15? If so how do i do this....im not sure how to point out negative numbers.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I'd probably get clarification from your teacher on that one. It can be interpreted a few different ways.

    My interpretation would be that you need to come up with an algorithm to compute the product for every combination of odd integers from 1 - 15. Obviously you wouldn't manually write out every combination so that means you need one or more loops and some code to determine if the two integers are odd and then multiply them together and do so for every combination of odd integers. So for example you want to:

    1x1 = 1
    1x3 = 3
    1x5 = 5
    1x7 = 7
    1x9 = 9
    1x11 = 11
    1x13 = 13
    1x15 = 15
    3x1 = 3
    3x3 = 9
    3x5 = 15
    .
    .
    .
    until you get to
    15x15 = 225

    Another question would be if it wants you to exclude duplicate calculations. Obviously you would have already done 1x3 so do you need to also include 3x1.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    I would guess it means 1x3x5x7x9x11x13x15. Technically thats what its asking, if it wanted spooksters example it should have said 'products' as you end up with multiple answers.
    Ofcourse what a question asks for and what it wants are not always the same thing
    I'm assuming the book has answers in it too? If so, ask a friend to look up the answer.

  • #4
    New Coder
    Join Date
    Feb 2006
    Location
    US
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    int product = 1;
    for (int i = 1; i <= 15, i+=2)
             product *= i;
    or you can do it recursively.
    Code:
    public int oddFactorial(int i)
    {
          if (i == 1)
             return 1;     
          return i * oddFactorial(i-2);
    }

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i tried both of those ways and cant seem to get either to work.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Ender
    Code:
    int product = 1;
    for (int i = 1; i <= 15, i+=2)
             product *= i;
    or you can do it recursively.
    Code:
    public int oddFactorial(int i)
    {
          if (i == 1)
             return 1;     
          return i * oddFactorial(i-2);
    }
    Why would you just provide a solution to what likely is a homework assignment? They didn't ask for a solution. They asked for an explanation of what the book was asking. The book he/she is using is used in the classroom for programming courses and those questions are normally assigned for homework.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    New Coder
    Join Date
    Feb 2006
    Location
    US
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Uber Fr0g
    i tried both of those ways and cant seem to get either to work.
    They do work. Can you not debug a comma? Learn to program.

    Quote Originally Posted by spookster
    Why would you just provide a solution to what likely is a homework assignment? They didn't ask for a solution. They asked for an explanation of what the book was asking. The book he/she is using is used in the classroom for programming courses and those questions are normally assigned for homework.
    I'm sorry, I know I shouldn't have given solutions, as he obviously posted his homework. I don't know why I answered like that... partly because it was early in the morning and I had not gotten a lot of sleep, and partly because of strange, subconscious, contemptuous reasons.


  •  

    Posting Permissions

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