Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Using for, while and do while loops in c language, can someone help me to print this program. please I really need help.

1 9 2 8 3 7 4 5 6
2 8 3 7 4 6 5
3 7 4 6 5
4 6 5
5

What I have tried:

C++
#include <stdio.h>
#include <conio.h>

int main()
{
	for (int q = 1; q < 6; q++)
	{	
		printf("%d ", q);
		
		for (int w = 10; w > 5; w--)
		{
			printf("%d ", w--);
			//printf("\n");
		}
		
		for (int a = q + 1; a < 6; a++)
		{
			printf("%d ", a);
		}	
		printf("\n");
	}
	return 0;
}
Posted
Updated 26-Nov-20 20:39pm
v2

In order to print such a pattern, you have to, well..., find a pattern.
That is, while the console pointer is at position (row, col) you should know what you have to print.
In order to find out such a pattern, it is useful to show the pattern together with the console coordinates (rows and columns)
   col  0 1 2 3 4 5 6 7 8
   =====================
row  |
   0 |  1 9 2 8 3 7 4 5 6
   1 |  2 8 3 7 4 6 5
   2 |  3 7 4 6 5
   3 |  4 6 5
   4 |  5


I see a 'conditional pattern' emerging, that is a different pattern for even and odd column numbers
  • even columns (col = 0, 2, 4, ...): p(row, col) = 1 + row + col/2;
  • odd columns (col = 1, 3, 5, ...): p(row, col) = 9 - row - col/2;


Let's verify it on two example cases

even column: p(2,4) = 1 + 2 + 4/2 = 1 + 2 + 2 = 5
odd column: p(1,5) = 9 - 1 - 5/2 = 9 - 1 - 2 = 6

You may check those below.
    0 1 2 3 4 5 6 7 8

0   1 9 2 8 3 7 4 5 6
1   2 8 3 7 4 6 5
2   3 7 4 6 5
3   4 6 5
4   5


Now writing the corresponding C code is up to you.
 
Share this answer
 
Comments
TheRealSteveJudge 27-Nov-20 2:49am    
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
5*
CPallini 27-Nov-20 3:24am    
Thank you!
Patrice T 27-Nov-20 5:55am    
+5
CPallini 27-Nov-20 7:32am    
Thank you!
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Hint: There are two sequences on each line: one rising, one falling. Start by writing the code to do the rising sequence for a single line, and when that's working, add the falling sequence. (You can do this in a single loop, there are many different ways.) When both of them are working, add the code to print multiple lines.
This isn't difficult, it just takes a little thinking about before you dive into code!
 
Share this answer
 
Comments
kael rasseru 29-Nov-20 2:05am    
hey! Thanks for your tips, but still I can't get the right one. here's my code. can you give more tips and hints to me? I appreciate your help!

#include <stdio.h>
#include <conio.h>

int main()
{
int q = 1, w = 9;
int a, s;
while (q < 6, w > 5)
{
a = q + 1;
s = w - 1;
printf("%d ", q);
q++;
printf("%d ", w);
w--;

while (a < 6, s > 5)
{
printf("%d ", a);
a++;
printf("%d ", s);
s--;
}
printf("\n");
}
}
OriginalGriff 29-Nov-20 3:19am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?
What does the debugger show you is happening?
kael rasseru 29-Nov-20 3:36am    
i want to ask why it didn't print number 5? can you give me hints??
there's no error message, i also expect that it will work but doesn't.
OriginalGriff 29-Nov-20 3:53am    
First off, the output you show in your original post is almost certainly wrong, the first line should be
"1 9 2 8 3 7 4 5 6"
to fit in with the rest of the lines and the sequence.

Secondly, that is what the debugger is there *for* - to help you find out what you wrote that was wrong!
So break out the debugger and start single stepping through your code to find out exactly what is going on.

Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Get used to the debugger, it's your best friend, and you will be using it a lot, we all do - time for you to learn a new (and very, very useful) skill: debugging!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900