Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

matrix with dimensions m x n in C++

matrix

  • Please log in to reply
4 replies to this topic

#1 zemzela

zemzela

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

Posted 05 January 2012 - 02:44 PM

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++ .
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 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

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 zemzela

zemzela

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

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...
  • 0

#4 zemzela

zemzela

    CC Regular

  • Member
  • PipPipPip
  • 30 posts

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;
}

  • 0

#5 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

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.
  • 0
Remebre about KISS & DRY





Also tagged with one or more of these keywords: matrix

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download