
Akisha L.
asked 01/21/21Programming Logic and Design
1. The following module uses a loop. Rewrite it as a recursive module that performs the same operation.
Module trafficSign(int n)
While n > 0
Display "No Parking"
Set n = n - 1
End While
End Module
2. Recursive Array Sum
Design a function that accepts an Integer array and the size of the array as arguments. The function should recursively calculate the sum of all the numbers in the array and return that value.
1 Expert Answer

Patrick B. answered 01/21/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
/*-------------------
Problem in the GIVEN algorithm...
input parameter n MUST be positive,
or else infinite loop
-------------------------------------*/
void trafficSign(int n)
{
if (n<=0) { n = -n; }
while (n > 0 )
{
cout << "No Parking" << endl;
n = n - 1 ;
}
}
//recursive version of the function is capitalized
void TrafficSign(int n)
{
if (n>0)
{
cout << "No Parking" << endl;
trafficSign(n-1);
}
}
long Sum(int * A, int n)
{
if (n>0)
{
return( A[n-1] + Sum(A,n-1));
}
else
{
return(0);
}
}
/****
For example, under this defintion for index parameter n.....
Sum(5) = A[4] + Sum(4)
= A[4] + A[3] + sum(3)
= A[4]+ A[3] + A[2] + Sum(2)
= A[4] + A[3] + A[2] + A[1] + Sum(1)
= A[4] + A[3] + A[2] + A[1] + A[0] + sum(0)
= A[4] + A[3] + A[2] + A[1] + A[0] + 0
**************/
int main()
{
int n=10;
trafficSign(n);
cout <<"------------------------" << endl;
TrafficSign(n);
cout << "-----------------------" << endl;
int A[] = {2,3,4,6,8,12,16,18}; //current total is 69
cout << Sum(A,8);
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code is uploaded to the RESOURCES section if you want it01/21/21