Write a C++ function display prototyped by int display(char ch, int n); that dis
ID: 3641780 • Letter: W
Question
Write a C++ function display prototyped by
int display(char ch, int n);
that displays the character ch on the stdout device (screen) consecutively n times if n>0 and displays nothings if n=0. When n>0 and ch is not the blank character, the function will also display an additional newline at the end. The function display returns the actual number of characters (excluding the possible newline at the end) displayed.
Write a driver program, making use of the above specified function display, that line by line repeatedly displays the character pattern
*
**
***
****
*****
******
*******
********
*********
**********
++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+
until a given numer of non-whitespace characters are displayed. In other words, the program will first ask the user to enter the total number, say target, of non-whitespace characters to be displayed, and then it will repeatedly display the above pattern until exactly target non-whitespace characters are displayed. For instance, if target is entered as 150, then the program will produce the following output (containging exactly 150 non-whitespace characters) afterwards:
*
**
***
****
*****
******
*******
********
*********
**********
++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+
*
**
***
****
*****
******
*******
********
****
Explanation / Answer
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int i,j,s,n;
cin>>n;
if (n==0)
cout<<endl;
else
for (i=1; i<=n; i++)
{
for(s=0; s<i; s++)
{
cout<<" ";
}
for(j=1; j<=i; j++)
{
cout<<"*";
}
cout<<endl;
}
for (i=n; i>0; i--)
{
for(s=0; s<i; s++)
{
cout<<" ";
}
for(j=1; j<=i; j++)
{
cout<<"+";
}
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.