Oct 20, 2016 at 9:18am UTC
I'm trying to solve this exercise: (#4)
https://books.google.nl/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming+principle+and+practice&pg=PA754.&redir_esc=y&hl=en#v=onepage&q&f=false
The section
20.3.1 talks about this code:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#include <std_lib_facilities_4.h>
using namespace std;
//------------------------------------------------------------------------------
template <class Iterator >
Iterator high(Iterator first, Iterator last)
// return an iterator to the element in [first:last) that has the highest value
{
Iterator high = first;
for (Iterator p = first; p != last; ++p)
if (*high<*p) high = p;
return high;
}
//------------------------------------------------------------------------------
double * get_from_jack(int * count); // jack puts doubles into an array
// and returns the number of elements
// in *count
vector<double >* get_from_jill(); // Jill fills the vector
//------------------------------------------------------------------------------
void fct()
{
int jack_count = 0;
double * jack_data = get_from_jack(&jack_count);
vector<double >* jill_data = get_from_jill();
double * jack_high = high(jack_data, jack_data + jack_count);
vector<double >& v = *jill_data;
double * jill_high = high(&v[0], &v[0] + v.size());
cout << "Jill's high " << *jill_high << "; Jack's high " << *jack_high << endl;
// ...
delete [] jack_data;
delete jill_data;
}
//------------------------------------------------------------------------------
int main()
{
fct();
system("pause" );
return 0;
}
//------------------------------------------------------------------------------
double * get_from_jack(int * count)
{
if (!count)
return 0;
const int n = 10;
double * arr = new double [n];
if (arr)
{
*count = n;
for (int i = 0; i < n; ++i)
arr[i] = i;
}
return arr;
}
//------------------------------------------------------------------------
vector<double >* get_from_jill()
{
const int n = 10;
vector<double >* arr = new vector<double >(n);
if (arr)
{
for (int i = 0; i < n; ++i)
(*arr)[i] = i;
}
return arr;
}
According to the comments of the two declared functions (line 18 through 21) I defined them like above.
Now, what errors are there in your opinions? And what STL techniques are useful for fixing them please?
Last edited on Oct 20, 2016 at 9:20am UTC
Oct 20, 2016 at 10:59am UTC
I'm curious - why do you need to dynamically allocate the vector in the get_from_jill function? Is there a reason you don't want to just return the vector?
Oct 22, 2016 at 8:37am UTC
Thanks for the reply.
The lines 18 and 21 are part of the book themselves and we should observe them.
According to them, I wrote their definitions (in lines 55 to 73 and 77 to 90).
The code is correct and works fine. But I don't know what errors the author (Stroustrup) meant that can be fixed by STL techniques!