Question:
C++ loop and functions!?
iDontKnow
2012-03-26 14:44:16 UTC
hello, im stuck with this function/ loop program. i think im using the global constant correctly, but im its not working out in my equation.


heres the question:
When an object is falling because of gravity, the following formula can be used to determine the distance the object falls in a specific time period:
d=1/2gr^2

The variables in the formula are as follows: D is the distance in meters, g=9.8, and t the amount of time in seconds that object has been falling. Write a function named fallingDistance that accepts an object's falling time(in seconds) as an argument. The function should return the distance, in meters, that the object has fallen during the time interval. Write a program that demonstrates the function by calling it ina loop that passes the values 1-10 as arguments displays the return value.

here's what i got so far:


#include
#include
#include

using namespace std;

const double grav=9.8;
double fallingDistance(double time, double distance);

int main()
{

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

fallingDistance(time);
return 0;
}

double fallingDistance (double time)
{
double distance=0;
cout<<"please enter the time it took the object to fall:"< cout<<"Time"< cin>>time;
distance=(1/2)grav*pow(time,2.0);
cout<<"the distance is"< return distance;
}
Three answers:
?
2012-03-26 15:01:55 UTC
I fixed afew errors, try this (sample run): http://codepad.org/w5tWpQjv

____

#include

#include

#include



using namespace std;



const double grav=9.8;

double fallingDistance();



int main()

{

double distance=0;

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

{

distance =fallingDistance();

cout<
}

return 0;

}



double fallingDistance ()

{

double distance=0;

double time=4;

cout<<"please enter the time it took the object to fall:"<
cout<<"Time"<
cin>>time;

distance=((.5)*grav)*(pow(time,2));

cout<<"the distance is ";

return distance;

}
John H
2012-03-26 15:04:39 UTC
You were told to pass the value 1 thru 10 as the parameter time to your function. But then you've gone and overridden the value by prompting the user to enter it. That is wrong. Just compute distance from the value time passed into the function. I would put something in the for loop to print out time and distance, like:



printf("Time: %2d seconds Distance: %3.2f meters\n", time, dist);



where dist = fallingDistance(time)



or you could be tricky and write:



printf("Time: %2d seconds Distance: %3.2f meters\n", time, fallingDIstance(time));



You also have some minor syntax errors but you should be able to find those.
?
2016-11-29 16:09:38 UTC
you should use a for loop or an excellent as loop. I even have linked a pattern code for the for loop #contain cstdlib #contain iostream utilising namespace std; int substantial(int argc, char *argv[]) { int i,n; int sq., sum; cout<<"enter the quantity"<>n; for (i = 0; i <=n; i++) { sq. = i*i; sum = sum +sq.; } cout<<"the respond is "<


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...