Click here to Skip to main content
15,885,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
1       6       10      13      15
2       7       11      14
3       8       12
4       9
5


This is how the output should look like

What I have tried:

I tried to find a specific pattern that follows in the rows and columns and found:
1. Number in the successive rows increase by 1
2. Number in the columns increase as (+5,+4,+3,+2......)
Posted
Updated 30-Jul-21 19:30pm

1 solution

Think of it this way: assuming you have to print this in a console, then you need to print it as complete rows (or it gets complex: you need to create a 2D array, fill it, and then print that. While that is actually pretty simple to to, if you haven't reached that part of your course yet it's probably beyond you for the moment).

So look at the data differently:
1      Col1+nCol1   Col2+nCol2   Col3+nCol3   Col4+nCol4
2      Col1+nCol1   Col2+nCol2   Col3+nCol3
3      Col1+nCol1   Col2+nCol2
4      Col1+nCol1   
5
Where "nColx" is "the number of rows in Column x", and "Colx" is "the value in Column x of this row".
So that works out to:
1    1+5   1+5+4   1+5+4+3  1+5+4+3+2
2    2+5   2+5+4   2+5+4+3
3    3+5   3+5+4
4    4+5
5
Now, that's got a readable pattern that is easy to automate for any number of rows!
 
Share this answer
 
Comments
Atul Shreewastav 31-Jul-21 1:35am    
can you help me with some code? i am just getting started to learn c and i find it really difficult to build such kind of logic
OriginalGriff 31-Jul-21 1:41am    
Not really - this is your homework, not mine!
And the whole point of the homework is to get you to use what you have seen in class and start working out how to implement things like this.

Have a look here:
https://www.codeproject.com/Articles/5282084/How-to-Write-Code-to-Solve-a-Problem-A-Beginners-G
It uses C# as the example language, but that's pretty much like C from your POV - it's the principles behind it that matter!

Give it a try, see how far you can get.
Atul Shreewastav 31-Jul-21 1:44am    
Thanks man!
Appreciate it.
OriginalGriff 31-Jul-21 1:47am    
You're welcome!

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