Created
December 17, 2017 22:15
-
-
Save DragonOsman/a1cdfa30fcc9aa25d3c8fbd330f9fced 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 | |
// 12 / 18 / 2017 | |
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition | |
// Chapter 21 Drill "More vector use" | |
// Drill Specifications: | |
/** | |
* 1. Read some floating-point values (at least 16 values) from a file into a | |
* vector<double> called vd . | |
* 2. Output vd to cout . | |
* 3. Make a vector vi of type vector<int> with the same number of elements | |
* as vd ; copy the elements from vd into vi . | |
* 4. Output the pairs of ( vd[i] , vi[i] ) to cout , one pair per line. | |
* 5. Output the sum of the elements of vd . | |
* 6. Output the difference between the sum of the elements of vd and the sum | |
* of the elements of vi . | |
* 7. There is a standard library algorithm called reverse that takes a sequence | |
* (pair of iterators) as arguments; reverse vd , and output vd to cout . | |
* 8. Compute the mean value of the elements in vd ; output it. | |
* 9. Make a new vector<double> called vd2 and copy all elements of vd with | |
* values lower than (less than) the mean into vd2 . | |
* 10. Sort vd ; output it again. | |
*/ | |
#include <stdexcept> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> | |
#include "../../cust_std_lib_facilities.h" | |
std::vector<double> fill_vector(std::istream &is); | |
int main() | |
{ | |
using std::vector; | |
using std::string; | |
using std::ifstream; | |
using std::cin; | |
using std::cout; | |
cout << "Please enter filename: "; | |
std::string filename; | |
cin >> filename; | |
ifstream ifs{ filename }; | |
vector<double> vd; | |
try | |
{ | |
if (ifs) | |
{ | |
vd = fill_vector(ifs); | |
for (const auto &d : vd) | |
{ | |
std::cout << d << '\n'; | |
} | |
} | |
else | |
{ | |
error("Thrown in main(), line 57: can't open file ", filename); | |
} | |
} | |
catch (const std::runtime_error &e) | |
{ | |
std::cerr << "Caught in main(), line 63; " << e.what() << '\n'; | |
} | |
try | |
{ | |
if (!vd.empty()) | |
{ | |
vector<int> vi; | |
std::copy(vd.begin(), vd.end(), vi.begin()); | |
for (auto iter_double = vd.begin(); iter_double != vd.end(); ++iter_double) | |
{ | |
for (auto iter_int = vi.begin(); iter_int != vi.end(); ++iter_int) | |
{ | |
cout << '(' << *iter_double << ',' << *iter_int << ")\n"; | |
} | |
} | |
} | |
} | |
catch (const std::exception &e) | |
{ | |
std::cerr << "Caught in main() on line 83: " << e.what() << '\n'; | |
} | |
catch (...) | |
{ | |
std::cerr << "Structured exception caught on line 87\n"; | |
} | |
keep_window_open(); | |
} | |
std::vector<double> fill_vector(std::istream &is) | |
{ | |
using std::vector; | |
vector<double> vec; | |
for (double val; is >> val;) | |
{ | |
vec.push_back(val); | |
} | |
return vec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment