Public Group

# for and while loop problems

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i am having problems with my program wrote the program but my output is some random 25 digit number instead of my data that i did on paper. maybe its something in my code i dont know. here is the problem and my code. Write a program that displays a table which shows the height of a projectile launched straight up for each second form launch time (time zero) until the projectile hits the ground. The last entry in the table should show a projectile height of 0. The height after t seconds is given by: s = v0t - 1/2gt2, where v0 is the launch velocity in m/s and the gravitational constant g is 9.8 m/s2. The program should prompt the user for the launch velocity. Output of height should be to one decimal place. Make runs of your program for launch velocities of 60 and 87 m/s. Here is a sample format for your table given a launch velocity of 60 m/s.1 Projectile launched straight up at 60 m/s time height 1 0.0 2 55.1 3 100.4 4 135.9 .. 12 14.4 13 0.0 MY CODE AND OUTPUT WHEN RAN #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main () { cout << "Lab 3 \t\t\t Robert Lury\n\n "; double height, velocity; int time; cout << "Enter launch velocity: "; cin >> velocity; cout.setf(ios::fixed); cout << setw(8) << "time" << setw(16) << "height" << endl; for (height = 0; height == 0; time ++) { height = (velocity*time)-(1/2)*(9.8)*(time)*(time); cout << time << height << endl; } system ("pause"); return 0; } Lab 3 Robert Lury Enter launch velocity: 60 time height 2293672137620320 Press any key to continue . . .

##### Share on other sites
Sorry but we provide very limited help with homework.

The problem is not hard to find with a little thinking and work. So just spend a little time and you will find it.

theTroll

##### Share on other sites
i dont uderstand loops could you help clarify them?

##### Share on other sites
look at for(height=0;height==0;timer++)

it has something to do with height == 0;

I'll give you a hint, >=

##### Share on other sites
That I can do..

There are really three main types of loops.. the for loop, while loop and do while loop. The while and the do while loops are very similar, the only difference is that in the do while loop it is always executed once, while the while loop may not be executed at all. The both use a conditional statement to test if the loop should be continued, as long as the condition is true the loop continues.

The for loop is a very neat little construct. It has an initializer, a condition and a counter all in one little package.

for(initializer, condition, counter)

for(int a = 0; a < 10; a++)

a starts as 0 and goes through 9 counting up by one each time.

for(int b = 100 ; b > 0; b += -2)

b stats at 100 and goes down to 2 counting down by 2 each time.

I hope this helps.
theTroll

##### Share on other sites
Quote:
 Original post by rluryi am having problems with my program wrote the program but my output is some random 25 digit number instead of my data that i did on paper. maybe its something in my code i dont know.

I suppose thats possible. :D

If you really need help understanding loops I suggest you read the chapter on loops in your text. At that point, the problem should become obvious.

##### Share on other sites
I won't tell you how to do your homework, but I don't mind explaining the programming principles if I can.

for loops ...

for (start_value; condition_to_stop_looping; value_to_change)
{
Statements to execute until condition_to_stop is reached
}

Generally, you want to use 'for' loops when you know the exact number of iterations (cycles/loops) that you need to complete.

'for' loops check condition_to_stop BEFORE running the statements inside the {}, if the condition is already met, it will not enter the block and the
statements inside the block will never execute. (HINT)

A typical for loop looks like this ...

for(int counter = 0; counter > 50; counter++)
{
//Run statements in here once for each value of 'counter'
//until the counter variable goes over 50
//then break out of the loop and start with the next line of
//code below the closing bracket
}
============================================================================
while loops ...

while(expression){
//Execute statements in this block as long as
//'expression' evaluates to true
//when 'expression' evaluates to false, then exit the loop
//and continue with the next statement below the closing }.
}

'while' loops are typically used when you don't know how many times you need to
loop before the expression evaluates to false.

Example:
int value = 0;
while(value < 100){
value += (Random(10) +1) //Adds a random number between 1 and 10
//to 'value', and stores it in 'value'
}

In this situation, we don't know how many loops it will take for 'value' to exceed 100 and cause the expression to become false.
===============================
'do ... while' loops

do{
//Run these statements until 'expression' evaluates to false
}while(expression);

They work the same as 'while' loops with the exception that they will evaluate the expression AFTER the code block is executed. So, a 'do...while' loop will always execute at least once.

=============================
Hope that helps.

##### Share on other sites
thanks to all you guys for the help i see and understand loops a lot clearer now thank you.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013558
×