Is there a simpler way to do this?

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
// HW 5.12b Write a program that displays the following 
// **********
// *********
// ********
// *******
// ******
// *****
// ****
// ***
// **
// *

#include <iostream>
using namespace std;


int main()
{
	
	for (int k =1; k > 0; --k){	
	
		for ( int i = 10;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 9;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 8;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 7;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 6;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 5;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 4;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 3;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 2;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
		for ( int i = 1;i > 0; --i ){		
		cout << "*";
		}
		for ( int j = 1;j > 0; --j ){		
		cout << "\n";
		}
	}
		
}// main 


The problem states that cout must be written like this cout "*"; and that nested for statements must be used.
Last edited on
Yes there is a much simpler solution.

When you write a for loop and you output a character to the screen, what does it look like? If you loop through and output '*' you will get.

**********

Here is sample to get you started on the easier solution minus the details you will have to change to get it right.

1
2
3
4
5
for(int i=0;i<cols;i++)
{
    for(int j = 0; j<rows; j++)
       std::cout << "*";
}


But you only need 2 loops.
What is the point of the outer loop across k if you never use the value of k? Since k has the values that you are hard coding to start off i and j (why are you alternating??) then you should use k:
for k across 10 to 1:
    for i across 0 to k:
        print *
    print new line
Last edited on
10 rows, maximum 10 cols.

Row 1 would get 10 cols.
Row 2 would get 9 cols.
Row 3 would get 8 cols.
.
.
.
.
row 10(this does not have to be zero indexed) would get 1 cols.

Are you seeing a pattern? The row should have an effect on the number of columns.

I couldn't get it to start a new line and create the pattern without alternating "/n" and "*"
Edited my post with something to help you.
Create your algorithm on paper. I think you'll find it's much simpler than you think
I don't get it. Each time i runs through it executes j and then j runs through it's entire sequence. The best I can get it to do is print a 10x10 square of asterisks
Got it. Thank you everyone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	

for(int i=0;i<10;i++)
{
	
    for(int j = (0+i); j<10; j++){
       cout << "*";
	
}
cout << "\n";
	}
}
Weird, I'm not seeing the value of j = (0+i), since 0 + i = i, regardless.
Topic archived. No new replies allowed.