Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. /**
  2.  *
  3.  *
  4.  * @author     Francisco, Graham
  5.  * @assignment ICS 212 Assignment 10
  6.  * @date       2/19/2019
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "getdouble.h"  //allows the program to use getdouble() function
  11.  
  12. /*
  13.   Recursively displays all the numbers in sorted order from the first parameter
  14.   to the second parameter. The recursive method, prints the first parameter,
  15.   increments it and then prints it again until the base case is reached.
  16. */
  17. void recursiveCount(int first, int second) {
  18.     if (first <= second) {
  19.         printf("%d ", first);
  20.         recursiveCount(first + 1, second);
  21.     }
  22. }
  23.  
  24. /*
  25.   Recursively adds all the numbers from the first parameter to the second
  26.   parameter. The recursive method adds the first parameter with an incremented
  27.   first parameter until it reaches the base case (first > second).
  28.  */
  29. int recursiveAdd(int first, int second) {
  30.     if (first > second) {
  31.         return 0;   // returns 0, so that it doesn't affect result at the end
  32.     } else {
  33.         return first + recursiveAdd(first + 1, second);
  34.     }
  35. }
  36.  
  37. /*
  38.   Recursively multiplies all the numbers from the first parameter to the second
  39.   parameter. The recursive method multiplies the first parameter with an
  40.   incremented first parameter until it reaches the base case (first > second).
  41.  */
  42. int recursiveMultiply(int first, int second) {
  43.     if (first > second) {
  44.         return 1;   // returns 1, so that it doesn't affect result at the end
  45.     } else {
  46.         return first * recursiveMultiply(first + 1, second);
  47.     }
  48. }
  49.  
  50. /*
  51.   Recursively multiplies the first parameter by itself. The number of times it's
  52.   gonna multiply itself is determined by the second parameter.
  53.  */
  54. int recursivePower(int first, int second) {
  55.     if (second < 1) {
  56.         return 1;   // returns 1, signifying that first has been multiplied enough times
  57.     } else {
  58.         return first * recursivePower(first, second - 1);
  59.     }
  60. }
  61.  
  62. /*
  63.   Recursively calculates the Greatest Commond Divisor of the first and second
  64.   parameter. The recursive method utilizes the "Euclidean algorithm" where it
  65.   does modular arithmetic until first mod second = 0, then returns second.
  66.  */
  67. int recursiveGcd(int first, int second) {
  68.     if (first % second == 0) {
  69.         return second;
  70.     } else {
  71.         return recursiveGcd(second, first % second);
  72.     }
  73. }
  74.  
  75. /*Main function*/
  76. int main() {
  77.    
  78.     printf("Enter two positive integers, separated by a space, the first smaller than the second: ");
  79.    
  80.     int firstInput = (int) getdouble();
  81.     int secondInput = (int) getdouble();
  82.  
  83.     /*
  84.       Error Checking. If first input or second input is less than 1, or if first
  85.       input is bigger than the second input, throw an error. Otherwise, run the
  86.       recursive functions
  87.      */
  88.     if (firstInput < 1) {
  89.         printf("ERROR: %d is not positive", firstInput);
  90.     } else if (secondInput < 1) {
  91.         printf("ERROR: %d is not positive", secondInput);
  92.     } else if (firstInput > secondInput){
  93.         printf("ERROR: %d is not smaller than %d", firstInput, secondInput);
  94.     } else {
  95.        
  96.         /*
  97.           for organization purposes, we assign variables for functions with
  98.           return values
  99.          */
  100.         int add = recursiveAdd(firstInput, secondInput);
  101.         int multiply = recursiveMultiply(firstInput, secondInput);
  102.         int power = recursivePower(firstInput, secondInput);
  103.         int gcd = recursiveGcd(firstInput, secondInput);
  104.        
  105.         //prints the recursiveCount from firstInput to secondInput
  106.         printf("Counting from %d to %d: ", firstInput, secondInput);
  107.         recursiveCount(firstInput, secondInput);
  108.        
  109.         //prints the recursiveAdd from firstInput to secondInput
  110.         printf("\nThe sum of %d to %d = %d", firstInput, secondInput, add);
  111.        
  112.         //prints the recursiveMultiply from firstInput to secondInput
  113.         printf("\nThe product of %d to %d = %d", firstInput, secondInput, multiply);
  114.        
  115.         //prints the recursivePower of firstInput and secondInput
  116.         printf("\n%d to power of %d = %d", firstInput, secondInput, power);
  117.        
  118.         //prints the recursiveGcd of firstInput and secondInput
  119.         printf("\nThe gcd of %d and %d = %d", firstInput, secondInput, gcd);
  120.        
  121.     }
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement