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). It should works for MxN dimension, but I don't know how. Any suggestions?
I need code in C++ .
4 replies to this topic
#1
Posted 05 January 2012 - 02:44 PM
|
|
|
#2
Posted 05 January 2012 - 03:35 PM
Can you give an example of what you're trying to do? I'm really not clear on it.
#3
Posted 05 January 2012 - 03:52 PM
This is a matrix example
1, 2, 3,
4, 5, 6,
7, 8, 9
the result is 1, 4, 7, 8, 9, 6, 3, 2, 5
I should write code which will work for any dimension of matrix, for example 4x4, 4x5...
1, 2, 3,
4, 5, 6,
7, 8, 9
the result is 1, 4, 7, 8, 9, 6, 3, 2, 5
I should write code which will work for any dimension of matrix, for example 4x4, 4x5...
#4
Posted 06 January 2012 - 04:46 AM
Here I have code in C, I need help to translate this in assembly emu 8086. Please help me if you know.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int **mat; // Pointer to pointer
int rows, cols, i, j;
printf("How many rows you want ");
scanf("%d", &rows);
//rows = 10;
//cols = 10;
mat = malloc(rows*sizeof(int*)); // array of number of rows
printf("How many cols ");
scanf("%d", &cols);
for (i=0; i<rows; i++) { // for each row ...
mat[i] = malloc(cols * sizeof(int)); // add these many cols
}
for (i = 0; i<rows; i++) {
for (j = 0; j<cols; j++) {
mat[i][j] = (i+1) * (j+1);
printf("%4d ", mat[i][j]); //these two print lines
//printf("%4d ", *(*(mat+i)+j)); //do the same thing
}
putchar('\n');
}
for(i=0;i<rows;i++) //free malloc'd memory
free(mat[i]); //rows first
free(mat); //then the array pointer itself
printf("\n\n");
return 0;
}
#5
Posted 07 January 2012 - 03:49 AM
I don't know what your last post means. The given code doesn't compile for me.
As for spiral matrix's I've found a decent tutorial, It's in C but it's easy to understand and rewrite in C++
Here : Technical Interview Questions: Print 2D Array Matrix Spiral Order
Hope this helps.
As for spiral matrix's I've found a decent tutorial, It's in C but it's easy to understand and rewrite in C++
Here : Technical Interview Questions: Print 2D Array Matrix Spiral Order
Hope this helps.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









