I need help with output manipulation.

So I have to write a code that lets the user input an integer called size and i have to display a a square so that the length and width are determined by size. but i have to have what ever number size is, to the run diagonally through the square. for example if the user pick 4 it would display
###4
##4#
#4##
4###
all i can figure out is the first line and the last line. I am having trouble getting the number to move diagonally and I am having trouble getting the fill character to go after the number. my code looks like:



[code]
cout << "Please pick a size, from 1 through 9:" << endl;
cin >> size;
cout << setw(size) << setfill('#') << size << endl;
cout << left << setw(size) << setfill('#') << size << endl;


you should have a look at for loops.
if you still cant figure out how to do it, ask again
Nested loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>     // std::cout, std::endl

int main(){

	int x = 4;

	for (int r = 0; r < x; r++){
		std::cout << std::endl;
		for (int c = 0; c < x; c++)
			if (r == c)
				std::cout << x;
			else
				std::cout << '#';
	}
	std::cout << std::endl;
	return 0;
}


Should be able to figure out how to do the diagonal the other way now.
Last edited on
That helps but how do i get the number to move diagonally through the output like:
###4
##4#
#4##
4###

when i run that for loop it gives me

####
####
####
####
Last edited on
Try 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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
	int size = 0;

	cout << "Please pick a size from 1 through 9: ";
	cin >> size;
	cout << endl;

	for (int x = 0; x < size; x++)
	{
		for (int y = 0; y < size; y++)
		{
			if (x == y)
				cout << size;
			else
				cout << '#';
		}
		cout << endl;
	}

	return 0;
}


If you enter 4 the output will be

4###
#4##
##4#
###4

It is a different angle then what you wanted but I'm not sure how to get it to go the other way.

Hope this helps! =)

Topic archived. No new replies allowed.