Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- *
- * @author Francisco, Graham
- * @assignment ICS 212 Assignment 10
- * @date 2/19/2019
- */
- #include <stdio.h>
- #include "getdouble.h" //allows the program to use getdouble() function
- /*
- Recursively displays all the numbers in sorted order from the first parameter
- to the second parameter. The recursive method, prints the first parameter,
- increments it and then prints it again until the base case is reached.
- */
- void recursiveCount(int first, int second) {
- if (first <= second) {
- printf("%d ", first);
- recursiveCount(first + 1, second);
- }
- }
- /*
- Recursively adds all the numbers from the first parameter to the second
- parameter. The recursive method adds the first parameter with an incremented
- first parameter until it reaches the base case (first > second).
- */
- int recursiveAdd(int first, int second) {
- if (first > second) {
- return 0; // returns 0, so that it doesn't affect result at the end
- } else {
- return first + recursiveAdd(first + 1, second);
- }
- }
- /*
- Recursively multiplies all the numbers from the first parameter to the second
- parameter. The recursive method multiplies the first parameter with an
- incremented first parameter until it reaches the base case (first > second).
- */
- int recursiveMultiply(int first, int second) {
- if (first > second) {
- return 1; // returns 1, so that it doesn't affect result at the end
- } else {
- return first * recursiveMultiply(first + 1, second);
- }
- }
- /*
- Recursively multiplies the first parameter by itself. The number of times it's
- gonna multiply itself is determined by the second parameter.
- */
- int recursivePower(int first, int second) {
- if (second < 1) {
- return 1; // returns 1, signifying that first has been multiplied enough times
- } else {
- return first * recursivePower(first, second - 1);
- }
- }
- /*
- Recursively calculates the Greatest Commond Divisor of the first and second
- parameter. The recursive method utilizes the "Euclidean algorithm" where it
- does modular arithmetic until first mod second = 0, then returns second.
- */
- int recursiveGcd(int first, int second) {
- if (first % second == 0) {
- return second;
- } else {
- return recursiveGcd(second, first % second);
- }
- }
- /*Main function*/
- int main() {
- printf("Enter two positive integers, separated by a space, the first smaller than the second: ");
- int firstInput = (int) getdouble();
- int secondInput = (int) getdouble();
- /*
- Error Checking. If first input or second input is less than 1, or if first
- input is bigger than the second input, throw an error. Otherwise, run the
- recursive functions
- */
- if (firstInput < 1) {
- printf("ERROR: %d is not positive", firstInput);
- } else if (secondInput < 1) {
- printf("ERROR: %d is not positive", secondInput);
- } else if (firstInput > secondInput){
- printf("ERROR: %d is not smaller than %d", firstInput, secondInput);
- } else {
- /*
- for organization purposes, we assign variables for functions with
- return values
- */
- int add = recursiveAdd(firstInput, secondInput);
- int multiply = recursiveMultiply(firstInput, secondInput);
- int power = recursivePower(firstInput, secondInput);
- int gcd = recursiveGcd(firstInput, secondInput);
- //prints the recursiveCount from firstInput to secondInput
- printf("Counting from %d to %d: ", firstInput, secondInput);
- recursiveCount(firstInput, secondInput);
- //prints the recursiveAdd from firstInput to secondInput
- printf("\nThe sum of %d to %d = %d", firstInput, secondInput, add);
- //prints the recursiveMultiply from firstInput to secondInput
- printf("\nThe product of %d to %d = %d", firstInput, secondInput, multiply);
- //prints the recursivePower of firstInput and secondInput
- printf("\n%d to power of %d = %d", firstInput, secondInput, power);
- //prints the recursiveGcd of firstInput and secondInput
- printf("\nThe gcd of %d and %d = %d", firstInput, secondInput, gcd);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement