C++ Programming. Ch.7 The purpose of the assignment is to practice writing metho
ID: 3686737 • Letter: C
Question
C++ Programming. Ch.7 The purpose of the assignment is to practice writing methods that are recursive. We will write four methods each is worth 15 points. a- int sum_sqr_rec(stack stk) which will receive a stack of "int" and output the sum of the squares of the elements in the stack. b- int plus_minus_rec(stack stk) which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and output the sum of the elements in the stack as follows: a - b + c - d + e - f + g - h + i -j c- void prt_chars_rev_rec(stack stk) which will receive a stack of "char" and print its elements in reverse. d- void prt_chars_rec(queue que) which will receive a queue of "char" and print its elements. Remember to use the stack and queue STL. The Assignment will require you to create 2 files: 1- Recursive.h which contain the details of creating the 4 methods as specified above: int sum_sqr_rec(stack stk), (15 points) int plus_minus_rec(stack stk), (15 points) void prt_chars_rev_rec(stack stk), (15 points) void prt_chars_rec(queue que), (15 points) 2- RecursiveDemo.cpp which: A- reads a string expression: {(1+2)+[4*(2+3)]} and store the expression in a stack and a queue.(15 points) a- prints the corresponding expression in reverse using: prt_chars_rev_rec ( 5 points): }])3+2(*4[+)2+1({ b- prints the corresponding expressing as is using: prt_chars_rec.( 5 points): {(1+2)+[4*(2+3)]} B- reads an array of integers: 1 2 3 4 5 6 7 8 9 10 and store them in a stack of ints.(5 points) Then it: c- prints the sum of the squares of the elements in the stack using int sum_sqr_rec(stack stk) and outputting the value(5 points): 385 d- prints the sum of the elements in the stack using: int plus_minus_rec(stack stk) and outputting the value(5 points): 1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10 = -5 Do not forget to drop the 2 files in the dropbox of Assignment7.
Explanation / Answer
//Recursive.h
#include <iostream>
//template stack & queue files
//they will accept both char or int
#include "stack.cpp"
#include "queue.cpp"
using namespace std;
/*int sum_sqr_rec(stack stk) which will receive a stack of "int"
and output the sum of the squares of the elements in the stack*/
int sum_sqr_rec(stack stk)
{
if(stk.top==0)
{
int v=stk.pop();
return v*v;
}
else
{
int v=stk.pop();
return (v*v)+ sum_sqr_rec(stk);
}
}
/* void prt_chars_rev_rec(stack stk) which will receive a stack of "char" and print its elements in reverse*/
void prt_chars_rev_rec(stack stk)
{
if(stk.top==0)
{
cout<<stk.pop();
}
else
{
stk.pop();
prt_chars_rev_rec(stk);
}
}
/*
int plus_minus_rec(stack stk) which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and
output the sum of the elements in the stack as follows: a - b + c - d + e - f + g - h + i -j c-*/
int plus_minus_rec(stack stk)
{
static int sign=1;
if(stk.top==0)
{
return stk.pop();
}
else
{
int v=(sign)*stk.pop());
sign*=-1;
return (v+ plus_minus_rec(stk));
}
}
/*------------------------------------------------------------------------------------*/
//RecursiveDemo.cpp
#include <iostream>
#include "Recursive.h"
using namespace std;
int main()
{
stack s;
queue q;
/*A- reads a string expression: {(1+2)+[4*(2+3)]} and store the expression in a stack and a queue.*/
string expression="(1+2)+[4*(2+3)]";
for(int i=0;i<expression.length();i++)
{
s.push(expression.substr(i,i));
q.enqueur(expression.substr(i,i));
}
/*prints the corresponding expression in reverse using: prt_chars_rev_rec */
cout<<endl<<"Printing expression in reverse: "<<endl;
prt_chars_rev_rec(s);
/*B- reads an array of integers: 1 2 3 4 5 6 7 8 9 10 and store them in a stack of ints.*/
stack s1;
for(int i=0;i<10;i++)
{
s1.push(i+1);
}
/* c- prints the sum of the squares of the elements in the stack using int sum_sqr_rec(stack stk)
and outputting the value*/
cout<<endl<<"Sum of squares: "<<sum_sqr_rec(s1);
/*d- prints the sum of the elements in the stack using: int plus_minus_rec(stack stk) and outputting the value*/
cout<<endl<<"Sum of elements using alternating signs: "<<plus_minus_rec(s1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.