I just recently started learning programming and this is my first language and I have no prior experience. I am following the Bjarne Stroustrup Programming and Principles book and the exercise says to do this:
Read a sequence of double values into a vector. Think of each value as
the distance between two cities along a given route. Compute and print
the total distance (the sum of all distances). Find and print the smallest
and greatest distance between two neighboring cities. Find and print the
mean distance between two neighboring cities.
The issue with my code comes when I try to input values for my vectors as in my distance values. I put my distance values in and click a letter to cancel the while loop. However, my entire window closes when I do this. I also am having trouble figuring out how to do greatest distance. I tried to sort the vectors by size in order to do smallest distance.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
usingnamespace std;
inlinevoid keep_window_open() { char ch; cin >> ch; }
int distances = 0;
int sum = 0;
int greatestDist = 0;
int smallestDist = 0;
int mean = 0;
int main()
{
vector <int> distCities;
while (cin >> distances)
{
if (distances > 0)
distCities.push_back(distances);
elseif (distCities.size() == 0)
cout << "Error, please enter a distance value." << endl;
}
for (int i = 0; i < distCities.size(); i++)
{
sum += distCities[i];
}
sort(distCities.begin(), distCities.end());
if (distCities.size() < 2)
{
smallestDist = distCities[0];
greatestDist = distCities[0];
}
else
{
smallestDist = distCities[1] - distCities[0];
//greatestDist??
}
mean = sum / distCities.size();
cout << "The total distance between all of the cities is: " << sum << endl;
cout << "The smallest distance between all of the cities is: " << smallestDist << endl;
//not sure how to do greatest dist yet
cout << "The mean is: " << mean << endl;
keep_window_open();
return 0;
}
try cin.getline() for keep open. or, just run the program from a console window that is already open.
greatest distance is the last 2 elements in the vector, reachable via back() or .size()-1 and .size()-2. or you can pop back twice or use the reverse iterator. Lots of ways to get there :)
Okay, just to confirm. My code is correct right and should work as intended? I want the while loop to stop working as soon as I stop putting in inputs for distance and go on to compute the sum and mean etc.
I didn't check your code 100% but it looks really good.
easier to just run it to see... add greatest distance code and run it, see if it works.
your <2 distance seems wrong. if there are 2, its [1] - [0] for both, right?
if there are zero or no entries, you need to do something else (possibly print invalid text).
your problem statement says double, your code is all integer.
there is no reason to make global variables, put them inside main, after first {
sorting may actually be a bug? the biggest distance between cities 1, 100, 3, 50 is 99. sorted, you will get 50. sorting is like taking the route from atlanta to Chattanooga to Nashville to Chicago and moving Atlanta next to Chicago before measuring the distance. This means you have some more work to do :(
but on the bright side, your efforts are in the top I have seen for brand new coders, very well done.