in c++ Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. It should use a loop to display the total distance traveled. The speed traveled should be limited by the fastest speed achieved by a car thus far. Values should not be negative.•Ex: if hours = 3 and speed = 40, then the program should display•Hour 1: Distance Traveled: 40 miles•Hour 2: Distance Traveled: 80 miles•Hour 3: Distance Traveled: 120 miles
continued•Write a program that asks a person for their weight then converts their weight to the weight on the planet of their choice. This program should use a switch. You may have to use google to look up some conversions. •Example: If the user entered a weight of 180 lbs and chose the planet Jupiter. The program should return: “Your weight on Jupiter is 455 lbs.
1.
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include <iostream>
using namespace std;
/*main function*/
int main()
{
/*variables*/
int speed,hours,i;
/*read speed from user*/
cout<<"Enter the speed of the vehicle(in miles
per hour): ";
cin>>speed;
/*read hours from user*/
cout<<"Enter number of hours travelled: ";
cin>>hours;
/*using a for loop to display the total distance
traveled*/
for(i=1;i<=hours;i++)
{
/*calculate and print
distance*/
int d=speed*i;
cout<<"Hour
"<<i<<": Distance Travelled: "<<d<<"
miles"<<endl;
}
return 0;
}
2.
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include <iostream>
using namespace std;
/*main function*/
int main()
{
/*variables*/
double weight;
int user;
string planet;
/*enum planets names*/
enum
planets{mercury,mars,uranus,venus,saturn,neptune,jupiter};
/*read weight from the user*/
cout<<"Enter you weight: ";
cin>>weight;
/*display and ask the user to choose planet*/
cout<<"1. Mercury 2.mars 3. uranus 4. venus 5.
saturn 6. neptune 7.jupiter\n";
cout<<"Choose a planet: ";
cin>>user;
/*switch block*/
switch(user-1)
{
/*for mercury*/
case mercury:
weight=((weight)/9.81)*3.7;
planet="mercury";
break;
/*for mars*/
case mars:
weight=((weight)/9.81)*3.711;
planet="mars";
break;
/*for uranus*/
case uranus:
weight=((weight)/9.81)*8.69;
planet="uranus";
break;
/*for venus*/
case venus:
weight=((weight)/9.81)*8.87;
planet="venus";
break;
/*for saturn*/
case saturn:
weight=((weight)/9.81)*10.44;
planet="saturn";
break;
/*for neptune*/
case neptune:
weight=((weight)/9.81)*11.15;
planet="neptune";
break;
/*for jupiter*/
case jupiter:
weight=((weight)/9.81)*24.79;
planet="jupiter";
break;
/*if invalid option*/
default:
cout<<"You
choose invalid planet!";
exit(0);
}
/*print weight*/
cout<<"Your weight on "<<planet<<"
is "<<weight<<" lbs";
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.