I got answers already. Can anyone help me to get a test file for the below codes
ID: 3737112 • Letter: I
Question
I got answers already. Can anyone help me to get a test file for the below codes, it will be a separate file so that i can test below codes successfully. Also it is an assignment of data structure on recursive function "You will implement and test four short recursive functions. With the proper use of recursion, none of these function should require more than a dozen lines of code. rec_fun.cxx: This file should contain the implementations of the four functions described below. You might also want to put the functions prototypes in a separate file rec_fun.h and write a test program that includes rec_fun.h."
#include "rec_fun.h" //rec_fun header file
#include <bits/stdc++.h>
using namespace std;
int main()
{
triangle(cout,3, 5); //to print traingular patter
/*------------
*binary values
*-------*/
binary_print(cout,27); //print binary value
cout<<endl;
binary_print(cout,4);//print binary value
cout<<endl;
/* power function */
cout<<pow(2,3)<<endl;
/* indented sentences function*/
indented_sentences(1,4);
return 0;
}
//power function
double pow(double x, int n)
{
if (n!= 1) //base case
return (x*pow(x, x-1));// x^n=x*pow(x,n-1)
else
return 1;// return 1
}
//print traingular pattern
void triangle(ostream& outs, unsigned int m, unsigned int n)
{
if (m>n) return; //base case
else{
for (int q = 1; q <=m; q++)
{outs << "*";} //print *
outs<< endl; //new line
//recursively call for m+1 stars line
triangle(outs,m +1, n);
//for bottom part of triangle
//print stars
for (int q = 1; q <= m; q++)
{outs << "*";}
outs << endl;
}
}
//similar to printing *s but instead of stars
//print the text
void indented_sentences(size_t m, size_t n)
{
if (m>n) return; //base case
else{
for (int q = 1; q <m; q++)
cout <<" ";//print spaces
cout<<"This was written by calling number "<<m<< endl;
//recursively call for m+1 line
//for bottom part
indented_sentences(m +1, n);
for (int q = 1; q <m; q++) //to print spaces
cout <<" ";
cout<<"This was ALSO written by calling number"<<m<<endl;
}
}
//print in binary format
void binary_print(ostream& outs, unsigned int n)
{
if(n<=1) //base case
{
outs<<n;
return;
}
//recursively call by dividing by 2
binary_print(outs,n>>1);
outs<<n%2;//print n%2
}
rec_fun.h
#include<bits/stdc++.h>
void triangle(std::ostream& outs, unsigned int num, unsigned int limit) ;
void indented_sentences(size_t m,size_t n);
void binary_print(std::ostream& outs, unsigned int n);
double pow(double x, int n);
Explanation / Answer
// File name: rec_fun.h
#ifndef _REC_FUN
#define _REC_FUN
#include<bits/stdc++.h>
// Prototype of functions
void triangle(std::ostream& outs, unsigned int num, unsigned int limit) ;
void indented_sentences(size_t m,size_t n);
void binary_print(std::ostream& outs, unsigned int n);
double pow(double x, int n);
#endif // End of header file
------------------------------------------------------------------
// File Name: rec_fun.cpp
#include"rec_fun.h"
#include<iostream>
using namespace std;
// Power function
double pow(double x, int n)
{
if (n!= 1) //base case
return (x*pow(x, x-1));// x^n=x*pow(x,n-1)
else
return 1;// return 1
}// End of function
// Print traingular pattern
void triangle(ostream& outs, unsigned int m, unsigned int n)
{
if (m > n)
return; //base case
else
{
// Loops up to m
for (int q = 1; q <= m; q++)
{
outs << "*";
} //print *
outs<< endl; //new line
// Recursively call for m+1 stars line
triangle(outs,m +1, n);
// For bottom part of triangle
// Print stars
for (int q = 1; q <= m; q++)
outs << "*";
outs << endl;
}// End of else
}// End of function
// Similar to printing *s but instead of stars
// Print the text
void indented_sentences(size_t m, size_t n)
{
if (m > n)
return; //base case
else
{
for (int q = 1; q <m; q++)
cout <<" ";//print spaces
cout<<"This was written by calling number "<<m<< endl;
// Recursively call for m+1 line
// For bottom part
indented_sentences(m + 1, n);
for (int q = 1; q < m; q++) //to print spaces
cout <<" ";
cout<<"This was ALSO written by calling number"<<m<<endl;
}// End of else
}// End of function
// Print in binary format
void binary_print(ostream& outs, unsigned int n)
{
if(n <= 1) //base case
{
outs<<n;
return;
}// End of if condition
// Recursively call by dividing by 2
binary_print(outs,n>>1);
outs<<n%2;//print n%2
}// End of function
--------------------------------------------------------------
#include "rec_fun.cpp"
#include <bits/stdc++.h>
using namespace std;
// main function definition
int main()
{
triangle(cout,3, 5); //to print traingular patter
/*------------
*binary values
*-------*/
binary_print(cout,27); //print binary value
cout<<endl;
binary_print(cout,4);//print binary value
cout<<endl;
/* power function */
cout<<pow(2,3)<<endl;
/* indented sentences function*/
indented_sentences(1,4);
return 0;
}// End of main function
Sample Output:
***
****
*****
*****
****
***
11011
100
8
This was written by calling number 1
This was written by calling number 2
This was written by calling number 3
This was written by calling number 4
This was ALSO written by calling number4
This was ALSO written by calling number3
This was ALSO written by calling number2
This was ALSO written by calling number1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.