Answer to Question #27335 in C++ for red rose

Question #27335
An automobile travels at an average speed of 55 miles per hour for four hours. Write a C++ program using a while loop that displays the distance driven, in miles, that the car has traveled after, 1, 2, etc., hours until the end of the trip.
1
Expert's answer
2013-03-29T13:38:21-0400
#include <iostream>

using namespace std;


//distance == time * speed;
//if you need sentence-answer just delete this symbols in code : "/*" "*/"
int main() {
int time = 0; //current time from the start in hours
const int miles_per_hour = 55; //miles per hour
int distance = 0; //distance at the current time
while (time <= 4) {
& time++; // increment current time
& distance += miles_per_hour;// add miles that car traveled during new hour
& cout << distance; // output distance in miles after "time" hours
& /*cout<<" miles after "<<time;
& if (time == 1) cout<<" hour"; //if time == 1 hours must be in singular
& else cout<<" hours";*/
& cout << "\n";
}
system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS