Fibonacci Sequence: Dividing Vector HELP!

This is a discussion on Fibonacci Sequence: Dividing Vector HELP! within the C++ Programming forums, part of the General Programming Boards category; Hello! I have been working on the this code to output a fibonacci sequence: The user inputs the first two ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    Fibonacci Sequence: Dividing Vector HELP!

    Hello!
    I have been working on the this code to output a fibonacci sequence:
    The user inputs the first two numbers followed by how many times the numbers will be added.
    I want to take the last two additions of the sequence and divide the last by the second to last.
    As an example, a valid input is 1 3 5 and the output would be 1,3,4,7,11.
    I want to divide the 7 by 11 and output that result. (the result should always end up being around 1.6) Thats the part I am having trouble on, where would I call the vectors of n and n-1 and how would I output them? This is the code (the header file is attached as well)

    As you can probably tell; im new to the forum and I hope i attached everything in the proper format. I apologize if I didnt...
    Thank you for your help!!!

    Code:
    #include "std_lib_facilities.h"	
    
    void print(vector<int>& v, const string& name)
    {
    	cout << name << ": (" << v.size() << ") { ";
    	for (int i = 0; i<v.size(); ++i) {
      
      cout << v[i];
      
      if (i<v.size()-1) cout << ", ";	
       }
    	cout << " }\n";
    	
    }
    
    void fibonacci(int x, int y, vector<int>& v, int n)
    	{
      if (n<1) return;
    	if (1<=n) v.push_back(x);
    	if (1<=2) v.push_back(y);
    
    	for (int i=2; i<n; ++i) {	
      int z = x+y;	// next element
      v.push_back(z);
      x = y;	// move the sequence on
      y = z;
      
    	}
    }
    
    
    int main()
    try
    {
    	cout<< "please enter two integer values (to be used to start a Fibinacci series) and the number of elements of the series: ";
    	int val1 = 0;
    	int val2 = 0;
    	int n;
    	while (cin>>val1>>val2>>n) {	// read two integers
      vector<int> vf;
      fibonacci(val1,val2,vf,n);
      cout << "fibonnaci(" << val1 << "," << val2 << ") ";
      print(vf,"");
    	
      cout << "The value of the quotient between the last n and n-1 is: "<< "\n";
      cout << "Try again: ";
    	}
    }
    catch (runtime_error e) {	
    	cout << e.what() << '\n';
    	}
    catch (...) {	
    	cout << "exiting\n";
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    The user inputs the first two numbers followed by how many times the numbers will be added. ... As an example, a valid input is 1 3 5 and the output would be 1,3,4,7,11.
    In your example, according to your output, only three additions actually took place. Do you want:

    A) 5 additions to take place or
    B) Do you want to stop once your reach the 5th element of your sequence?
    There is a difference between the two.

    Either way, you don't need a vector for what you're trying to do; you just need to keep track of the last two values. Try something like this (warning: no error checking and minimal debugging, assumes you want choice A, but modifying this for choice B should be easy)

    Code:
    int n1 = // Get from user
    int n2 = // Get from user
    int n3 = // Get from user
    
    // We add two values every time
    for (int i = 0; i < n3 - 2; i += 2) {
            n1 += n2 ;
            n2 += n1 ;    
    }
    
    // Take care of the last values    
    n1 += n2 ;
        
    float f1 = 0.0 ;
        
    if (n3 & 1) { // If n3 is odd
            f1 = (float) n1 / n2 ;
    } else { // If n3 is even
            n2 += n1 ;
            f1 = (float) n2 / n1 ;
    }
    
    return f1 ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fibonacci sequence
    By paut in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2011, 12:27 PM
  2. Fibonacci Sequence
    By iGuardian in forum C++ Programming
    Replies: 19
    Last Post: 10-21-2011, 01:34 AM
  3. Fibonacci sequence in c++
    By anytime in forum C++ Programming
    Replies: 50
    Last Post: 02-06-2011, 11:40 PM
  4. Fibonacci sequence
    By MuffanMan123 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2008, 09:15 AM
  5. Fibonacci sequence
    By Squirrelly in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2001, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21