Pyramid

Please help me find a solution for this question below.

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:

*
**
***
****
*****
******
*******
********
*********
**********
++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+
*
**
***
****
*****
******
*******
********
****


Many thanks in advance.
I was given some tips for this question, and the below is the tips.

#include<iostream>
using namespace std;

// display ch consecutively for n times
int display(char, int);

int main( )
{
// declare and initialise variables
// prompt and input the total number of non-whitespace characters

// loop the display of * pyramid and + pyramid
while ( ... ) // compare total with target?
{
// loop through the * shape
// find how many non-space characters to display in next line
// leave the loop if the target is met
// display the leading blanks in a line
// display * for the rest of the line
// update the total number of displayed characters

// loop through the + shape
// similar to the * case
}

system("pause");
return 0;
}


int display(char ch, int n)
{
// display the characters ch for n times
// display the trailing newline if needed
// return a proper value
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
//  main.cpp
//  PROJECTTRIANGLES
//
//  Created by Aaron on 2/29/12.
// input:
// the heights and lengths/widths of all 5 shapes, some require more of them that others
// output:
// shapes which have their size based on the numbers entered
// asumptions:
// user will enter integers for the requested values
// *************************************************************************************

#include <iostream>
#include <cstdlib> 

//data structure for shapes
struct shape{
    int height;
    int length;
    int reverb;
};
// function to produce a triangle
void triangle_1(int height);
// function to produce the 2nd triangle
void triangle_2(int height, int reverb);
// function to produce the 3rd triangle
void triangle_3(int height, int reverb);
//function to produce 4th triangle
void diamond(int height, int length);
// function to produce square
void square_shape(int width);

int main(){
    // defines shapes 
    shape triangle, square, triangle2, triangle3, triangle4;
    // into and prompts user for the length of square 
    std::cout << "*** The Incridible Shape Drawing Program ***\n";
    std::cout << "Let's draw a rectangle please enter a number for the width between 2 and 20: ";
    std::cin >> square.length;
    // length of square size requirements
    for(square.length = square.length; square.length < 2 || square.length > 20; --triangle.reverb) {
        std::cout << "You entered an invalid number...\n Enter a new number: ";
        std::cin >> square.length;
    }
    //prompts user for height of square 
    std::cout << "Please enter a number for height between  1 and 10, (but less than the width: ";
    std::cin >> square.height;
    // height of square size requirements
    for(square.height = square.height; square.length > square.height && (square.height > 10 || square.height < 1); --triangle.reverb){
        std::cout << "You entered an invalid number...\n Enter a new number: ";
        std::cin >> square.height;
    }
    // formating
    std::cout << '\n';
    //using square_shape function and a fore loop to make the square
    for (square.height = square.height; square.height > 0; --square.height){
        square_shape(square.length);
        std::cout << '\n';
    }
    //formating
    std::cout << '\n';
    //prompts user for height of triangle_1
    std::cout << "Lets draw  triangle #1\nPlease enter a number for the height between 2 and 10: ";
    std::cin >> triangle.height;
    // height of triangle_1 requirements
    for(triangle.height = triangle.height; triangle.height < 2 || triangle.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle.height;
    }
    //formating
    std::cout << '\n';
    //for loop which creates triangle_1
    for(triangle.length = 1; triangle.height > 0; ++triangle.length && --triangle.height){ 
    triangle_1(triangle.length);
        std::cout << '\n';
    }
    //formating
    std::cout << '\n';
    // prompts user for height of triangle2
    std::cout << "Lets draw triangle #2\nPlease enter a number for the height between 2 and 20: ";
    std::cin >> triangle2.height;
    // height of triangle2 requirements
    for(triangle2.height = triangle2.height; triangle2.height < 2 || triangle2.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle2.height;
    }
    //formating
    std::cout << '\n';
    //for statement to create triangle_2
    for(triangle2.length = 1 && (triangle2.reverb = triangle2.height - 1); triangle2.height > 0; --triangle2.reverb){ 
        triangle_2(triangle2.length,triangle2.reverb);
        std::cout << '\n';
        ++triangle2.length;
        --triangle2.height;
        }
    // prompts user for height of triangle3
    std::cout << "\nLets draw triangle #3\nPlease enter a number for the height between 2 and 20: ";
    std::cin >> triangle3.height;
    // height of triangle3 requirements
    for(triangle3.height = triangle3.height; triangle3.height < 2 || triangle3.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle3.height;
    }
    //formating
    std::cout << '\n';
    //for statement to create triangle_3
    for(triangle3.length = 1 && (triangle3.reverb = triangle3.height - 1); triangle3.height > 0; --triangle3.reverb){ 
        triangle_3(triangle3.length,triangle3.reverb);
        std::cout << '\n';
        triangle3.length += 2;
        --triangle3.height;
    }
    // prompts user for height of triangle4
    std::cout << "\nLets draw diamond #1\nPlease enter a number for the height between 3 and 10: ";
    std::cin >> triangle4.height;
    // height of triangle4 requirements
    
    for(triangle4.height = triangle4.height; triangle4.height < 3 || triangle4.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle4.height;
    }
    
    //formating and determining the actual value of variable triangle4.height
       triangle4.height -= 2;
    // this starts the reversing proccess for the value of triangle4.height
    std::cout << '\n';
    
    //for statement to create triangle_3 which is the top half of the diamond
    for(triangle4.length = 1 && (triangle4.reverb = triangle4.height - 1); triangle4.height > 0; --triangle4.reverb){ 
        triangle_3(triangle4.length,triangle4.reverb);
        std::cout << '\n';
        triangle4.length += 2;
        --triangle4.height;
    }
    
    //reseting values in preperation for 2nd half of rhombus 
    triangle4.length -= 4; 
    triangle4.reverb = 1;
    triangle4.height = 1; 
    //creates 2nd half of rhombus
    
    for(triangle4.length = triangle4.length; 0 < triangle4.length; ++triangle4.reverb){ 
        diamond(triangle4.length, triangle4.reverb);
        std::cout << '\n';
        triangle4.length -= 2;
        ++triangle4.height;
    }
        return 0;
}

void triangle_1(int height){
    //for loop to make the width of triangle_1
    for(height = height; height != 0; --height){
        std::cout << "*";
    }
}    

void triangle_2(int height, int reverb){
    //for loop to make the width of triangle_2 empty space
    for(reverb = reverb; reverb != 0; --reverb){
        std::cout << " "; 
    }
    //for loop to make the width of triangle_2 
    for(height = height; height != 0; --height){
        std::cout << "*";
        }
}

void triangle_3(int height, int reverb){
    //for loop to make the width of triangle_3 empty space
    for(reverb = reverb; reverb != 0; --reverb){
        std::cout << " "; 
    }
    //for loop to make the width of triangle_2 
    for(height = height; height != 0; --height){
        std::cout << "*";
    }
}

void diamond(int height, int length){
    //for loop to make the width of diamond empty space
    for(length = length; length != 0; --length){
        std::cout << " "; 
    }
    //for loop to make the width of diamond  
    for(height = height; height != 0; --height){
        std::cout << "*";
    }
}

void square_shape(int width){
    // for loop to make the width of the square
    for(width = width; width != 0; --width){
        std::cout << "*";
    }
}
see if you can follow along to see how i did some of the things. that will help you try to make your project.
Last edited on
Topic archived. No new replies allowed.