Diamond Program

Hello, I am new to C++ and I am working in Visual Studios 2012. I am supposed to write a program which, given an integer k greater than 4 and less than 60, displays a diamond of height and width (in the middle) equal to k. Yes, this is one of my homework questions but I have most of the code done. Just need help on where to approach this problem next because of right now I am stumped.

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
#pragma warning (disable:4996)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#define BLANK ' '
#define STAR '*'

/* Function prototypes
 */

// Function to prompt to obtain width
// Returns an integer width as read in using scanf
int getWidth(void);

// Function to draw top of a odd valued diamond
// Uses width to compute
void drawOddtop(int, char);

// Function to draw top of a even valued diamond
// Uses width to compute
void drawEventop(int, char);

// Function to draw bottom of a odd valued diamond
// Uses width to compute
void drawOddbottom(int, char);

// Function to draw bottom of a even valued diamond
// Uses width to compute
void drawEvenbottom(int, char);

// Function to display diamond
// Uses width and characters to display
void drawNChars(int, char);



int main()
{
    int w;
    char ch;
    
    printf("Welcome to the Diamond Generator!\n");
    w = getWidth();
    printf("Width=%d\n\n", w);
    
    drawOddtop(w, STAR);
    drawEventop(w, STAR);
    drawOddbottom(w, STAR);
    drawEvenbottom(w, STAR);
    
    
    printf("\nEnter any character to continue");
    scanf("%c%c", &ch, &ch);
    
    if ( w % 2 == 0 )
    {
        drawEventop(w, STAR);
        drawEvenbottom(w, STAR);
    }
    else if ( w % 2 == 1 )
    {
        drawOddtop(w, STAR);
        drawOddbottom(w, STAR);
    }
    else
    {
        printf ("Invalid.\n");
        return (-1);  //return non-zero on error
    } // end multiple alternative if
    return (0);
}   // end Main




int getWidth()
{
    int w;
    int counter;
    printf("Please enter a width with an odd number between 4 and 60\n");
    scanf("%d", &w);
    counter = 1;
    
    while (w %2 == 0 || w < 4 || w > 60)
    {
        scanf("%d", &w);
        counter = counter +1; ++counter;
        if(counter == 5)
        {
            exit (0);
        }
    }// end while
    return (w);
}


void drawOddtop(int w, char ch) //Function for diamond
{
    int nlb;   // represents numberof leading blanks
    int ns;// represents number of star characters to print in each line
    nlb = (w/2)-2;
    ns = 1;
    
    for (nlb=(w/2)-2; nlb>=0; --nlb)
    {
        for ( ns=1; ns>=0; --ns)
        {
            drawNChars(nlb,STAR);
            drawNChars(ns,STAR);
            drawNChars(1, '\n');
        }
        // end ns loop
    } // end nlb loop
}

void drawEventop(int w, char ch) //Function for diamond
{
    int nlb;   // represents numberof leading blanks
    int ns;// represents number of star characters to print in each line
    nlb = (w/2)-2;
    ns = 2;
    
    for (nlb=(w/2)-2; nlb>=0; --nlb)
    {
        for ( ns=1; ns>=0; --ns)
        {
            drawNChars(nlb,STAR);
            drawNChars(ns,STAR);
            drawNChars(1, '\n');
        }
        // end ns loop
    } // end nlb loop
}

void drawOddbottom(int w, char ch) //Function for diamond
{
    int nlb;   // represents numberof leading blanks
    int ns;// represents number of star characters to print in each line
    nlb = (w/2)-2;
    ns = 1;
    
    for (nlb=(w/2)-2; nlb>=0; --nlb)
    {
        for ( ns=1; ns>=0; --ns)
        {
            drawNChars(nlb,STAR);
            drawNChars(ns,STAR);
            drawNChars(1, '\n');
        }
        // end ns loop
    } // end nlb loop
}

void drawEvenbottom(int w, char ch) //Function for diamond
{
    int nlb;   // represents numberof leading blanks
    int ns;// represents number of star characters to print in each line
    nlb = (w/2)-2;
    ns = 2;
    
    for (nlb=(w/2)-2; nlb>=0; --nlb)
    {
        for ( ns=1; ns>=0; --ns)
        {
            drawNChars(nlb,STAR);
            drawNChars(ns,STAR);
            drawNChars(1, '\n');
        }
        // end ns loop
    } // end nlb loop
}

