http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
Chapter 8 // Exercise 3
3. Create a vector of Fibonacci numbers and print them using the function from exercise 2. To create the vector, write a function, fibonacci(x,y,v,n), where integers x and y are ints, v is an empty vector<int>, and n is the number of elements to put into v; v[0] will be x and v[1] will be y. A Fibonacci number is one that is part of a sequence where each element is the sum of the previous ones. For example, starting with 1 and 2, we get 1,2,3,4,8,13,21,....Your fibonacci() function should make such a sequence starting with its x and y arguments.
For this one I changed the name of few things to make them more descriptive. I also decided to not make the ints references and instead allow the user to pass numbers directly as arguments. The vector is just passed by reference in fibonacci() due to the need to modify it.
For this one I changed the name of few things to make them more descriptive. I also decided to not make the ints references and instead allow the user to pass numbers directly as arguments. The vector is just passed by reference in fibonacci() due to the need to modify it.
#include "stdafx.h" #include "std_lib_facilities.h" //prints a given vector to the screen with a label void print(string& label, const vector<int>& v) { cout << label << ": " << endl; for (int i = 0; i < v.size(); ++i) cout << v[i] << endl; cout << '\n'; } //calculates fibonnaci sequence for a given amount of numbers void fibonnaci(int first, int second, vector<int>& v, int howMany) { //is vector empty? if (v.size() == 0) { v.push_back(first); v.push_back(second); int temp; //pushback numbers from sequence depending on how many we want //start with 3rd number for (int i = 1; i < howMany; ++i) { temp = v[i] + v[i - 1]; v.push_back(temp); } } else cout << "Sorry that vector is not empty.\n"; } int main() { vector<int> fibonacciNumbers; //populate vector with sequence fibonnaci(1, 2, fibonacciNumbers, 10); //print the vector string label = "Fibonacci Numbers"; print(label, fibonacciNumbers); keep_window_open(); return 0; }
Chapter 8 // Exercise 4
4. An int can hold integers only up to a maximum number. Find approximation of that maximum number by using fibonacci().
#include "stdafx.h" #include "std_lib_facilities.h" //prints a given vector to the screen with a label void print(string& label, const vector<int>& v) { cout << label << ": " << endl; for (int i = 0; i < v.size(); ++i) { //if incrementor is divisible by 10, start a new line to print cout << v[i] << '\t'; if (i % 10 == 0 && i != 0) cout << '\n'; } cout << '\n'; } //calculates fibonnaci sequence for a given amount of numbers void fibonnaci(int first, int second, vector<int>& v, int howMany) { //is vector empty? if (v.size() == 0) { v.push_back(first); v.push_back(second); int temp; //pushback numbers from sequence depending on how many we want //start with 3rd number for (int i = 1; i < howMany; ++i) { temp = v[i] + v[i - 1]; v.push_back(temp); } } else cout << "Sorry that vector is not empty.\n"; } int main() { vector<int> fibonacciNumbers; //populate vector with sequence fibonnaci(1, 2, fibonacciNumbers, 10); //print the vector string label = "Fibonacci Numbers"; print(label, fibonacciNumbers); //after printing 100 numbers this is highest number it can go to int number = 1836311903; cout << '\n' << number; keep_window_open(); return 0; }Here I added a couple of lines in print to print numbers in rows of 10. When told to find 100 numbers the number before everything goes weird is 1836311903.
No comments:
Post a Comment