#include<iostream> using namespace std; #define ArraySize 15 #include <time.h> /
ID: 3863533 • Letter: #
Question
#include<iostream>
using namespace std;
#define ArraySize 15
#include <time.h>
//Prototype
double power(double base, int exponent);
int gcd(int x, int y);
int recursiveMinimum(int a[], int n);
void main() {
cout << "-----------------PROGRAM MENU----------------------" << endl;
cout << "For Recursive Exponentiation Select 1 : " << endl;
cout << "For Recursive Greatest Common Divisor Select 2 : " << endl;
cout << "For Find the Minimum Value in an array Select 3 : " << endl;
cout << "--------------------------------------------------- " << endl;
cout << "Select your Choice : ";
int choice;
cin >> choice;
cout << " ";
switch (choice)
{
case 1:
cout << "-----Recursive Exponentiation Starts now.----- " << endl;
double base;
int degree;
cout << "Enter Base :";
cin >> base;
cout << "Enter Exponent : ";
cin >> degree;
cout << "Number " << base << " raised to " << degree << " is: " << power(base, degree) << endl;
cout << endl;
break;
case 2:
cout << "----Recursive Greatest Common Divisor starts now.---- " << endl;
int number1, number2;
cout << "Enter 1st Number :";
cin >> number1;
cout << "Enter 2nd Number :";
cin >> number2;
cout << "Greatest Common Divisor of " << number1 << " & " << number2 << " is : " << gcd(number1, number2) << " " << endl;
break;
case 3:
cout << "-----Find the Minimum Value in an array starts now.----- " << endl;
int smallestNum, testArray[15];
srand(time(NULL)); //Seed Null value, We shall get every time the dynamic sequence
int i;
//Fill Array with random numbers.
for (i = 0; i < ArraySize; i++) {
testArray[i] = rand() % 500; //Generate in the range of 200 and below.
}
cout << "Array elements are: " << endl;
//Display the elements of Array filled by Random Numbers
for (i = 0; i < ArraySize; i++) {
cout << testArray[i] << " ";
}
smallestNum = recursiveMinimum(testArray, ArraySize);
cout << " Smallest value in Array is : " << smallestNum << endl;
break;
default:
cout << "Invalid Selection" << endl;
}
system("PAUSE");
}
//1:Recursive Exponentiation
double power(double base, int exponent)
{
if (exponent == 1)
return base;
else
return base * power(base, exponent - 1);
}
//2:Recursive Greatest Common Divisor
int gcd(int x, int y)
{
if (y == 0)
return x;
else return gcd(y, x % y);
}
//3:Find the Minimum Value in an array
int recursiveMinimum(int a[], int n)
{
if (n == 1)
return a[0];
n--;
return recursiveMinimum(a + (a[0] > a[n]), n);
}
Hi can u change this to from c++ to c program
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#define ArraySize 15
#include <time.h>
//Prototype
int power(int base, int exponent);
int gcd(int x, int y);
int recursiveMinimum(int a[], int n);
int main() {
printf( "-----------------PROGRAM MENU---------------------- ");
printf( "For Recursive Exponentiation Select 1 : ");
printf( "For Recursive Greatest Common Divisor Select 2 : ");
printf( "For Find the Minimum Value in an array Select 3 : ");
printf( "--------------------------------------------------- ");
printf( "Select your Choice : ");
int choice=0;
scanf("%d",&choice);
printf(" ");
switch (choice)
{
case 1:
printf( "-----Recursive Exponentiation Starts now.----- ");
int base;
int degree;
degree=0;
printf( "Enter Base :");
scanf("%d",&base);
printf( "Enter Exponent : ");
scanf("%d",°ree);
printf("Number %d raised to %d is : %d ",base,degree,power(base, degree));
printf(" ");
break;
case 2:
printf( "----Recursive Greatest Common Divisor starts now.---- ");
int number1, number2;
printf( "Enter 1st Number :");
scanf("%d",&number1);
printf( "Enter 2nd Number :");
scanf("%d",&number2);
printf("Greatest Common Divisor of %d & %d is : %d ",number1,number2,gcd(number1, number2));
break;
case 3:
printf("-----Find the Minimum Value in an array starts now.----- ");
int smallestNum, testArray[15];
srand(time(NULL)); //Seed Null value, We shall get every time the dynamic sequence
int i;
//Fill Array with random numbers.
for (i = 0; i < ArraySize; i++) {
testArray[i] = rand() % 500; //Generate in the range of 200 and below.
}
printf( "Array elements are: ");
//Display the elements of Array filled by Random Numbers
for (i = 0; i < ArraySize; i++) {
printf("%d ", testArray[i] );
}
smallestNum = recursiveMinimum(testArray, ArraySize);
printf(" Smallest value in Array is : %d ", smallestNum );
break;
default:
printf( "Invalid Selection " );
}
system("PAUSE");
}
//1:Recursive Exponentiation
int power(int base, int exponent)
{
if (exponent == 1)
return base;
else
return base * power(base, exponent - 1);
}
//2:Recursive Greatest Common Divisor
int gcd(int x, int y)
{
if (y == 0)
return x;
else return gcd(y, x % y);
}
//3:Find the Minimum Value in an array
int recursiveMinimum(int a[], int n)
{
if (n == 1)
return a[0];
n--;
return recursiveMinimum(a + (a[0] > a[n]), n);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.