Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Purpose: Learn about Function (Pass by value/reference)
- //
- // Inputs: The inputs for this program are two positive whole
- // numbers. Those two numbers present two different
- // positions in Fibonacci series
- //
- // Outputs: Fibonacci numbers between those two positions.
- // There are some more tasks also. Find the detail
- // description
- //----------------------------------------------------------------
- #include <iostream>
- using namespace std;
- //Do_0: Declare the function prototype
- // One is shown as example
- bool isValid(int, int);
- void printFibonacci(int, int);
- void swapNums(int &, int &);
- int findMax(int, int);
- float divide(int, int);
- int main()
- {
- //Do_1: Declare the necessary variables.
- // A few are shown as example.
- int num1 = 0, num2 = 0;
- bool isValidPos = false;
- int max = 0;
- float divResult = 0.0;
- bool isValidNums = true;
- //int pos1; delete this if not needed
- //int pos2; delete this if not needed
- //Do_2: Prompt the user for two numbers (start and end positions)
- cout << "Enter two numbers: " << endl;
- cin >> num1;
- cin >> num2;
- //Do_3: Check whether the first number
- // is less than the second one or not.
- // if not print an error message.
- // You must use this function protype:
- // bool isValid(int, int);
- // Call the functiontion here and store its value
- // in a variable called isValidNums. Then write the if
- // block to print the error message
- bool isValid(int, int);
- isValidNums = isValid(int, int);
- if (num1 > num2)
- {
- cout << "Check the numbers. First number should be less than the second number. " << endl;
- }
- else
- {
- cout << "The numbers entered are as follows: " << endl;
- cout << "Num1 = " << num1 << endl;
- cout << "Num2 = " << num2 << endl;
- cout << endl;
- //Do_4: If the numbers are valid then print
- // the fibonacci numbers in that range
- // You should start counting from 0.
- // You must use this function prototype
- // void printFibonacci(int, int)
- // Call the function after this cout statement but before cout << endl
- cout << "The fibonacci numbers between position "
- << num1 << " and " << num2 << " are as follows: " << endl;
- printFibonacci(int, int);
- cout << endl;
- //Do_5: Now Swap the numbers in place.
- // That is using reference of num1 and num2
- // You must use this function prototype
- // void swapNums(int, int)
- // Call the function before the following cout statements
- cout << "After swapping, the numbers are as follows: " << endl;
- cout << "Num1 = " << num1 << endl;
- cout << "Num2 = " << num2 << endl;
- cout << endl;
- //Do_6: Now find the maximum of the two numbers
- // You must use this function prototypes
- // int findMax(int, int)
- // float divide(int, int)
- // Call the function before the cout statement
- // and assign the return value to a variable max
- cout << "Maximum of the two numbers = " << max << endl;
- cout << endl;
- //Do_7: If the smaller number is 0, then print an error message.
- // Otherwise, divide the maximum number by the smaller number
- // You must use this function prototypes
- // float divide(int, int)
- }
- return 0;
- }
- //Do_8: if the first number is less
- // than the second number return true
- // otherwise return false.
- // Params: (in, in)
- bool isValid(int n1, int n2)
- {
- if (num1 < num2)
- cout <<
- return true;
- else
- {
- cout << "First number should be less than the second number. " << endl;
- return false;
- }
- }
- //Do_9: Write the function to print the fibonacci numbers
- //Params: (in, in)
- void printFibonacci(int Pos1, int pos2)
- {
- //Do_10: Declare the local variables
- int posCounter = 0, next, first = 0, second = 1;
- //Do_11: There are three Special cases.
- // 1) If the first position is 0 print 0
- // for the fibonacci number at position 0.
- // 2) If the second position is 1 print 1
- // for the at position 1.
- // 3) if the first position is 0 and the second position is 1
- // print 1 for position 1.
- // (One special case is shown as example)
- if (Pos1 == 0)
- {
- cout << "Position " << 0 << ": " << first << endl;
- }
- //Do_12: Starting from second position loop through position pos2.
- // and print the fibonacci number for each position.
- // The details of fibonacci series can be found in
- // the lab description. Fill out the following while loop
- // to print the fibonacci number.
- posCounter = 2;
- while ( )
- {
- if (posCounter >= Pos1)
- {
- cout << "Position " << posCounter << ": " << next << endl;
- }
- posCounter++;
- }
- return;
- }
- //Do_13: Function to swap two numbers
- // Params: (inout, inout)
- void swapNums(int &n1, int &n2)
- {
- return;
- }
- //Do_14: Function to find the maximum of two numbers
- // Params: (in, in)
- int findMax(int n1, int n2)
- {
- }
- //Do_15: Function to divide two integers.
- // But you need to return the actual result in float
- // Params: (in, in)
- float divide(int n1, int n2)
- {
- float result = 0.0;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement