473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Nov 14 '07 #1
5 1768
Ganon11
3,652 Expert 2GB
The problem specification is fairly straightforward. What are you having trouble with?
Nov 14 '07 #2
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!!
Nov 14 '07 #3
dav3
94
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

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 8; i++)
  2. {
  3. do your math here. 
  4. }
  5.  
Im not certain but look at math.POW, I think that might have what you need
Nov 14 '07 #4
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??
Nov 15 '07 #5
Ganon11
3,652 Expert 2GB
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).
Nov 15 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rejimonb | last post by:
Can I fill a combobox without using a loop. Using a loop will be time consuming. Reji
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
1
by: python101 | last post by:
I would like to use loop 'for' to make something look like ------ * * * * * * * * * * #(please don't mind ----- because I can not type the first star in right position) however I...
1
by: bhavanirayala | last post by:
Hi, How can I get the hash values without loop? i.e I want to assign the value of a first key to one variable based on the condition.If that condition falis i need to assign the second value...
5
by: boss1 | last post by:
hi all, i have a problem with loop in select statement.i m using code : <select name = "s" size = "1" > <option selected>P-Code</option> <option...
1
by: xukaifeng | last post by:
Hi guys, I retrieve books from a database and put each book with its data into a bean. this works well, the beans are called bookBeanBeg1, bookBeanBeg2, bookBeanBeg3, bookBeanBeg4, etc. ...
1
by: smepperson | last post by:
Can anyone help explain this in simple terms. My textbok is useless... Have to code a Loop that will check if a name you are adding to a CheckedListBox has already been entered , if not add name...
5
by: dave816 | last post by:
Sorry for the Excel question in an Access forum...................I don't see an Excel forum and there's probably a reason for that but figured I'd give this a shot anyway. Again sorry, delete if...
1
by: Donnie Seibel | last post by:
I wrote a script to list all lines in a file with Perl. I am having trouble with the looping part of it. My script is supposed to look at the file and as long as the file is larger than the current...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.