How to print a diagonal of numbers in stars?


------------------------------------
Last edited on
try using a for loop within another for loop


fout << setfill('*');

for(index1 = 0; index1 < 6; index1++)
{
for(index2 = 5; index2 > 0; index2--)
{
fout << setw(index1) << "6" << setw(index2) << endl;
}
}

You may need to mess around with the individual values, but that's how it should probably work.
Thanks for reply,

but that number 6 should be the variable "Size"
and the row and col should match with the "Size"
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/178311/
This is a bit of code that I wrote that accomplishes this task using a few for-loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
		std::cout << "Enter the number of lines" << std::endl;
		std::cin >> num_lines;


		for (int k = 0; k < num_lines; k++)
		{
			for (int j = 0; j < k; j++)
			{
				std::cout << "*";
			}
			std::cout << num_lines;

			for (int j = num_lines - k; j >= 0; j--)
			{
				std::cout << "*";
			}

			std::cout << std::endl;

		}


Also, try to not post the same question in multiple places..
Last edited on
closed account (48T7M4Gy)
int num_lines = 0;

Otherwise what's the problem?
Last edited on
@JayZom

Thanks for reply,

Your code is great! but it is for "Pattern 1". My question is how to do the "Pattern 3"...

closed account (48T7M4Gy)
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 namespace std;

int main() {
    
    int num_lines = 0;
    
    std::cout << "Enter the number of lines: ";
		std::cin >> num_lines;


		for (int k = 0; k < num_lines; k++)
		{
			for (int j = 0; j < k; j++)
			    std::cout << "*";
			    
			std::cout << num_lines;

			for (int j = 0; j < num_lines - k - 1; j++)
			    std::cout << "*";
			
			std::cout << std::endl;

		}
	return 0;
}


Enter the number of lines: 6
6*****
*6****
**6***
***6**
****6*
*****6
 
Exit code: 0 (normal program termination)

Thank you Kemort and JayZom !!!! I really appreciate your help !!!!!!!!!
Why did you delete your question? It makes this thread impossible to understand, and ruins the usefulness of this thread for helping others learn.

It's a selfish abuse of this forum, and its users.
@ MikeyBoy: Probably because he is trying to hide the fact that someone else did his homework for him. This site should do like many other programming forums and restrict users ability to edit their posts. There's no good reason to allow it.
If anyone reads this: please don't delete your questions. Another person might come along with the same question and they might find the answers here very helpful - but now they cannot find this because the question was removed.
Probably because he is trying to hide the fact that someone else did his homework for him. 

Well, yeah. People shouldn't do that. You shouldn't just write chunks of code for people. Answer questions, explain things, point out mistakes, provide examples - but don't just write a chunk of code for someone to copy into their homework.

I already report users who delete their questions after getting answers. I sometimes wonder if I should start reporting the users who just write code for other people, enabling them to cheat their way through their schoolwork.
Topic archived. No new replies allowed.