Hi there, right now my teacher wants me to make an outline of a half-diamond with numbers, using for loops, kind of like the following:

1
 2
  3
   4
    5
   4
  3 
 2
1

The number 5 is a user input, so the half-diamond's width would be controlled by the user. I really don't know how to do this and would kindly request some help!

Right now my code looks like the following:

void fifthFunction(int number)
{
    const char SPACE = ' ';

    for(int counter = 0; counter <= number; counter++){
	for(int spaces = 0; counter > 0 && counter < number; spaces)
	   cout << SPACE;	
	for(int row = 1; row <= number; row++)
           cout << row;}
	   cout << endl;
				
			
				

}

To be honest, I really don't know what I'm doing with this problem and it's really stumping me. Any advice or help would be appreciated!

Recommended Answers

All 22 Replies

Here are some questions for you to answer?

1) Do you know how to print the first line of the required output?

2) Do you know how to print the second line?

3) If I give you a number n, do you know how to print the nth line?

Hmm. The first line should just be

for(int row = 1, row <= number, row++)
cout << row;

The second line and the nth is what really stumps me. I'm not sure how to incorporate the spaces in. I know it has to do with nested loops, I just don't know how to exactly go about it.
Thanks for the help!

Hey Kyren Your problem statement is quite simple. Just look at it in this way.

1, You need to count from 1 to a number 'n' and then count back from 'n' to one. This can be simply by 2 loops.
2, For a given number 'i', You need to print down 'i' spaces(' ') and then the number 'i'.
For eg: For a number 3, you need to print '3 spaces' followed by 3.

Now combine both of them and then you'll find that you've got your program done.

Hmm. The first line should just be

for(int row = 1, row <= number, row++)
cout << row;

Not even close.

The first line of output is

cout << 1 << "\n";

Right?

Well, I'm not allowed to hard-code anything so I can't just cout everything. I have to use all for loops for the assignment, unless the first cout statement leads into a for loop? I don't know. Um.. Sky, this is what I have for the first step.

1.

for(int counter = 0; counter <= number; counter-++)
for(int counter = number; counter >= 0; counter--)

The second step is the one I'm not sure about. Would I need two for loops nested?

Your first part you've done right.
However, You'll need one for loop for the 2nd part.

After you figure that out. Place the second part in your first for loops and then you're DONE !!

Please answer my question. Does the code fragment I posted correctly produce the first line of your desired output, or doesn't it? Ignore any additional constraints from your homework assignment. Once we get code that produces the correct output, we can worry about how to make it fit the other requirements.

Yes it does arkoenig.

Second line looks like this then:

cout << " " << 2 << endl;

And so on, using the spaces.

Thanks for the help man.

To Sky: Thank you, I'll try to figure out this loop as I refresh the page haha.

The second part can be divided into 2 parts again.
1. Print 'n' spaces
2. Print the number n and go to a newline :D

Sky Diploma is correct here. Also, note that the first line:

cout << 1 << endl;

can be thought of as:

1. print 0 spaces
2. print the number 1 and a newline

and, of course, "print 0 spaces" is a degenerate case of "print n spaces."

So... how would you code just the "print n spaces" part?

I'm not sure at all. It's the spaces that are getting me. I don't see how I can use a constant char SPACE and multiply them AND give the nth number without using a nested loop.

I got this for my code, or rather, what I tried to make:

const char SPACE = ' ';

for(int counter = 0; counter <= number; counter++){
  for(int i = counter - 1; i <= counter; i--)
     cout << SPACE;
     cout << counter;
}

I know that it's probably simple, I'm just not getting it. D:

You forgot to decrement i in the loop !!

Just fixed it, thanks for that. The whole for loop is probably wrong huh?

Do you know how to write a loop that does something n times?

Now that I think about it, I'm not very sure that I do.

Isn't a loop that counts up from 1 to n considered something that couts "n" times?

Such as this:

for(int i = 0; i <= number; i++)
cout << i

OK, then how do you print n spaces?

for(int i = 0, int i <= number, i++)
cout << " ";

correct?

Yes!!

Damn, I got it!

Thank you guys!!

help me to print half diamond out in numbers(I wanna print the down part)..

Welcome Sappy T,

I don't mean to sound brutal when you're new here, but:
1) The theme of the forums here on DaniWeb is "you do the work, we help you understand the concepts and spot errors in your code." We don't do your work for you.
2) Please don't post in months-old solved threads. Even if you have a question directly related to it, please start a new thread.

Read the "Read this before posting" thread at the top of the Forum list!

That said, since the thread is already solved, and many months ago, I'm going to say "No, read everything in this thread and do it yourself." If you get stuck, please start a new thread, and post your code, and we'll be happy to help you out.

check if till what number you need to form a diamond?
cout<<n<<"\t";

apply some character escape seqence..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.