Advertisement
SmellyBadger

Untitled

Nov 1st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. // Purpose: Learn about Function (Pass by value/reference)
  2. //
  3. // Inputs: The inputs for this program are two positive whole
  4. // numbers. Those two numbers present two different
  5. // positions in Fibonacci series
  6. //
  7. // Outputs: Fibonacci numbers between those two positions.
  8. // There are some more tasks also. Find the detail
  9. // description
  10. //----------------------------------------------------------------
  11.  
  12. #include <iostream>
  13. using namespace std;
  14.  
  15.  
  16. //Do_0: Declare the function prototype
  17. // One is shown as example
  18. bool isValid(int, int);
  19. void printFibonacci(int, int);
  20. void swapNums(int &, int &);
  21. int findMax(int, int);
  22. float divide(int, int);
  23. int main()
  24. {
  25. //Do_1: Declare the necessary variables.
  26. // A few are shown as example.
  27. int num1 = 0, num2 = 0;
  28. bool isValidPos = false;
  29. int max = 0;
  30. float divResult = 0.0;
  31. bool isValidNums = true;
  32. //int pos1; delete this if not needed
  33. //int pos2; delete this if not needed
  34. //Do_2: Prompt the user for two numbers (start and end positions)
  35. cout << "Enter two numbers: " << endl;
  36. cin >> num1;
  37. cin >> num2;
  38. //Do_3: Check whether the first number
  39. // is less than the second one or not.
  40. // if not print an error message.
  41. // You must use this function protype:
  42. // bool isValid(int, int);
  43. // Call the functiontion here and store its value
  44. // in a variable called isValidNums. Then write the if
  45. // block to print the error message
  46. bool isValid(int, int);
  47. isValidNums = isValid(int, int);
  48. if (num1 > num2)
  49. {
  50. cout << "Check the numbers. First number should be less than the second number. " << endl;
  51.  
  52. }
  53.  
  54. else
  55. {
  56. cout << "The numbers entered are as follows: " << endl;
  57. cout << "Num1 = " << num1 << endl;
  58. cout << "Num2 = " << num2 << endl;
  59.  
  60. cout << endl;
  61. //Do_4: If the numbers are valid then print
  62. // the fibonacci numbers in that range
  63. // You should start counting from 0.
  64. // You must use this function prototype
  65. // void printFibonacci(int, int)
  66. // Call the function after this cout statement but before cout << endl
  67. cout << "The fibonacci numbers between position "
  68. << num1 << " and " << num2 << " are as follows: " << endl;
  69. printFibonacci(int, int);
  70. cout << endl;
  71.  
  72. //Do_5: Now Swap the numbers in place.
  73. // That is using reference of num1 and num2
  74. // You must use this function prototype
  75. // void swapNums(int, int)
  76. // Call the function before the following cout statements
  77. cout << "After swapping, the numbers are as follows: " << endl;
  78. cout << "Num1 = " << num1 << endl;
  79. cout << "Num2 = " << num2 << endl;
  80.  
  81. cout << endl;
  82.  
  83. //Do_6: Now find the maximum of the two numbers
  84. // You must use this function prototypes
  85. // int findMax(int, int)
  86. // float divide(int, int)
  87. // Call the function before the cout statement
  88. // and assign the return value to a variable max
  89. cout << "Maximum of the two numbers = " << max << endl;
  90.  
  91. cout << endl;
  92.  
  93. //Do_7: If the smaller number is 0, then print an error message.
  94. // Otherwise, divide the maximum number by the smaller number
  95. // You must use this function prototypes
  96. // float divide(int, int)
  97.  
  98. }
  99. return 0;
  100. }
  101.  
  102. //Do_8: if the first number is less
  103. // than the second number return true
  104. // otherwise return false.
  105. // Params: (in, in)
  106. bool isValid(int n1, int n2)
  107. {
  108. if (num1 < num2)
  109. cout <<
  110. return true;
  111. else
  112. {
  113. cout << "First number should be less than the second number. " << endl;
  114. return false;
  115. }
  116. }
  117.  
  118. //Do_9: Write the function to print the fibonacci numbers
  119. //Params: (in, in)
  120. void printFibonacci(int Pos1, int pos2)
  121. {
  122. //Do_10: Declare the local variables
  123. int posCounter = 0, next, first = 0, second = 1;
  124.  
  125. //Do_11: There are three Special cases.
  126. // 1) If the first position is 0 print 0
  127. // for the fibonacci number at position 0.
  128. // 2) If the second position is 1 print 1
  129. // for the at position 1.
  130. // 3) if the first position is 0 and the second position is 1
  131. // print 1 for position 1.
  132. // (One special case is shown as example)
  133. if (Pos1 == 0)
  134. {
  135. cout << "Position " << 0 << ": " << first << endl;
  136. }
  137.  
  138.  
  139. //Do_12: Starting from second position loop through position pos2.
  140. // and print the fibonacci number for each position.
  141. // The details of fibonacci series can be found in
  142. // the lab description. Fill out the following while loop
  143. // to print the fibonacci number.
  144. posCounter = 2;
  145. while ( )
  146. {
  147.  
  148.  
  149.  
  150.  
  151.  
  152. if (posCounter >= Pos1)
  153. {
  154. cout << "Position " << posCounter << ": " << next << endl;
  155. }
  156.  
  157. posCounter++;
  158. }
  159. return;
  160. }
  161.  
  162. //Do_13: Function to swap two numbers
  163. // Params: (inout, inout)
  164. void swapNums(int &n1, int &n2)
  165. {
  166.  
  167. return;
  168. }
  169.  
  170. //Do_14: Function to find the maximum of two numbers
  171. // Params: (in, in)
  172. int findMax(int n1, int n2)
  173. {
  174.  
  175. }
  176.  
  177. //Do_15: Function to divide two integers.
  178. // But you need to return the actual result in float
  179. // Params: (in, in)
  180. float divide(int n1, int n2)
  181. {
  182. float result = 0.0;
  183.  
  184.  
  185. return result;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement