C++ Recursion Practice!
1)Multiplication
#include <iostream.h>
int Multiply(int M, int N)
//Performs multiplication using the + operator.
//Pre : M and N are defined and N > 0.
//Post: Returns M x N
{
int Prod;
if (N == 1)
Prod = M; //base case
else
Prod = M + Multiply(M, N - 1); //recursive step
return Prod;
}
2) Reverse
#include <iostream.h>
void Reverse(int N)
//Displays string of length N in the reverse order
//Pre : N >= 1.
//Post: Displays N characters.
{
char Next; //next data character
if (N <= 1)
{ //base case
cin >> Next;
cout << Next;
}
else
{ //recursive step
cin >> Next;
Reverse(N - 1);
cout << Next;
}
}
3) Complete the following recursive function that calculates the
value of a number (Base) raised to a power (Power).
Assume that Power is positive, i.e Power>=1
int PowerRaiser (int Base, int Power)
{
int Prod;
if (Power == ______)
Prod = ________;
else
Prod = ________ * ________;
return Prod;
}
4) What is the output of the following program?
What does function Strange compute?
int Strange (int N)
{
int Res;
if (N == 1) then
Res = 0;
else
Res = 1 + Strange(N % 2);
return Res;
}
void main()
{
cout << Strange(8) << "\n"
}
5) recursive sum
int FindSum(int X[], int N)
//Finds sum of values in array X elements 0..N - 1.
//Pre : Array X is defined and N >= 0.
//Post: Returns sum of first N elements of X,
{
int Sum;
if (N == 0)
Sum = 0;
else
Sum = X[N - 1] + FindSum(X, N - 1);
return Sum;
}
void main()
//Tests function FindSum.
{
const MaxIndex = 20;
int Num;
int X[MaxIndex];
Num = 3;
X[0] = 5;
X[1] = 10;
X[2] = -7;
cout << "The array sum is " << FindSum(X, Num) << "\n";
}
Ans 1
#include<iostream>
using namespace std;
int Multiply(int M, int N)
//Performs multiplication using the + operator.
//Pre : M and N are defined and N > 0.
//Post: Returns M x N
{
int Prod;
if (N == 1)
Prod = M; //base case
else
Prod = M + Multiply(M, N - 1); //recursive step
return Prod;
}
int main()
{
cout<<Multiply(5,6);
return 0;
}
=====================================================================================
Output
=====================================================================================
Ans 2
#include<iostream>
using namespace std;
void Reverse(int N)
//Displays string of length N in the reverse order
//Pre : N >= 1.
//Post: Displays N characters.
{
char Next; //next data character
if (N <= 1)
{ //base case
cin >> Next;
cout << Next;
}
else
{ //recursive step
cin >> Next;
Reverse(N - 1);
cout << Next;
}
}
int main()
{
Reverse(5);
return 0;
}
=====================================================================================
Output
=====================================================================================
Ans 3:
#include<iostream>
using namespace std;
/*
Complete the following recursive function that calculates the
value of a number (Base) raised to a power (Power).
Assume that Power is positive, i.e Power>=1
*/
int PowerRaiser (int Base, int Power)
{
int Prod;
if (Power == 1)
Prod = Base;
else
Prod = Base *PowerRaiser(Base,Power-1);
return Prod;
}
int main()
{
cout<<PowerRaiser(5,1)<<endl;
cout<<PowerRaiser(2,6)<<endl;
return 0;
}
=====================================================================================
Output
=====================================================================================
4) What is the output of the following program?
What does function Strange compute?
int Strange (int N)
{
int Res;
if (N == 1) then
Res = 0;
else
Res = 1 + Strange(N % 2);
return Res;
}
void main()
{
cout << Strange(8) << "\n";
}
=====================================================================================
Output
it is an infinite loop.
because of
Strange(8) = 1+ Strange(0) = 1+1+Strange(0) = 1+1+1+Strange(0)......................
for even number it will go in the infinite loop and nothing will be printed on the console.
=====================================================================================
Ans 5:
#include<iostream>
using namespace std;
int FindSum(int X[], int N)
//Finds sum of values in array X elements 0..N - 1.
//Pre : Array X is defined and N >= 0.
//Post: Returns sum of first N elements of X,
{
int Sum;
if (N == 0)
Sum = 0;
else
Sum = X[N - 1] + FindSum(X, N - 1);
return Sum;
}
int main()
{
const int MaxIndex = 20;
int Num;
int X[MaxIndex];
Num = 3;
X[0] = 5;
X[1] = 10;
X[2] = -7;
cout << "The array sum is " << FindSum(X, Num) << "\n";
return 0;
}
=====================================================================================
Output
C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...
C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...
c++ #include using namespace std; int main() { int n, x, num, j = 0; cin >> n >> x; int*arr = new int[n]; for (int i = 0; i < n; i++) { cin >> num; if (num < x) { arr[j] = num; j++; } } for (int i = 0; i < j; i++) {...
Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...
howthe output of the following 4 program segments (a) const int SIZE=8; int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8}; int index; index=0; res = values[index]; for (int j=1; j<SIZE; j++) { if (values[j] > res) { res = values[j]; index = j; cout << index << res << endl; } } cout <<...
LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...
QUESTION 26 Given the following function: int secret(int num, int m) inti, prod=1; if (m=0) return 1: - for (i=0; i<m; i++) { prod = prod * num; } return prod; What is the output for this function call? cout << secret(10,6);
Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1]; } void mergeSort(int arr[], int start, int end){ //base case: down to 1 item, do nothing //recursive case: //MergeSort(left) //MergeSort(right) //Merge(left, right) int m = (end - start) / 2; if(start==end){ } else { mergeSort(arr, start, m); mergeSort(arr, m+1, end); merge(arr, start, m, m+1, end); } } void fill(int arr[],...
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...
PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...