Question

In: Computer Science

PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance...

PROGRAM IN C++

The distance a vehicle travels can be calculated using the following equation: distance = speed * time

For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each hour of that time.

Sample Output: What is the speed of the vehicle in mph? 40 For how many hours has it traveled? 3 Hour Distance Traveled in Miles

----------------------------------

1 40

2 80

3 120

Input Validation:

Do not accept a negative number for speed and do not accept any value less than 1 for time traveled. Try to validate using loops.

Solutions

Expert Solution

C++ code:

#include <iostream>
using namespace std;
int main()
{
    //initializing hour and mph
    int hour,mph;
    //asking for speed
    cout<<"What is the speed of the vehicle in mph? ";
    //accepting it
    cin>>mph;
    //looping till a a non negative number is entered
    while(mph<0){
        //asking to enter a non negative number
        cout<<"Please enter a non negative number"<<endl;
        //asking for speed
        cout<<"What is the speed of the vehicle in mph? ";
        //accepting it
        cin>>mph;
    }
    //asking for hours
    cout<<"For how many hours has it traveled? ";
    //accepting it
    cin>>hour;
    //looping till a value greater than or equal to 1 is entered
    while(hour<1){
        //asking to enter a value greater than or equal to 1
        cout<<"Please enter a value greater than or equal to 1"<<endl;
        //asking for hours
        cout<<"For how many hours has it traveled? ";
        //accepting it
        cin>>hour;
    }
    //printing Hour Distance Traveled in Miles
    cout<<"Hour Distance Traveled in Miles"<<endl;
    //printing ----------------------------------
    cout<<"----------------------------------"<<endl;
    //loop to print Hour Distance Traveled in Miles
    for(int i=1;i<=hour;i++)
        //printing value of Hour Distance Traveled in Miles
        cout<<i<<" "<<i*mph<<endl;
    return 0;
}

Screenshot:


Input and Output:


Related Solutions

The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For...
The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles. Write a program that asks for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. Both values are assumed to be integers. It should use a loop to display the distance a vehicle has traveled for each hour of a time period...
1) Write a program in C++ sin(x) can be approximately calculated using the following formula, where...
1) Write a program in C++ sin(x) can be approximately calculated using the following formula, where n! is factorial(n) – for example 3!=3*2*1 = 6 (the function in previous problem). The more terms we use in the series, the higher will be accuracy of the calculations. By using infinite terms in the series we will have the exact value. Hint 1: For n! simply use (copy and paste) the factorialFunc(n)from previous problem. Hint 2: This problems is similar to the...
comments please Your program will calculate the distance an object travels (in meters) on Earth for...
comments please Your program will calculate the distance an object travels (in meters) on Earth for a specified number of seconds. You will also calculate the distance traveled on the Moon (in meters) for the specified number of seconds. Your program must have the main function and, at least, the following four additional functions. The signatures for these functions must be as follows: double readSeconds() double calculateEarthDistance(double seconds) double calculateMoonDistance(double seconds) void displayResults(double seconds, double earthDistance, double moonDistance) The readSeconds...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance...
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance in Feet. The user should be able to enter a distance in Meters, click a button, and then see the equivalent distance in feet. Use the following formula to make the conversion: Meters = Feet x 0.304 For example, 1 Meter is 3.28 Feet.
A program written in C that asks for the distance to be entered and then prints...
A program written in C that asks for the distance to be entered and then prints the fare A transportation company has the following rates For the first 100 miles                                                       20 cents a mile For the next 100 miles                                                       a) + 10 cents per mile over 100 miles For the next 100 miles                                                       b) + 8 cents per mile over 200 miles more than 300 miles                                                          c) + 5 cents per mile over 300 miles Write a program that asks...
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage...
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage = (new_odometer – old_odometer)/(gallons_gas) // illustrating how ‘for’ loop works. 2. How to initialize an array of size 5 using an initializer list and to compute it’s sum How to initialize an array of size 5 with even numbers starting from 2 using ‘for’ loop and to compute it’s sum 3. Program to compute the car insurance premium for a person based on their...
You measure the distance, l, a projectile travels several times using the same apparatus and find...
You measure the distance, l, a projectile travels several times using the same apparatus and find the following values (cm): 5.11, 4.89, 5.04, 4.94, 4.93, 5.06 and 5.09. Assuming that the errors are random: a) What is the mean value forl? b) What is the standard deviation for this data set? c) What is the uncertainty associated with your best estimate forl? d) How should one report the result of this measurement along with the uncertainty at the 95 %...
Write a C++ program to calculate the price of the annual insurance of a private vehicle according to the following table.
Write a C++ program to calculate the price of the annual insurance of a private vehicle according to the following table. Age of the person Model year of the car History of Accident Insurance price Less than 40 years Less than 2015 Yes 20 % more than price of last year No 15 % more than price of last year Later than 2015 Yes 12 % more than price of last year No 10 % more than price of last...
Done in C language using mobaxterm if you can but use basic C This program is...
Done in C language using mobaxterm if you can but use basic C This program is broken up into three different functions of insert, delete, and main. This program implements a queue of individual characters in a circular list using only a single pointer to the circular list; separate pointers to the front and rear elements of the queue are not used. The linked list must be circular.      The insert and remove operations must both be O(1)    You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT