Vector element

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) {
^
dude, you've got code floating in the middle of nowhere, namely all of this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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));


that stuff's not in a function or any kinda data structure.
also
1
2
3
4
5
6
7
{

   cout<< " Smallest element: << y.s << " '\n';
   cout<< " Largest element: << y.l << " '\n';
   cout<< " Median: << y.m << " '\n';

 }
You might want to refresh your memory on how to write a function that takes parameters.
Does this look better?

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

};


int find (vector<int>&v, int n)

{

Res y;

for (int i = 0; i<v.size(); ++i) {

while (cin>>y) { v.push_back(y); }

for (int y : v) n+=y;

 y.m = n/v.size();
 y.s = min_element(begin(v), end(v));
 y.l = max_element(begin(v), end(v));

 cout<< v[m];
 cout<< v[s];
 cout<< v[l];

 }

}



int main()

{  
   int n;
   vector<int> vf;

   find(n,vf);

}




Last edited on
Yes... and no. Try doing all of the array creation and filling inside int main and then write the function that can do three things by returning a struct.
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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//your struct is okay
struct Res {

  int s;             // smallest element
  int l;             // largest element
  int m;             // median

};

//change your function to look like this
Res function(vector<int>& v) {

}

int main() {
    vector<int> vf;
    
    //fill vf with the numbers 1 through 10
    for(int i = 1; i <= 10; i++) {
        vf.push_back(i);
    }
    
    Res r = function(vf);
    cout << "Smallest element: " << r.s << "\n";
    cout<< "Largest element: " << r.l << '\n';
    cout<< "Median: " << r.m << '\n';
    
    return 0;
}
Last edited on
Also, your code to populate the array is wrong. I'll repeat it here:
1
2
3
for (int i = 0; i<v.size(); ++i) {

while (cin>>y) { v.push_back(y); }

This basically says
1
2
for each element in the array v
    read all numbers from cin and add them to the array.

If the array is empty to begin with (and it probably will be) then you won't put anything in it. You can do away with the for loop entirely and just do the while loop:
while (cin>>y) { v.push_back(y); }

Your code to compute the min/max won't work. min_element() and max_element() return an iterator, not the value of element. Hmm. An iterator? That should make you wonder if it can return v.end() and what you should do in that case. Hint: it can return v.end().

It's great that you're getting familiar with max_element() and min_element(), but let me suggest an additional exercise after you get this working: Consider the case where the vector contains 10 million elements. You're making 3 complete passes through it: once to compute n, once in min_element and once in max_element. Can you rewrite the code to make just one pass instead? You will need to find the min and max elements yourself.
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

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

using namespace std;


struct Res {

  int s;             // smallest element
  int l;             // largest element
  int m;             // median

};


Res function(vector<int>& v, int y, int n) {

while (cin>>y) { v.push_back(y); }

for (int y : v) n+=y;

 y.m = n/v.size();
 y.s = min_element(begin(v), end(v));
 y.l = max_element(begin(v), end(v));

}

int main() {
 
      
   int sum;
   int val;
    
   while (cin>>y) {
   vector<int> vf;  
    
    Res r = function(vf,val,sum);
    cout << "Smallest element: " << *r.s << '\n';
    cout<< "Largest element: " << *r.l << '\n';
    cout<< "Median: " << r.m << '\n';
    
    return 0;

 }

}

1
2
3
4
//change your function to look like this
Res function(vector<int>& v) {

}

Why did you add int y, int n????? You can declare local variables in the function like this:
1
2
3
4
5
6
7
8
9
10
11
Res function(vector<int>& v) {
    int sum = 0;
    for(int x : v) {
        sum += x;
    }
    Res res;
    res.m = sum / v.size();
    res.s = *min_element(begin(v), end(v));
    res.l = *max_element(begin(v), end(v));
    return res;
}

Complete example here:
http://coliru.stacked-crooked.com/a/42786a64dfc8730b
Last edited on
This should work:

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

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

using namespace std;


struct Res {

  int s;             // smallest element
  int l;             // largest element
  int m;             // median

};


Res function(vector<int>& v)

{
    int sum=0;
    
    for (int x : v) { sum+=x; }
    Res res;
    res.m = sum / v.size();
    res.s = *min_element(begin(v), end(v));
    res.l = *max_element(begin(v), end(v));

}


int main() 

{
    int x;
    vector<int> vf; 
    while (cin>>x) {  vf.push_back(x); } 
    Res r = function(vf);
    cout << "Smallest element: " << r.s << '\n';
    cout<< "Largest element: " << r.l << '\n';
    cout<< "Median: " << r.m << '\n';
    
    return 0;

}

You forgot to return res; at the bottom of function
Edit: Probably because my small example did too, but the complete example did have it.
Last edited on
and also computes the mean and the median


This isn't completely relevant to the problems you're facing, but if this is homework, you don't want to lose marks by forgetting about the median :-)

Also consider getting the min and max in your range for, rather than making 3 passes through the vector asb dhayden)

void gsl_vector_minmax (const gsl_vector * v, double * min_out, double * max_out)


This function returns the minimum and maximum values in the vector v, storing them in min_out and max_out.


Taken from secret library.
Topic archived. No new replies allowed.