Program20

Hello,

I had this exercise:

Read a sequence of doubles 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int main()

{

cout<< "please enter a whitespace-separated sequence of doubles (representing distances) : ";

vector<double>distances;
for (double distance;cin>>distance;)
distances.push_back(distance);

double sum=0;
for (double x:distances) sum+=x;

min/max of vector?

max_element (distances.begin(), distances.end()); ?


If some of you would be more technically-oriented and know how to find min/max of vector I would appreciate, thanks.

Plans are to start at university at some point, to get my learning going faster :)
Last edited on
That is the bog-standard "show min, max, sum, and avg for a set of numbers".

The question is: Do you want to learn to use std::max_element or to learn the logic of finding the largest value?

The answer to both is in the documentation of std::max_element.
I can`t get the code to run, I`m now on my windows partition.

http://www.compileonline.com/compile_cpp_online.php

Oops! Now it erased all the changes that I did

Here is the code rewritten:

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
#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;


int main()

{

cout<< "please enter a whitespace-separated sequence of doubles (representing distances) : ";

vector<double>distances;
for (double distance;cin>>distance;)
distances.push_back(distance);

double sum=0;
for (double x:distances) sum+=x;




cout << "sum of the distances is " << sum << '\n';
cout << "The smallest distance is " << min_element(distances.begin(), distances.end()) << '\n';
cout << "The largest distance is "  << max_element(distances.begin(), distances.end()) << '\n';
cout << "The mean distance is" << sum/distances.size() << '\n';

return 0;

}
Last edited on
What do you mean by "can`t get the code to run"?

Pay attention to the return type of min_element and max_element.

The use of for on lines 15-16 is ... it is more common to write that as a while loop.
It doesn`t compile and produces too long error message to be posted here.

Isn`t the syntax of min/max element in a vector max_element(std::begin(cloud), std::end(cloud));
The error(s) may be long, but at least check the line-number. Which line has the first error?


What is the return type of function max_element?
Yes yes, I already fixed the error.

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

#include<algorithm>
#include<iostream>
#include<vector>

using namespace std;


int main()

{

cout<< "please enter a whitespace-separated sequence of doubles (representing distances) : ";

vector<double>distances;
for (double distance;cin>>distance;)
distances.push_back(distance);

double sum=0;
for (double x:distances) sum+=x;




cout << "sum of the distances is " << sum << '\n';
cout << "The smallest distance is " << *min_element(distances.begin(), distances.end()) << '\n';
cout << "The largest distance is "  << *max_element(distances.begin(), distances.end()) << '\n';
cout << "The mean distance is" <<'\t'<< sum/distances.size() << '\n';

return 0;

}

	


One thing seems strange though. Range based for loops are not allowed in C++98. Is there a way to change the compiler settings so that it would support all these modern standards. Because it is a pain to write always -std=c++11.

Or maybe I should just write in C++98 mode, why not, people managed to write with that standard back then.
How do you compile? Command line, IDE, ...?
As already indicated, I use Linux and gcc and that will not change at least for the next 30 years...
you can probably use bash aliases
OS makes no difference.

It seems like you type the
g++ [options] [files]

Look up GNU Make. Create a Makefile for the project. Then you can limit typing to
make


A step further is to use something like cmake. The cmake creates Makefile for you.

(Bash has a command-line history too, so it is easy to repeat commands.)
Topic archived. No new replies allowed.