A travel group travelled to 5 cities. The
group started its journey from city A and ended at city A. From A it travelled
to city B, from B to city C, from C to city D, from D to city E and form E back
to city A. The distance between two cities are to be provided by the user of
your program. Also, user will provide the total duration of the trip. Write a
program to calculate the total distance travelled by the group and the average
speed for the trip?
Here is an example of the output. User inputs
are given in bold.
What is the distance between cities A and B
(in miles) : 200.6
What is distance between B and C (in miles) : 100.6
What is distance between C and D (in miles) : 150.3
What is distance between D and E (in miles) : 100.1
What is distance between E and A (in miles) : 250.5
What is the total trip (in hours) : 16.3
The total distance travelled is: ? (To be calculated)
Average speed (miles/hr): ? (To be calculated)
Post what you have written so far [code]between code tags[/code] so that we can help you further. If you don't even know how to start, the hello-world program is a good template. See also, the tutorial on this site: http://www.cplusplus.com/doc/tutorial/
//Distance in cities
#include <iostream>
using namespace std;
int main ()
{
"distance between cities A and B : 200.6
"distance between B and C : 100.6
"distance between C and D : 150.3
"distance between D and E : 100.1
"distance between E and A : 250.5
//Distance in cities
#include <iostream>
usingnamespace std;
int main ()
{
"distance between cities A and B : 200.6
"distance between B and C : 100.6
"distance between C and D : 150.3
"distance between D and E : 100.1
"distance between E and A : 250.5
Start with something like this:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
usingnamespace std;
int main ()
{
double a = 0;
cout <<"Enter the distance between cities A and B : "<< endl;
cin >> a;
return 0;
}
i have done it as you said Mr Kermot below here it is
//Distance in cities
#include <iostream>
using namespace std;
int main ()
{
double a = 0;
cout <<"distance between cities A and B : 200.6 "<< endl;
cin >> a;
cout <<" between cities B and C :100.6 "<< endl;
cin >> a;
cout <<" between cities C and D :150.3 "<< endl;
cin >> a;
cout <<" between cities D and E :100.1 "<< endl;
cin >> a;
cout <<" between cities E and A :250.5 "<< endl;
cin >> a;
return 0;
You will want to store each distance in a different variable. Also, do not include the numbers in the prompt - the numbers are what the user types in, and should not exist in your code.
#include <iostream>
int main()
{
std::cout << "Enter the distance between cities A and B: " << std::endl;
double a_to_b = 0;
cin >> a_to_b;
std::cout << "Enter the distance between cities B and C: " << std::endl;
double b_to_c = 0;
cin >> b_to_c;
//...
}
By the way, you can get the fancy formatting by putting your code [code]between code tags[/code] - it helps a lot because it shows indentation and line numbers.