954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

matrix with dimensions m x n in C++

Matrix given in memory; the objective is to print the spiral in opposite direction from clockwise (left column down the right lower range, right up column, a series of upper left, etc. until you get to the environment). This works just for dimension 3x3. It should works for MxN dimension, but I don't know how. Any suggestions?

I need code in C++ for this matrix:

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


the result is 1, 4, 7, 8, 9, 6, 3, 2, 5

kikic
Light Poster
28 posts since Dec 2011
Reputation Points: 7
Solved Threads: 0
 

If you have it working for a 3 x 3, post the code for that and perhaps people can offer advice for how to generalize to M x N.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

I wrote code in assembly, and ask for help, but just few people know how to read code in assembly 8086. so i don't get the advice :( That is why i ask for C++ help... I will try to do that in assembly. is it useful to paste the code in assembly?thanks in advance

kikic
Light Poster
28 posts since Dec 2011
Reputation Points: 7
Solved Threads: 0
 
; multi-segment executable file template.

data segment
matrix db 1, 2, 3, 4, 5, 6, 7, 8, 9 ; load matrix in memory

ends

stack segment
dw 128 dup(0)
ends

code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax



mov bx, matrix ; move matrix to offset bx
mov ch, 3
mov cl, 0


COLUMNAHEAD: 

mov dl, [bx] ; get the first element of matrix in dl
add dl, 30h ; add to show number
mov ah, 02h
int 21h ; print first number
inc cl 

cmp cl, ch ; compare if the counter is at the end of column


jge ROWAHEAD ; if greater go to row
add bx, 3 ; if not inc offset for 3 
jmp COLUMNAHEAD





ROWAHEAD:
inc cl ; 1 element of roe
inc bx ; inc offset for one place
mov dl, [bx] ; get the number in dl
add dl, 30h ; convert to number
mov ah, 02h
int 21h ; print the number

cmp cl, 5 
je COLUMNAHEAD
jne ROWAHEAD 


COLUMNBACK:
inc cl 
sub bx, 3
mov dl, [bx]
add dl, 30h
mov ah, 02h
int 21h 
cmp cl, 7
jne COLUMNBACK 
je ROWBACK

ROWBACK:
dec bx 
mov dl, [bx] 
add dl, 30h
mov ah, 02h
int 21h 
JMP MIDDLE


MIDDLE:
add bx, 3
mov dl, [bx] 
add dl, 30h
mov ah, 02h
int 21h 

JMP END

END: 


this is the code i wrote. it works for the matrix 
1, 2, 3,
4, 5, 6, 
7, 8, 9 and print 1, 4, 7, 8, 9, 6, 3, 2, 5
kikic
Light Poster
28 posts since Dec 2011
Reputation Points: 7
Solved Threads: 0
 

Wrong forum. This is the C++ forum.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

I know that is C++, and I need to write the code in C++, Could someone help?

kikic
Light Poster
28 posts since Dec 2011
Reputation Points: 7
Solved Threads: 0
 
I know that is C++, and I need to write the code in C++, Could someone help?


First post quote...This works just for dimension 3x3. It should works for MxN dimension

That implies you have it working in C++ for 3 x 3 and need help generalizing it. No, it doesn't work for 3 x 3. If it worked for 3 x 3, you would have posted the C++ code for 3 x 3, not the Assembly code. It may work just great for 3 x 3 in Assembly, but who cares in the C++ forum? Do you know how to code at all in C++?

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

It is useful for me, if you write me, code in C++ for this task, and I translate in assembly... Please help me if you know, it is very important

kikic
Light Poster
28 posts since Dec 2011
Reputation Points: 7
Solved Threads: 0
 
It is useful for me, if you write me, code in C++ for this task, and I translate in assembly... Please help me if you know, it is very important

People here will give you suggestions to help you but I don't think that anyone will do the work for you

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: