123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #include "utils.h"
- #include <limits>
- bool is_size_ok(int size) {
- if (size <= 0 || size > 1024) {
- display_message("size is invalid");
- return false;
- }
- else {
- return true;
- }
- }
- vector<u32>* fibon_seq(int size) {
- static vector<u32> elems;
- if (!is_size_ok(size)) {
- return 0;
- }
- int current_size = elems.size();
- if (size > current_size) {
- for (int ix = current_size; ix < size; ++ix) {
- if (ix < 2) {
- elems.push_back(1);
- }
- else {
- int value_ix = elems[ix - 1] + elems[ix - 2];
- elems.push_back(value_ix);
- }
- }
- }
- return &elems;
- }
- bool fibon_elem(int position, u32& elem) {
- vector<u32>* fibon_vector = fibon_seq(position);
- if (fibon_vector) {
- elem = (*fibon_vector)[position - 1]; //index overflow
- return true;
- }
- else {
- elem = 0;
- return false;
- }
- }
- int main() {
- u32 elem = 0;
- bool ret = fibon_elem(0, elem);
- if (ret) {
- cout << "elem is" << elem << endl;
- }
- display_message('s');
- display_message("test string message", 1);
- display_message("test string message", 1, 2);
- string s1("test string message 1");
- vector<u32>* v1 = fibon_seq(10);
- display(*v1);
- display_message(s1, *v1);
- return 0;
- }
|