240 Spring 2016 Program #4 100 Points Deadline: Wednesday, Apri at 12 55 PM asic
ID: 3688676 • Letter: 2
Question
240 Spring 2016 Program #4 100 Points Deadline: Wednesday, Apri at 12 55 PM asic recursion is a technique that, for many, takes time ahd practice to master. In this rogram, you are to create a menu-driven program that ailows the user to execute one of everal recursive routines as many times as desired. The menu must list the routines, accept he user's selection, and then periorm the chosen recursive routine. The menu must also list an option to quit the program. Use a character to accept the menu choice and error trap for bad menú selections. Also print welcoming and goodbye messages at the start/end of the program Description of the recursive routines: i. Array Totaler: Given an integer array of size 100 (Filled with numbers read in from a file called "numbers.txt", which is assumed to have 100 integer values), do the following: A) display the array to the screen (use loops if you want and print 10 rows of 10 numbers each), B) ask the user how many numbers they would like totaled (error trap accepting only numbers between 1 and 100), C) Traverse the array recursively starting at the first eiement in the array and display each element in the array up to the number the user entered, and D) display the total of all the elements traversed. 2. Number Mirror: Ask the user to enter a non-negative integer (error trap for bad input). Then display the number with the digits reversed. For example, if the user entered 3456, the reversed number should be 6543. 3. GCD (Greatest Common Divisor) Finder. Implement Euclid's Algorithm, described below. Euclid's Algorithm: Euclid's Elements (Book VIil) we find a way of calculating the GCD of two numbers, without In listing the divisors of either number 8 the divisors of either number. It is now called Euclid's Algorithm. First, an example: Find the GCD of 36 and 15. Divide 36 by 15 (the greater by the smaller), getting 2 with a remainder of 6. Then we divide 15 by 6 (the previou 3. Then we divide 6 by 3 (the previous zero remainder (3) is our GCD. Here it is in general: a/b gives a remainder of r býr gives a remainder of s r/s gives a remainder of t s remainder) and we get 2 and a remainder of h no remainder. The last non- remainder) and we get 2 wit wjx gives a remainder of y x/y gives no remainder In this case, y is the GCD of a and b. if the first step produced no remainder the two numbers) is the GCD. then b (the lesser of 4. Rectangle e Printer: Write a recursive function that draws a rectangle to the screen. Ask the user to enter positive integers for the height and width of the rectangle (error trap for bad ask the user for a character to use to print the rectangle (no error trap required). rectangle to the screen. No loops are allowed when printing the rectangle. input). Then Then print theExplanation / Answer
I did two parts:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int gcd(int u, int v)
{
return (v != 0) ? gcd(v, u % v) : u;
}
int mirror(int n)
{
int rev=0;
while(n != 0) {
int remainder = n%10;
rev = rev*10 + remainder;
n/=10;
}
return rev;
}
int main()
{
char ch;
int input,num1,num2;
do{
cout << " WELCOME ";
cout << "Choose your Option : ";
cout << "1: GCD () 2: Number Mirror () 3: Exit () ";
cout << "Enter your choice :";
cin >> input;
switch(input)
{
case 1:cout << "Enter two numbers :";
cin >> num1 >> num2;
cout << "GCD of " << num1 << " and " << num2 << " is : " << gcd(num1,num2) << " ";
break;
case 2:cout << "Enter a NUMBER :";
cin >> num1;
cout << "Mirror of the number " << num1 << " is :" << mirror(num1) << " ";
break;
case 3: exit(0);
default : cout << "BAD input : ";
}
cout << "Do you want to continue [Y/y] : ";
cin >> ch;
}while(ch == 'y' || ch == 'Y');
cout << " GOOD BYE ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.