Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I wants to print matrix such that

Consider that original matrix look like following

1 2 3 
4 5 6
7 8 9


Now I want to print like following

step1: print 1

step2: print 2

step3: print 3

step4: print 6

step5: print 9

step6: print 8

step7: print 7

step8: print 4

step9: print 5

1 2 3
    6  first step
    9

1 2 3
    6 second step
  8 9


1 2 3
    6 3rd step
7 8 9

1 2 3
4   6 4th step
7 8 9

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


What I have tried:

i have tried to display the code above manner?
Posted
Updated 23-Sep-16 4:07am
v5
Comments
[no name] 23-Sep-16 8:33am    
I hate to be the one to tell you this, but the isn't a single loop, inner or outer, in the code that you have shown us that demonstrates whatever your problem is.
[no name] 23-Sep-16 8:35am    
suppose i have matrix A(3*3).
i want to print the that matrix in following manner:-

for an example:

->A11,A12,A13,A23,A33,A32,A31,A21,A22 how can i print the matrix in following manner.
Leo Chapiro 23-Sep-16 8:44am    
As @NotPolitcallyCorrect already said, show us your code please, we can't read your mind, dude!
[no name] 23-Sep-16 8:46am    
As per my company policy it is not possible show my code to you.
Leo Chapiro 23-Sep-16 8:50am    
But it seems to be possible (as per your company policy) to ask for help in this forum. And there are rools here: you need to show what you already achieved before ask for code ...

First of all... sorry but one note aside:
That your company rules don't allow to show code... it is perfectly understable and I find it very good that you follow the rules.
But...
You are asking for help, repeating 3 times the same sentence doesn't clarify anything.
If you don't want to show the code of the company, then don't do it, but at least you still can use pseudo-code to explain the steps you are doing and where are you getting problems
Learn how to use the debugger. This is something you should be able to solve on your own actually pretty quick

Please don't take it personally, it is not an attack against you, just a philosophical question: Are you sure you are prepared to do this job?
And I am not speaking about the technical content, everyone is a newbie at the beggining. I am thinking on other habilities... i.e. curiosity, tenacity, logical thinking and others related.

I would recommend you to try it first on your own and learn from your mistakes, this is the best school you can get. Don't give up easily and come here to ask. Of course it is confortable and you will probably always find someone that answers you. The most important point is... you won't learn so much and you will miss a very big reward for your soul, that is the satisfaction of being able to solve things on your own.

----------------------
Now to the technical side. I am not going to give you a ready solution, and probably there are better ways to solve this, but... it is still a way that you can infere / guess just taking paper and pencil and doing 2 or 3 examples to search for a pattern:

Assuming a Matrix A(MxN) no matter M and N values

1) Think on going through your matrix as squares / rectangles, each of them is one full iteration. You will need so many iterations as the ((smallest index / 2) + (smalles index % 2))
i.e.
A(2x6) = 2/2 + 2%2 = 1 rectangle or iteration
A(6x3) = 3/2 + 3%2 = 2 rectangles or iterations
A(7x7) = 7/2 + 7%2 = 4 rectangles or iterations

2) How do you do each square?
Line 1: From A(1+squareNr, 1+squareNr) to A(1+squareNr, N-squareNr)
Line 2: From A(1+squareNr, N-squareNr) to A(M-squareNr, N-squareNr)
Line 3: From A(M-squareNr, N-squareNr) to A(M-squareNr, 1+squareNr)
Line 4: From A(M-squareNr, 1+squareNr) to A(1+(squareNr+1), 1+squareNr)

3) You end when you reach the last element = M*N
A(2x6) = 12
A(6x3) = 18
A(7x7) = 49

4) Now you only have to figure out how to use the loops and the conditions to manage this. Point 2) is considering 0-based iterations, adapt it if you prefer start counting in 1

edits to correct spelling and make sentence more coherent
 
Share this answer
 
v4
Comments
Leo Chapiro 23-Sep-16 10:06am    
Words of wisdom, +5 :)
Nelek 23-Sep-16 10:14am    
Thank you
This smells of homework, so no code!
It's not difficult, it's just cumbersome.
Declare eight variables: X, Y, dX, dY, minX, maxX, minY, maxY.
Set them to 0, 0, 1, 0, 0, width, 0, height respectively.
Now set up a loop to look at each cell: width * height.
Inside the loop, print the X,Y cell.
Now add dX to X and dY to Y.
Check X:
If it's equal to maxX then reduce it by one, increment y by one, increment minX, and set dX to 0, dY to 1.
If it's equal to minX then increment it by one, reduce y by one, decrement maxY, and set dX to 0, dY to -1.
Check Y:
If it's equal to maxY then reduce it by one, decrement x by one, decrement maxX, and set dX to -1, dY to 0.
If it's equal to minY then increment it by one, increment x by one, increment minX, and set dX to 1, dY to 0.

That should do it.
 
Share this answer
 
v2
Comments
Nelek 23-Sep-16 10:14am    
Different ways, same result :)
Yours looks more professional, have a +5
Nelek 23-Sep-16 11:47am    
Only one thing... you have 2 maxX, I suppose you forgot one minX
OriginalGriff 23-Sep-16 11:48am    
Oops! The second maxX should have been a maxY - fixed, thank you!

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