COSC230 Assignment 3
This assignment consolidates material from Practice’s 4 and 5 on “Stacks, Queues, and
Recursion”. Please work
through these practice’s before attempting this assignment. Please
submit all of your assignment files through Turing (see the link
Assignment Submission via
Turing). Before you submit your assignment please check your code compiles on Turing and
does what you expect it
Question 1: [30 marks/
Use the following function interface:
void rev erse_stack(stackint>& S. stackini>& St slackcint>& 82).
write a C++ function that rev erses the order of elements on stack S using two additional
slacks SI and S2. Do not use the assignment
operator anywhere in your code. A
good format for testing your function is the following:
slackcint> S, SI . 52:
soul « he top element of S -« S.top0 « endl:
rev erse stack(S. St. S2);
tout «’The top element of S is now ‘ «
StopQ « endl;
Submit only the code for this function (no drivers please) in a file called assignment3 function scc.
Question 2: pc,
Use the function interface:
double sum(int n).
to write a recursive function (i.e., no loops are to be used) to add the first n
terms of the series
and return the value of that sum. Make sure to test that your function
gives the correct outpud
Submit only the code for this function (no drivers please) in the file assignment3 functions.cc.
as for Question
Question 3: [40 marks(
Consider the following function:
int call = 1:
string statement = This was written
by call number
and return the value of that sum. Make sure to test that your function gives the correct output! Submit only the code for this function (no drivers please) in the file assignment3 functions-cc. as for Question I. Question 3: (40 marks] Consider the following function: void iterative_call() int call = 1; string statement = Thiswaswritten by call number for (; call != 5; ++cal0 tout « statement « call « endl; statement = “+ + statement; 11 adds leading white space ) for (– call; call I= 0; –call) cout « statement erase(0.1) « call « endl; // removes leading white space Rewrite this as a recursive function (i.e., no loops are to be used) using the function interface: void recuisive_call(int call = 1). so that calling recursive call() gives exactly the same output as calling iterative call° Note that the default argument will be set to call = 1 in the first function call. You might find the keyword static to be useful. Submit the code for this function in assignment3 functions.cc. as for Questions 1 and 2.
COSC230 Assignment 3