62 540
Assignments Done
98,7%
Successfully Done
In June 2018

Answer to Question #7652 in C++ for zane

Question #7652
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 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-whitespac
Expert's answer
#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

Assignment Expert
13.04.12, 15:02

You're welcome. We are glad to be helpful.
If you really liked our service please press like-button beside answer field. Thank you!

Blod Lognish
13.04.12, 13:08

this is great. but the number typed in is meant to be the amount of characters shown not the amount of rows. but it's great anyway.

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions