printing retangle

I am trying to print a rectangle which allow the user to input width and height. And it should composed of 0 and 1. In the i-th row of the rectangle, the first i-th elements will be 1, other elements will be 0. If the i-th row has less than i elements, fill the whole row with 1. I have no idea how to write this programme by "for" loop. Can someone help me?


example:
width: 5
height: 3
10000
11000
11100
width: 5

width: 3
height: 5
100
110
111
111
111
Could you make a rectangle so I can see what they should look like?
these are the examples, width and height are the cin by the user



width: 5
height: 3

10000
11000
11100

width: 3
height: 5

100
110
111
111
111
Ask yourself:
(1) How many rows will it have (the outer loop)?
(2)(a) How many 1s will it print (minimum of current row and number of columns/width) - first inner loop
(2)(b) How many 0s will it print (number of columns - number of 1s printed) - second inner loop

Make sure that you end the line (at end of outer loop)
that what I have written, but I have no idea how to write the code of the "for" loop of "1" and "0". Also, is my structure correct?





#include <iostream>
using namespace std;
int main() {
int width, height;

cout<<"width: ";
cin>>width;
cout<<"height: ";
cin>>height;

for(int h=0; h<height; h++){

for(int w=0; w<width; w++){
for(){
cout<<"1";
}

for(){
cout<<"0";
}
}cout<<endl;
}



}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	int i, j;
	int width, height;
	cout << "Enter width : "; cin >> width;
	cout << "Enter height : "; cin >> height; cout << endl;

	for(i = 0; i < height; i++)
	{
		for(j = 0; j <= i && j < width; j++)
		{
			cout << "1";
		}

		for(j = j; j < width; j++)
		{
			cout << "0";
		}

		cout << endl;
	}
Enter width : 8
Enter height : 5

10000000
11000000
11100000
11110000
11111000

Topic archived. No new replies allowed.