Jump to content

matrix with dimensions m x n in C++

- - - - -

  • Please log in to reply
4 replies to this topic

#1
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts
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++ .

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Can you give an example of what you're trying to do? I'm really not clear on it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts
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...

#4
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts
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
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
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.
Remebre about KISS & DRY




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users