Need help

I'm not sure if this kind of discussion is allowed here but I'm desperate and need help

I'm new to programming and an eager learner
I have been trying to code but I'm getting frustrated when my code doesn't work
I can't print out any patterns in C++ like the hollow diamond triangle or anything

I am having problems to develop logic

I am not gonna give up no matter what but I seek guidance I WANNA ACE PROGRAMMING NO MATTER WHAT!

Any help would be appreciated
programming is just basic logic. take a complicated idea and break it down into it's simplest step. If you can write on paper, you can program it.

How would you draw a triangle in notepad?

1
2
3
      *
     * *
    *****


the top and bottom rows are unique: the top row writes 1 * and the bottom row writes several.

All the other rows will write 2, in a repeating pattern of write N-v spaces, write *, write v spaces, write * .. a loop. Play with the spaces written and you will get different looking triangles.

the last line is the same logic as the loop except the inner spaces are * instead of space.

The first line can be thought of as logically the same (write spaces, write *, write 0/nothing, write *) but you can't write nothing and over-write your *, so you have to code it differently.

A diamond removes the bottom row and reverses the pattern at some point, and the last row is like the first row, writing just 1 *

(by the way it is possible to write the entire thing in a single loop but on your first try it will be easier to make the special rows distinct).
Last edited on
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
#include <iostream>
using namespace std;

int main() {

	int rows;
	
	cout << "How many rows do you want to have? ";
	cin >> rows;

	if (rows <= 0) { return 0; }

	for (int i = 0; i < rows; i++) {
		if (i == 0) {
			for (int j = 0; j < (rows-1); j++) {
				cout << " ";
			}
			cout << "*" << endl;
		}//first row

		if (i > 0 && i < rows - 1) {
			for (int a = 0; a < (rows - 1 - i); a++) {
				cout << " ";
			}
			cout << "*";
			for (int b = 0; b < (i * 2 - 1); b++) {
				cout << " ";
			}
			cout << "*" << endl;
		}// middle

		if (i == rows - 1 && rows > 1) {
			for (int k = 0; k < (rows * 2) - 1; k++) {
				cout << "*";
		    }
			cout << endl;
		}// last row
	}

	cin.ignore();
	cin.get();

	return 0;
}


I tried to break the process down into three steps.
The first row which only has 1 star, the middle rows which have 2 stars, and the last row which has all stars. By separating it into these three steps you can spot easy patterns. There is probably a much cleaner solution and I encourage you to find your own. This assignment is a good study in logical thinking.
Last edited on
Topic archived. No new replies allowed.