Answer to Question #7927 in C++ for alex lee

Question #7927
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 display 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.
1
Expert's answer
2012-03-29T12:40:35-0400
#include <iostream>
using namespace std;

int display(char ch, int n);

int main() {
char ch;
int n;
cout << "Enter character: ";
cin >> ch;
cout << " Enter number: ";
cin >> n;
int number = display(ch, n);
cout << endl << number << endl;
return 0;
}

int display(char ch, int n) {
int k = 0;
for (int i = 1; i <= n; i++) {
& for (int j = 1; j <= i; j++)
& cout << ((j > 1) ? " " : "");
& for (int j = 1; j <= i; j++)
& cout << ch;
& cout << endl;
& k += i;
}
ch++;
for (int i = n; i >0; i--) {
& for (int j = i ; j > 0; j--)
& cout << ((j > 1) ? " " : "");
& for (int j = i ; j > 0; j--)
& cout << ch;
& cout << endl;
}
return k;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS