Answer the question by using C++ programme. A ball is thrown up in the air. Its
ID: 3586074 • Letter: A
Question
Answer the question by using C++ programme.
A ball is thrown up in the air. Its height above the ground is given by the formula h +10t - t2 where t is the time in seconds after it is thrown. For example, at 1, 3 = Time t =0, h-3+0-0=3 Time t = 2, h = 3 + 20-4=19 When the ball hits the ground, h becomes negative. Write a fragment of C++ code to print out the height, h, once every second, starting a t = 0, and continuing until h becomes negative (meaning the ball has hit the ground). (Do not print the negative value.) Include all necessary data declarations and initializations. 516c (10 marks)Explanation / Answer
#include<iostream>
using namespace std;
main()
{
// declaring variables
int t, h;
// looping from 0 and incrementing t by 1 each time
for(t=0;;t++)
{
// calculating h
h = 3+(10*t)-(t*t);
// if h is positive, printing output
if(h>-1)
cout << "t = " << t << " --> h = " << h << endl;
// else breaking the loop
else
break;
}
}
/* SAMPLE OUTPUT
t = 0 --> h = 3
t = 1 --> h = 12
t = 2 --> h = 19
t = 3 --> h = 24
t = 4 --> h = 27
t = 5 --> h = 28
t = 6 --> h = 27
t = 7 --> h = 24
t = 8 --> h = 19
t = 9 --> h = 12
t = 10 --> h = 3
*/
/* Rate it if you are happy with the answer.*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.