Cant figure out how to print out figures

I have to write a code that prints out three figures and I got the first one down but have no idea how to even approach the other two. Below are the two I have to print out when a user inputs an odd number

http://www.cs.kent.edu/~ruttan/cs13011/Labs/Lab_Assignment%20_1.txt

This is the code

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

using namespace std;


int main() {

	int odd_Num;
	bool mistake;
	for (int i = 0; i < 3; i++) {
		mistake = false;

cout << "Please enter a positive odd number < 40 for the size of the figures: " << endl;
		cin >> odd_Num;


		if (odd_Num > 40) {
			cout << "Sorry your number is too big " << endl;
			mistake = true;
		}


		if (odd_Num % 2 == 0) {
			cout << "Sorry you entered an even number" << endl;
			mistake = true;
		}


		if (odd_Num < 0) {
			cout << "Sorry your number is not postive " << endl;
			mistake = true;
		}
		if (!mistake) {
			cout << "Thank You" << endl;
			break;
		}
		else if (i == 2) {
			cout << "try again later" << endl;
			exit(0);
		}
	}


	for (int r = 0; r < odd_Num; ++r)
		cout << "--";
	cout << endl;


	for (int r = 0; r < odd_Num; ++r) {
		for (int c = 0; c < odd_Num; ++c)
			if (c == odd_Num - 1)
				cout << "*|";
			else
				cout << "* ";
		cout << endl;
	}

	for (int c = 0; c < odd_Num; ++c)
		cout << "--";
	cout << endl;
}
Last edited on
Here is the code for the 3rd figure. If you can understand what I did you should be able to do the 2nd one yourself.

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
for(int r = 1; r <= odd_Num; r++)
{
	for(int c = 1; c <= odd_Num; c++)
	{
		//under mid
		if(r <= odd_Num/2)
			if(c >= r && (odd_Num - r + 2) > c)
				cout << "*";
			else
				cout << " ";
		//over mid
		else if ((r - 1) > odd_Num/2)
			if(c <= r && (odd_Num - r) < c)
				cout << "*";
			else
				cout << " ";
		//mid
		else 
			if(c == r) //centre
				cout << "*";
			else
				cout << " ";
		
		//add space or end of line
		if(c == odd_Num)
			cout << "|" << endl;
		else
			cout << " ";
	}
}
Can you explain this part of the code?

[code]
//under mid
if(r <= odd_Num/2)
if(c >= r && (odd_Num - r + 2) > c)
cout << "*";
else
cout << " ";
//over mid
else if ((r - 1) > odd_Num/2)
if(c <= r && (odd_Num - r) < c)
cout << "*";
else
cout << " ";
[code]

Our teacher is teaching computer science 1 like if we all already know the langue and its very frustrating.
Hi sorry for late reply hope it helps anyway.
I will try to explain with some examples.
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
/*
For the figure 3 you have to compute where you have to set a * depending on your r and c
in relation to odd_number.
odd_Number/2 returns the rounded down half of odd_Number so you have
under mid, mid and over mid
Example: odd_Number = 11
               under mid =< 5
               mid = 6
               over mid > 6
*/

//under mid
if(r <= odd_Num/2)
/*
For under mid you have to get smaller with higher row count.
Column (c) must be equal or higher than row (r) AND 
column must be lower than the total number of rows (odd_number) - row + 2
(+1 bacause c andr starts at 1 and +1 bacause c has to be lower).
Example: odd_Number = 11, r = 4 
               c >= r,  true for c = 4 to 11
               (11 - 4 +2) > c,  true for c = 1 to 8
               c = 4 to 11 AND 1 to 8 = 4 to 8
*/
if(c >= r && (odd_Num - r + 2) > c)
cout << "*";
else
cout << " ";


//over mid
else if ((r - 1) > odd_Num/2)
/*
For over mid you have to get wider with higher row count.
Column (c) must be equal or lower than row (r) AND 
column must be greater than the total number of rows (odd_number) - row.
Example: odd_Number = 11, r = 7
               c <= 7, true for c = 1 to 7
               (11 - 7) < c, true for c = 5 to 11
               c = 1 to 7 AND 5 to 11 = 5 to 7
*/
if(c <= r && (odd_Num - r) < c)
cout << "*";
else
cout << " ";
Topic archived. No new replies allowed.