-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.2.cpp
More file actions
47 lines (39 loc) · 1.06 KB
/
2.2.cpp
File metadata and controls
47 lines (39 loc) · 1.06 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
bool calc_elements(vector<int> &vec, int pos);
void display_elems(vector<int> &vec, const string &title, ostream &os=cout);
int main ()
{
ofstream outfile("2.2.txt", ios_base::app);
vector<int> pent;
const string title ("Pentagonal Numeric Series");
if (calc_elements (pent, 0))
display_elems(pent, title, outfile);
if (calc_elements (pent, 8))
display_elems(pent, title, outfile);
if (calc_elements (pent, 14))
display_elems(pent, title, outfile);
if (calc_elements (pent, 138))
display_elems(pent, title, outfile);
}
bool calc_elements (vector<int> &vec, int pos)
{
if (pos <=0 || pos >64)
{
cerr << "sorry. Invalid position:" << pos << endl;
return false;
}
for (int ix = vec.size()+1; ix <= pos; ++ix)
vec.push_back( (ix*(3*ix-1))/2 );
return true;
}
void display_elems (vector<int> &vec, const string &title, ostream &os)
{
os <<'\n' << title << "\n\t";
for (unsigned long ix = 0; ix < vec.size(); ++ix)
os << vec[ix] << ' ';
os << endl;
}