Using a FOR LOOP to multiply weights!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javajive
    New Member
    • Nov 2007
    • 2

    Using a FOR LOOP to multiply weights!!

    Any ideas in how to do this question?? its causing me alot of trouble!

    Q.Write a class that uses a for loop to multiply your weight by itself eight times, storing the amount in another variable.
    i.e. on the first iteration your weight to the power of one will be stored. On the next iteration, this amount will again be multiplied by your weight and stored, and so on. Include any relevant classes to run and test the application.

    Thanks in advance
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    The problem specification is fairly straightforward . What are you having trouble with?

    Comment

    • javajive
      New Member
      • Nov 2007
      • 2

      #3
      with the FOR LOOP statement.
      I've been using it already for simple programs like a counter but don't know what to do with it when having to do math.
      i know the statement should be like:
      for(int i = 0;i < 4; i = i+1)

      but dont know how to fit math into!!

      Comment

      • dav3
        New Member
        • Nov 2006
        • 94

        #4
        Originally posted by javajive
        with the FOR LOOP statement.
        I've been using it already for simple programs like a counter but don't know what to do with it when having to do math.
        i know the statement should be like:
        for(int i = 0;i < 4; i = i+1)

        but dont know how to fit math into!!
        I think you want i < 8 not i < 4....

        and the math will go in the for loop, not when you declare it

        Code:
        for(int i = 0; i < 8; i++)
        {
        do your math here. 
        }
        Im not certain but look at math.POW, I think that might have what you need

        Comment

        • javatech007
          New Member
          • Nov 2007
          • 51

          #5
          how would i store the weight to a value so it can be used again??

          for example: weight = 5

          First it would be
          5 to the power of 1 = 5
          5*5=25
          25*5=125
          125*5=625
          so on for eight times

          with the loop:
          for(int i = 0; i < 8; i++)

          how would i work the weight into it??

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            OK, what are you doing to the value every time inside the loop? You start with an initial variable, weight. You are told to multiply it by itself 8 times. Actually, I read this two ways:

            1) You are squaring the term weight 8 times. So it is ((((((((weight^ 2)^2)^2)^2)^2)^ 2)^2)^2) = weight^(2^8) = weight^256.
            2) You are taking the value weight^8 by multiplying an additional variable, sum, by weight 8 times. So it is ((((((((1 * weight) * weight) * weight) * weight) * weight) * weight) * weight) * weight) = weight ^ 8.

            Which of these do you think it is? regardless, how can you make a mathematical statement to do this correctly, knowing that your loop is properly executing 8 times? (Hint - each one involves some simply multiplication) .

            Comment

            Working...