C++ Print Parallelogram using functions

I am confused whether I'm supposed to print the stars in either PrintChars or DrawShape. Any other hints on what I'm doing wrong would also be helpful. Thank you.

Instructions:
// get and verify number of rows – do everything to make
// sure the number is in the right range and is odd
int GetNRows( void );
// given the number of rows and the current row number,
// calculate the number of spaces on this row
int NumSpaces( int nRows, int row );
// given the number of rows and the current row number,
// calculate the number of stars (asterisks) on this row
int NumStars( int nRows, int row );
// print n copies of the character ch on the current row
void PrintChars( int n, char ch );
// draw the shape – do all the work to draw the shape
void DrawShape( int nRows );

Youmay not use any global variables for this program, nor may you add any parameters to any of the functions.

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
  #include <iostream>
using namespace std;

int GetNRows(int &);
int NumSpaces( int, int);
int NumStars( int, int);
void PrintChars( int, char);
void DrawShape(int);

int main()
{
    int nRows, maxC, space, ast, n, row;
    char ch = ' ';


    for(int row = 1;row <= nRows;row++) //rows
    {
    if(row <= 1)
    {
        GetNRows( nRows );
    }
    n = NumSpaces(nRows, row );
    nRows = NumStars( nRows, row );
    PrintChars( n, ch );
    DrawShape( nRows );
    }
    return 0;
}

int GetNRows( int &nRows )
{
    int maxC;
    cout << "Enter number of rows(3-23): " << flush;
    cin >> nRows;

    while(nRows < 3)
    {
        cout << "That is too few rows for me to make a parallelogram."
             << endl;
        cout << "Enter number of rows(3-23): " << flush;
        cin >> nRows;
    }
    while(nRows > 23)
    {
        cout << "That is too many rows for me to make a parallelogram."
             << endl;
        cout << "Enter number of rows(3-23): " << flush;
        cin >> nRows;
    }
    if(nRows % 2 == 0)
    {
        nRows++;
    }
}

int NumSpaces( int nRows, int row )
{
    int maxC = (nRows / 2), space;
    if(row <= maxC)
    {
        space = maxC - row;
    }
    else
    {
        space = 0;
    }
    return space;
}

int NumStars( int nRows, int row )
{
    int ast, maxC = (nRows / 2);
    if(row <= maxC)
    {
        ast = row;
    }
    else
    {
        ast = nRows - row;
    }
    cout << ast;
    return ast;
}

void PrintChars( int n, char ch )
{
    for(int s = 0; s < n; s++)
    {
        cout << ch;
    }
}

void DrawShape( int nRows )
{
    int ast;
    for(int a = 0; a < nRows; a++)
        {
            cout << '*';
        }
        cout << '\n';
}
Last edited on
closed account (48T7M4Gy)
It's ambiguous so ask your teacher otherwise take your pick yourself because your guess is as good as anybody here unless you teacher is here too.

Otherwise provided your program runs and does what it is supposed to do no changes are needed, looks god, move on. Cheers :)
Maybe DrawShape is supposed to call PrintChars?
closed account (48T7M4Gy)
Or vice versa ... therein lies the OP problem ... ;)
1
2
3
4
// print n copies of the character ch on the current row
void PrintChars( int n, char ch );
// draw the shape – do all the work to draw the shape
void DrawShape( int nRows );


So PrintChars prints n chars on a row, so DrawShape could use this to build up a shape, hence the nRows parameter. The other way around doesn't make sense IMO :+)
Last edited on
closed account (48T7M4Gy)
My point exactly :)
Topic archived. No new replies allowed.