C++ Programming The purpose of the assignment is to practice writing methods tha
ID: 3694410 • Letter: C
Question
C++ Programming
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<int> 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<int> 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<char> stk)
which will receive a stack of "char" and print its elements in reverse.
d- void prt_chars_rec(queue<char> 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<int> stk), (15 points)
int plus_minus_rec(stack<int> stk), (15 points)
void prt_chars_rev_rec(stack<char> stk), (15 points)
void prt_chars_rec(queue<char> 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<int> stk) and outputting the value(5 points):
385
d- prints the sum of the elements in the stack using:
int plus_minus_rec(stack<int> stk) and outputting the value(5 points):
1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10 = -5
Here is my work so far, I am almost correct but I need to code it per the specifications:
Recursive.H
#include <stack>
#include <queue>
using namespace std;
#ifndef Recursive_h
#define Recursive_h
int sum_sqr_rec(stack<int> stk);
int plus_minus_rec(stack<int> stk);
void prt_chars_rev_rec(stack<char> stk);
void prt_chars_rec(queue<char> que);
#endif /* Recursive_h */
RecursiveDemo.cpp
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "Recursive.h"
using namespace std;
int sum_sqr_rec(stack<int> stk) {
if(stk.empty()) { //base condition - empty stack
return 0;
}
int ret = (stk.top()*stk.top()); //square of top element
stk.pop(); //remove top element
return ret + sum_sqr_rec(stk); //recursive call added to current square
}
int plus_minus_rec(stack<int> stk) {
if(stk.empty()) { //base condition - empty stack
return 0;
}
int ret = stk.top();
stk.pop(); //remove top element
if(stk.size()%2 == 1) { //if odd size left, current element needs to be subtracted
return plus_minus_rec(stk) - ret; //recursive call
}
else { //even size left
return plus_minus_rec(stk) + ret; //recursive call
}
}
void prt_chars_rev_rec(stack<char> stk) {
if(stk.empty()) { //base condition - empty stack
return;
}
cout<<stk.top(); //characters will be in reverse order, so print top first
stk.pop(); //remove printed element
prt_chars_rev_rec(stk); //recursive call
}
void prt_chars_rec(queue<char> que) {
if(que.empty()) { //base condition - empty stack
return;
}
cout << que.front(); //characters in correct order, as pushed to back of queue
que.pop(); //remove printed character, pop from front
prt_chars_rec(que); //recursive call
}
int main() {
stack<char> s1;
queue<char> q1;
string st;
cin>>st; //to create required stack and queue
for(int i = 0; i<st.size(); i++) {
s1.push(st[i]);
q1.push(st[i]);
}
prt_chars_rev_rec(s1);
cout<<" ";
prt_chars_rec(q1);
cout<<" ";
int in; //to create requored int stack
stack<int> s2;
for(int j = 0; j<10; j++) {
cin>>in;
s2.push(in);
}
cout<<sum_sqr_rec(s2);
cout<<" ";
cout<<plus_minus_rec(s2);
cout<<" ";
return 0;
}
The professor gave us an outline for both programs, not necessarily how it needs to be done, but helpful:
Here is an outline of the file Recursive.h:
// recursive methods:
#include <iomanip>
#include<iostream>
#include<sstream>
#include<stack>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
void print_chars_rec(queue<char> que);
void print_chars_rec_rev(stack<char> str);
int sum_sqr_rec(stack<int> str);
int plus_minus_rec(stack<int> stk);
class Recursive{
public:
void print_chars_rec(queue<char> que) {
if //if part is the base case
else //case queue is not empty
}
}
the outline of the RecursiveDemo.cpp:
#include <iostream>
#include <iomanip>
#include "Recursive.h"
// header file
using namespace std;
void main()
{
/*
declare an object ap1 of the Recursive class created in the header file above;
//connect ap1 to prt_chars_rec(queue<char> que)
//connect ap1 to print_chars_rec_rev(stack<char> stk)
//connect ap1 to sum_sqr_rec(stack<int> stk)
//connect ap1 to
int plus_minus_rec(stack<int> stk)
//and print its value;
};
If someone could evaluate my code and fix it to where I meet all stipulations, I'd be very greatly! Highly rated for someone who is correct! Thanks so much!
Explanation / Answer
Recursive.H
#include <stack>
#include <queue>
using namespace std;
#ifndef Recursive_h
#define Recursive_h
int sum_sqr_rec(stack<int> stk);
int plus_minus_rec(stack<int> stk);
void prt_chars_rev_rec(stack<char> stk);
void prt_chars_rec(queue<char> que);
#endif /* Recursive_h */
RecursiveDemo.cpp
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "Recursive.h"
using namespace std;
int sum_sqr_rec(stack<int> stk) {
if(stk.empty()) { //base condition - empty stack
return 0;
}
int ret = (stk.top()*stk.top()); //square of top element
stk.pop(); //remove top element
return ret + sum_sqr_rec(stk); //recursive call added to current square
}
int plus_minus_rec(stack<int> stk) {
if(stk.empty()) { //base condition - empty stack
return 0;
}
int ret = stk.top();
stk.pop(); //remove top element
if(stk.size()%2 == 1) { //if odd size left, current element needs to be subtracted
return plus_minus_rec(stk) - ret; //recursive call
}
else { //even size left
return plus_minus_rec(stk) + ret; //recursive call
}
}
void prt_chars_rev_rec(stack<char> stk) {
if(stk.empty()) { //base condition - empty stack
return;
}
cout<<stk.top(); //characters will be in reverse order, so print top first
stk.pop(); //remove printed element
prt_chars_rev_rec(stk); //recursive call
}
void prt_chars_rec(queue<char> que) {
if(que.empty()) { //base condition - empty stack
return;
}
cout << que.front(); //characters in correct order, as pushed to back of queue
que.pop(); //remove printed character, pop from front
prt_chars_rec(que); //recursive call
}
int main() {
stack<char> s1; //object of class stack
queue<char> q1; // object of class queue
string st;
cin>>st; //to create required stack and queue
for(int i = 0; i<st.size(); i++) {
s1.push(st[i]);
q1.push(st[i]);
}
prt_chars_rev_rec(s1); //it change the recursion value from int to char
cout<<" ";
prt_chars_rec(q1);
cout<<" ";
int in; //to create requored int stack
stack<int> s2;
for(int j = 0; j<10; j++) {
cin>>in;
s2.push(in); // insert the values in other requored stack with integer values
}
cout<<sum_sqr_rec(s2); // print the sum of requored
cout<<" "; stack
cout<<plus_minus_rec(s2); // print the value of required stack.
cout<<" ";
return 0;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.