DIAMOND OF ASTERISKS

I am in my 1st month of coding. I know the very basic things about it. This code is way out of my league...
I have to write a code for a diamond of asterisks. It is different than other codes i have seen on here.

It must ask the user to input the width of the diamond and the offset of the diamond. Please take a look at the code i have. Maybe experienced c++ people can help me.

If its ran in c++, i can get it to ask the user for input, but it does not make a diamond. It put many asterisks in lines.

Please help. I know it has to be some small problem but i have spent 4 hours looking for the problem and i do not understand what it could be.

i appreciate it your help!!!


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
  #include <iostream>

using namespace std;


void a( void );
void s( void );
void n( void );
void pyramid(int,int);
void wedge(int,int);


void a() {cout << "*";}
void s() {cout << " ";}
void n() {cout << "\n";}

void pyramid (int width, int offset)
{
    int count1, count2;
    for (int j=1; j < width; j += 2)
    {
        for (int i = 0; i < offset; i++) a();
        count2 = j;
        count1 = (width - count2)/2;

 {
            for (int i = 0; i < count1; i++) a();
            for (int i = 0; i < count2; i++) s();
            for (int i = 0; i < count1; i++) n();
            }
            }
        n();
    }


void wedge(int width, int offset)
{
    for (int j = 0; j < width; j++)
    {
        for (int i = 0; i < offset; i++) a();

{
            for (int i = 0; i < 1; i++) a();
            for (int i = 0; i < j; i++) s();
            for (int i = 0; i < width - j - 1; i++) n();

        }
        }
        cout << endl;
    }




int main()
{
    int colWidth, latOffset;

    cout << "This is a program for outputting a diamond formation.\n";
    cout << "First you must select an odd number diamond width.\n";
    cout << "Then you must select a lateral offset even or odd.\n";
    cout << " Input diamond width: ";
    cin >> colWidth;

    if (colWidth % 2 == 0)
    {
        cout << "ERROR: You entered an even number.\nThe program is correcting your error\n";
        colWidth +=1;
    }
    cout << "Input lateral offset: ";
    cin >> latOffset;
    cout << "\n";

    pyramid (colWidth, latOffset);
    wedge (colWidth, latOffset);



    cout << "\nAuthor: CZ" << endl;
    cin.get();
    cin.get();
    return 0;
}
1
2
3
4
5
     *     5 space 1 *
   ***   3 space 3 *
 ***** 1 space 5 *
   ***   3 space 3 *
     *     5 space 1 *


Decrement 2 space and increment 2 asterik also 1 \n after the last asterik for each line, and the opposite after reach the middle
The middle part of diamond is opposite of the first line and last line so first find how many lines that will be outputted, how ? Just count how many times you need to subract that width wth 2 til you get value 1.
Next just use for loop for(i=0;i<numof_line;i++) inside that loop create another statement to output your space, the space in the first line is equal to the diamnd width but remember to subtract that var with 2 afterwards for(j=0;j<same_as_width;j++) now for the asteriks, its equal to width - number of space form before so for(k=0;k<width_after_subtract;k++) that's my logic, I think I'll work.
For the line after the middle I think you'll figure it out by yourself, 1 more thing I don't understand that offset part
Last edited on
Think about how you would do it on paper. Reverse engineer the process.
Topic archived. No new replies allowed.