Hello,
I decided to move on with the exercises so here is one:
Write a function that finds the smallest and largest element of a vector argument and also computes the mean and the median. Do not use global variables. Either return a struct containing the results or pass them back through reference arguments.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Res {
int s; // smallest element
int l; // largest element
int m; // median
};
vector<int>v;
Res y;
for (int i=0; i<v.size(); ++i) {
while (cin>>y) { v.push_back(y); }
}
int sum=0;
for (int y : v) sum+=y;
y.m = sum/v.size();
y.s = min_element(begin(v), end(v));
y.l = max_element(begin(v), end(v));
void print();
{
cout<< " Smallest element: << y.s << " '\n';
cout<< " Largest element: << y.l << " '\n';
cout<< " Median: << y.m << " '\n';
}
int main()
{
print();
}
|
I`m getting some strange error again when trying to compile:
Documents/Program33.cpp:39:1: error: stray ‘\302’ in program
{
^
Documents/Program33.cpp:39:1: error: stray ‘\240’ in program
Documents/Program33.cpp:22:1: error: expected unqualified-id before ‘for’
for (int i=0; i<v.size(); ++i) {
^
Documents/Program33.cpp:22:15: error: ‘i’ does not name a type
for (int i=0; i<v.size(); ++i) {
^
Documents/Program33.cpp:22:27: error: expected unqualified-id before ‘++’ token
for (int i=0; i<v.size(); ++i) {
^