// Function to display a character (ch) n times on one line
void drawNChars(int n, char ch)
{
    int k;
    if (n < 0)
    {
        printf("\nIn drawNChars, n < 0. Invalid. Terminating program.\n");
        exit(0);
    } //end if
    
    for (k = 1; k <= n; ++k)
    {
        printf("%c", ch);
    } // end for
} // end drawNChars 
Last edited on
Use code tags for code, please.

if(w % 2 == 0) is before any input, so it doesn't do anything for you. You should just delete it.

You have functions for odd and even diamonds, yet you only call the odd one. You should probably change that.

You're going to want to draw the bottom of the diamond.

I don't know what you're asking for. Maybe a little bit more detail to your question will help.
Thanks for the response. The main question is Write a program which, given an integer k greater than 4 and less than 60, displays a diamond of height and width (in the middle) equal to k. Im not quite sure how to call my functions to display the diamond with main among other problems.


Heres my current one.

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
int main()
{
    int w;
    char ch;
    
    printf("Welcome to the Diamond Generator!\n");
    w = getWidth();
    printf("Width=%d\n\n", w);
    
    drawOddtop(w, STAR);
    drawEventop(w, STAR);
    drawOddbottom(w, STAR);
    drawEvenbottom(w, STAR);
    
    if ( w % 2 == 0 )
    {
        drawEventop(w, STAR);
        drawEvenbottom(w, STAR);
    }
    else if ( w % 2 == 1 )
    {
        drawOddtop(w, STAR);
        drawOddbottom(w, STAR);
    }
    else
    {
        printf ("Invalid.\n");
        return (-1);  //return non-zero on error
    } // end multiple alternative if
    
    printf("\nEnter any character to continue");
    scanf("%c%c", &ch, &ch);

}   // end Main 
Last edited on
Use code tags - http://www.cplusplus.com/articles/jEywvCM9/

Also, just edit your main next time
Lines 66-70 are useless (they'll never be reached). Line 61 should just be an else. Lines 47-50 don't make sense in this program.
I am basically trying to mimic this code from the last project I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()

{ 

int Width; 

char ch; 

printf("Welcome to the expandable House Program!\n"); 

Width = getWidth(); 

printf("Width=%d\n\n", Width); 

PrintInvVee(Width, ASTERISK);

PrintSquare(Width, drawChar); 

printf("\nEnter any character to continue"); 

scanf("%c%c", &ch, &ch); 

}   // end Main 


I thought lines 47-50 would be the same as "PrintInVee" in the code above.
Last edited on
47-50 will output something like this


   *
  * *
 *   *
*     *
   **
  *  *
 *    *
*      *
 *    *
  *  *
   **
 *   *
  * *
   *

Which I'm sure doesn't make any sense.
Last edited on
Is that also due to the equations inside my draw functions or solely my main?
Just your main. You called all 4 draws, so it did all 4 of them. If you don't write that, it will only draw them when you need them to be drawn.
Alright thanks for the help.
One more question. I changed my code for my draw functions. Now how do a flip the triangle to complete the diamond? I know I have to apply it to drawOddbottom but dont know where.

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
void drawOddtop(int w, char ch) //Function for diamond
{
    int h;
    int nlb;   // represents numberof leading blanks
    int ns;// represents number of star characters to print in each line
    nlb = (w/2);
    ns = 1;
    h = (w+1)/2;
    int i;
    
    for (i = 1; i <= h; i+=1)
    {
        drawNChars(nlb,BLANK);
        drawNChars(ns,STAR);
        drawNChars(1, '\n');
        
        nlb -= 1;
        ns += 2;
        
    }
    // end loop
    
}

void drawOddbottom(int w, char ch) //Function for diamond
{
    int h;
    int nlb;   // represents numberof leading blanks
    int ns;// represents number of star characters to print in each line
    nlb = (w/2);
    ns = 1;
    h = (w+1)/2;
    int i;
    
    for (i = 1; i <= h; i+=1)
    {
        drawNChars(nlb,BLANK);
        drawNChars(ns,STAR);
        drawNChars(1, '\n');
        
        nlb -= 1;
        ns += 2;
        
    }
    // end loop
    
}
Last edited on
You're right that they will be mirror images of each other, but one will be a line short, I believe. That's because the middle of the diamond will be drawn in the top.

The way to do it is probably just to reverse your for loop in the bottom and exclude one case.
Topic archived. No new replies allowed.