Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following code (C++): Main program: Ask the user to enter character da

ID: 3674042 • Letter: W

Question

Write the following code (C++):
Main program: Ask the user to enter character data and read their response into a string variable. The input may contain blank spaces.
   Call reverse function that will bring back a string that has the same characters, but in reverse order. Store the return value into another string variable.
   Call an output function that will write out both strings.
  Call getPos function to get a positive integer from the user.
  
Call drawupper function to draw a triangle that is largest at the top. Send the positive integer input value as a parameter.
  Call drawlower function to draw a triangle that is largest at the bottom. Send the positive integer input value as a parameter

reverse: write a recursive function that will receive one string as a parameter and will return another string that has all of the characters from the first, but in reverse order.

output: write a function that receives two strings and writes them out to the screen on separate lines.

getPos: write a function that will ask the user for a positive integer value and read in their reponse. If the input value is not positive, write out an error message and repeat the input until a positive value is found. Return the positive input value. drawUpper: write a recursive function that will draw a triangle using the ‘*’ character. The triangle should be largest at the top and decrease in size by one on each line. The function will receive the maximum width as a parameter. Example: if the parameter value is 4, the output should be
****
***
**
*

drawLower: write a recursive function that will draw a triangle using the ‘*’ character. The triangle should have a single ‘*’ at the top and increase in size by one on each line until the maximum size is reached. The function will receive the maximum width as a parameter. Example: if the parameter value is 4, the output should be
*
**
***
****

Include comments before each function definition

Explanation / Answer

#include<iostream.h>

#include<string.h>

using namespace std;

string reverse(const string& a);

void output(const string& a, string& b);

int getPos();

void drawUpper(int w);

void drawLower(int w);

int main()

{

sting a,a1;

cout<<"please enter characters";

getLine(cin,a);

a1= reverse(a);

output(a,a1);

int n= getPos();

cout<<"The upper triangle is:";

drawUpper(n);

cout<<"The lower Triangle is";

drawLower(n);

return 0;

}

string reverse(string& a)

{

size_t s=a.size();

if(s==1)

cout<<a<<endl;

else

{

cout<<a[s-1];

string b= a.substr(0,s-1);

reverse(b);

}

return b;

}

void output(string& a,string& b)

{

cout<<"the original string is:"<<a;

cout<<"The reverse string is:"<<b;

}

int getPos()

{

int num;

cout<<"enter the positive number";

cin>>num;

if(num <0)

getPos();

else

return num;

}

void drawUpper(int w)

{

for(int i=0;i<w;i++)

{

for(j=1; j<w-i;j++)

cout<<" ";

for(j=1;j<=2*i-1;j++)

cout<<" *";

cout<<endl; //cout<<" " can also be used

}

}

void drawLower(int w)

{

int m=w;

for(int i=w;i>=1; i--)

{

cout<<" ";

for(int j=1;j<=2*m-1;j++)

cout<<" ";

for(int k=1;k>=1;k--)

cout<<"* ";

m=m+1;

}

}