Two cars A and B leave an intersection at the same time. Car A travels west at an average
speed of x miles per hour and car B travels south at an average speed of y miles per hour.
Write a program that prompts the user to enter:
1. The average speed of Car A and Car B as two separate input statements.
2. The elapsed time (in hours and minutes, separated by a space) after the cars leave
the intersection.
o Ex: For two hours and 30 minutes, 2 30 would be entered
The program then outputs:
1. The (shortest) distance between the cars.
Hint:
You will need to use the Pythagorean theorem to calculate the distance.
Here is what I wrote so far and cannot for the life of me figure out what I am doing wrong. Any help would he highly helpful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
/* CS161 Winter 2019
Assignment 3
Joshua Gibson
Sources:
https://www.mathsisfun.com/pythagoras.html
Purpose:
This program calculates the shortest diatance between car A and car B from user inputs
of speed of car A (mph), speed of car B (mph), and elapsed time (in hours and minutes)
Formula / Assumptions
// (shortest_distance_traveled)^2 = [(speed_of_car_A + speed_of_car_B)/2]^2 + (elapsed_time)^2
*/
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double shortest_distance_traveled = 0;
double speed_of_car_A = 0;
double speed_of_car_B = 0;
double elapsed_time = 0;
shortest_distance_traveled = sqrt((speed_of_car_A + speed_of_car_B) / 2 * (speed_of_car_A + speed_of_car_B) / 2 + elapsed_time * elapsed_time);
cout << "Enter the speed of car A (mph): ";
cin >> speed_of_car_A;
cout << "Enter the speed of car B (mph): ";
cin >> speed_of_car_B;
cout << "Enter the time the cars traveled (in hours and minutes seperated by a space): ";
cin >> elapsed_time;
cout << "The shortest distance traveled between car A and car B: ";
cout << shortest_distance_traveled << endl;
return 0;
}
|