Created
November 17, 2017 21:31
-
-
Save DragonOsman/99ed59d180550d75b397c9e356a3d683 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Osman Zakir | |
// 11 / 10 / 2017 | |
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition | |
// Chapter 20 "Find highest element" example code (with templated high() function) | |
// This program has a serious bug that I have to find and fix. | |
#include "../../cust_std_lib_facilities.h" | |
#include <algorithm> | |
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <vld.h> | |
template<typename Iterator> | |
Iterator high(Iterator first, Iterator last); | |
double *get_from_jack(int *count); | |
std::vector<double> *get_from_jill(); | |
void fct(); | |
int main() | |
{ | |
try | |
{ | |
fct(); | |
} | |
catch (const std::exception &e) | |
{ | |
std::cerr << "Exception: " << e.what() << '\n'; | |
} | |
keep_window_open(); | |
} | |
template<typename 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) | |
{ | |
*count = 45; | |
double *data_arr = nullptr; | |
try | |
{ | |
data_arr = new double[*count]; | |
} | |
catch (const std::bad_alloc &e) | |
{ | |
std::cout << "Bad allocation error: " << e.what() << '\n'; | |
if (data_arr != nullptr) | |
{ | |
delete[] data_arr; | |
data_arr = nullptr; | |
} | |
} | |
catch (const std::bad_array_new_length &e) | |
{ | |
std::cerr << "Bad array new length error: " << e.what() << '\n'; | |
delete[] data_arr; | |
data_arr = nullptr; | |
} | |
catch (const std::exception &e) | |
{ | |
std::cout << "Exception: " << e.what() << '\n'; | |
delete[] data_arr; | |
data_arr = nullptr; | |
} | |
std::ifstream ifs{ "velocities1.txt" }; | |
std::size_t i = 0; | |
for (double velocity; ifs >> velocity;) | |
{ | |
data_arr[i] = velocity; | |
++i; | |
if (i >= *count) | |
{ | |
break; | |
} | |
} | |
return data_arr; | |
} | |
std::vector<double> *get_from_jill() | |
{ | |
std::vector<double> *data_vec = nullptr; | |
try | |
{ | |
data_vec = new std::vector<double>; | |
} | |
catch (const std::bad_alloc &e) | |
{ | |
std::cerr << "Bad alloction error: " << e.what() << '\n'; | |
if (data_vec != nullptr) | |
{ | |
delete data_vec; | |
data_vec = nullptr; | |
} | |
} | |
std::ifstream ifs{ "velocities2.txt" }; | |
for (double velocity; ifs >> velocity;) | |
{ | |
data_vec->push_back(velocity); | |
} | |
return data_vec; | |
} | |
void fct() | |
{ | |
int jack_count = 0; | |
double *jack_data = get_from_jack(&jack_count); | |
std::vector<double> *jill_data = get_from_jill(); | |
std::sort(jack_data, jack_data + jack_count); | |
std::sort(&jill_data->begin(), &jill_data->end()); | |
double *jack_high = high(jack_data, jack_data + jack_count); | |
std::vector<double> &v = *jill_data; | |
double *jill_high = high(&v[0], &v[0] + v.size()); | |
std::cout << "Jill's high " << *jill_high << "; Jack's high " << *jack_high << '\n'; | |
if (jack_data != nullptr) | |
{ | |
delete[] jack_data; | |
jack_data = nullptr; | |
} | |
if (jill_data != nullptr) | |
{ | |
delete jill_data; | |
jill_data = nullptr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment