algorithm 01

What is correct answer:
a. runtime error
or
b. 0,2,3,
thank you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Pocket { 
        int value;
public:
        Pocket(int value):value(value){}
        int getValue() const   { return value; }  
        bool operator < (const Pocket & _Right) const  { return value < _Right.value; } };
ostream & operator <<(ostream & stream, const Pocket & pocket) 
{         stream << pocket.getValue();         return stream;	}
void printer(Pocket i) {        cout << i << ", ";	}
int main() {
        Pocket mynumbers1[]={ 3, 9, 0, 2};
        sort(mynumbers1, mynumbers1 + 4);//LINE I
        vector<Pocket> v1(mynumbers1, mynumbers1+3);
        inplace_merge(v1.begin(), v1.begin()+3, v1.end());//LINE II
        for_each(v1.begin(), v1.end(), printer);        return 0;	}
- to use std::sort() on c-style arrays use std::begin(), std::end() instead of using the array size as a proxy for a 1 past the end iterator (see link in program)
- line 17: use the sizeof operator to right-size the vector, else as you can see you're missing the last element
- line 18: v1.begin() + 3 encapsulates all the elements of the vector, perhaps you want to split it a bit earlier for something non-trivial?
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 <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Pocket {
        int value;
public:
        Pocket(int value):value(value){}
        int getValue() const   { return value; }
        bool operator < (const Pocket & _Right) const  { return value < _Right.value; } };
ostream & operator <<(ostream & stream, const Pocket & pocket)
{         stream << pocket.getValue() << " ";         return stream;	}
void printer(Pocket i) {        cout << i << ", ";	}


int main() {
        Pocket mynumbers1[]={ 3, 9, 0, 2};
        //sort(mynumbers1, mynumbers1 + 4);
        //4 == proxy for 1 past the end iterator, following link says it may not be good practice
        //http://stackoverflow.com/questions/5897319/how-to-use-stdsort-to-sort-an-array-in-c
        std::sort(std::begin(mynumbers1), std::end(mynumbers1));
        for (size_t i = 0; i < 4; ++ i)std::cout << mynumbers1[i];
        std::cout << '\n';
        vector<Pocket> v1(mynumbers1, mynumbers1 + (sizeof(mynumbers1)/sizeof(mynumbers1[0])));


         inplace_merge(v1.begin(), v1.begin()+1, v1.end());//LINE II

     //  inplace_merge(v1.begin(), v1.begin()+2, v1.end(), [](const Pocket& lhs, const Pocket& rhs){return lhs.getValue() < rhs.getValue();});//LINE II
     //alternative using lambda, not strictly required as operator < has already been overloaded
        for_each(v1.begin(), v1.end(), printer);        return 0;
     }

ps: btw you don't need the printer() function any more as the extraction operator << has been overloaded for Pocket
Last edited on
Thank's gunnerfunner for spending your time.
These codes are from C++ Institute tests and I don't understand which is, for them, the correct answer. You made a very good and interesting comment, but I stiil not understand which is the correct answer for c++ Institute
If the question is whether or not your program is well-formed, then the answer is yes, and the output is 0, 2, 3,.

The sort call on line 16 sorts all the numbers in the array.
The vector constructed on line 17 takes the first three numbers from that (now sorted) array
The invocation of std::inplace_merge() on line 18 does nothing interesting.
Finally the three elements of the vector are printed.

In the future you can check this by compiling your program.
Last edited on
Yes, it looks like they want you to read the code and determine if the output is the expected (0,2,3) or if there is something that will mess up the code.

Interesting that they ask if there is a runtime error rather than just compilation error, which means you don't just need to be aware of syntax but also buffer overflow and other errors that the compiler might not catch.

I notice you are posting several of these, since they are all along the same topic please consider putting them all in a single posting-thread.
Thank you
Topic archived. No new replies allowed.