Purpose. The purpose of this lab is to perform a calculation repetitively within a loop. The loop may be either a while loop or a for loop. You are to write a program that calculates the distance a vehicle has traveled in a certain number of hours. Write a program called vehicleTrip.cpp to calculate the distance traveled for a given speed. Requirements. 1. Using the following formula: distance rate * time = calculate how many miles a vehicle has travelled in a certain number of hours.
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
![**Distance Traveled, v 1.0**
**Purpose.**
The purpose of this lab is to perform a calculation repetitively within a loop. The loop may be either a while loop or a for loop. You are to write a program that calculates the distance a vehicle has traveled in a certain number of hours.
Write a program called `vehicleTrip.cpp` to calculate the distance traveled for a given speed.
**Requirements.**
1. Using the following formula:
\[
\text{distance} = \text{rate} \times \text{time}
\]
calculate how many miles a vehicle has traveled in a certain number of hours.
2. Have the program ask the user for the speed of the vehicle (in miles per hour) and how many hours the vehicle has traveled. **Do not accept a negative number for the speed of the vehicle, and do not accept a value less than 1 for hours the vehicle has traveled.**
3. The program should use a loop to display the distance the vehicle has traveled for each hour of the time period that has been inputted.
**Program I/O.**
- **Input:** Speed of the vehicle (in miles per hour), and Number of hours the vehicle has traveled. The input values are shown colored in blue (the program you write does not need to use blue coloring for console input).
- **Output:** A table-like format that looks similar to this:
```
What is the speed of the vehicle in mph? 40
How many hours has it traveled? 3
Hours Distance Traveled
========================
1 40
2 80
3 120
```
This output shows the distance traveled for each hour, multiplying the speed by the number of hours sequentially.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6f46ae0e-a0ee-4500-808d-7fa0f129e90c%2Fea054ffa-154e-415e-b6dc-8200a37de8f0%2Fyb6l0ik_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
The
The program doesn't work properly. if we input 40 for the first question (What is the speed of the vehicle in mph?) and 3 for the second ( How many hours has it traveled?). The ouput should be something 1 40 , which is not.
2 80
3 120
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)