3a Program (OOPS)
a) Write a C++ program to read a sequence of double
values into a vector. Think of eachvalue as the distance between
two cities along a given route. Compute and int the total
distance. Find and print the smallest and greatest distance
between two neighboring cities.
Find and print the mean distance of the neighboring cities.
Program ::
#include < iostream>
#include < vector>
#include < algorithm>
using namespace std;
int main()
{
double val,sum=0;
vector< double>dist;
cout<< "Enter the series of distances between two cities along a route \n" ;
while(cin>>val&&val!=0)
{
dist.push_back(val);
sum+=val;
}
cout<<"\nThe total distance is: " << sum;
cout<<"\nThe max distance between two neighboring cities is " << *max_element(dist.begin(), dist.end());
cout<<"\nThe min distance between two neighboring cities is " << *min_element(dist.begin(), dist.end());
return (0);
}
Output::
Enter the series of distances between two cities along a route
25.77
50.50
60.73
77.77
0
The total distance is: 214.7
The max distance between two neighboring cities is 77.77
The min distance between two neighboring cities is 25.77