Click here to Skip to main content
15,889,631 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
class matrix
{
public:
int row,col,mat[100][100];
void spiraltraversal(int **,int,int);
void zigzagtraversal(int **,int,int);
};
void main()
{
matrix a;
clrscr();
printf("Enter the no. of rows=");
scanf("%d",&a.row);
printf("Enter the no. of columns=");
scanf("%d",&a.col);
printf("The order of matrix is %d*%d\n",a.row,a.col);
srand(time(NULL));
int i,j;
for(i=0;i<a.row;i++)>
 {
  for(j=0;j<a.col;j++)>
   {
    a.mat[i][j]=rand()%100+1;
    printf("%u\t",a.mat[i][j]);
   }
  printf("\n");
 }

 a.spiraltraversal(&&a.mat,a.row,a.col); //error :  expression syntax
 
getch();
}

void matrix::spiraltraversal(int **arr,int n,int m)
{

  int rs=0,cs=0;
  int re=n-1,ce=m-1;

  while(rs<=re && cs<=ce)
  {

    int i=rs,j=cs;

    for(j=cs;j<=ce;j++)
    printf(" %u ",arr[i][j]);

    for(j=rs+1,j--;i<=re;i++)
    printf(" %u  ",arr[i][j]);

    for(j=ce-1,i--;j>=cs;j--)
    printf(" %u ",arr[i][j]);

    for(i=re-1,j++;i>=rs+1;i--);
    printf(" %u ",arr[i][j]);

    rs++; cs++; re--; ce--;
   }
}
Posted
Updated 23-Aug-14 17:19pm
v5
Comments
[no name] 23-Aug-14 14:12pm    
And what you expect us to do with this? Your code is not compilable, no description of a problem anywhere, no description of what your code is supposed to do, nothing about what your code is doing.
Member 11018648 23-Aug-14 14:35pm    
sorry
In this program I want to print the matrix of random numbers and spiral traversing of matrix
But the problem is in calling function "spiraltraverse"
[no name] 23-Aug-14 16:17pm    
http://www.eskimo.com/~scs/cclass/int/sx9a.html
Sergey Alexandrovich Kryukov 23-Aug-14 23:18pm    
I fixed some formatting of the code for you, but it's still poor.
This is a code dump, not a question.
—SA

You are asking the same question as a couple of days ago. In my answer to it:
How Do I Call This Function[^]
in solution 3, you will find the full story, why transferring the array by int** is no good idea.

Use a simple int* instead, or even better don't pass the array as parameter when you can access it as a member variable of class matrix, just like KarstenK suggested.
 
Share this answer
 
Thats no real "OOP": dont make class members argument in functions of the object :doh:

and rewrite the call

C++
a.spiraltraversal();


the implementation should look like this
void matrix::spiraltraversal()
{
 
  int rs=0,cs=0;
  int re=row-1,ce=col-1;
 
  while(rs<=re && cs<=ce)
  {
 
    int i=rs,j=cs;
 
    for(j=cs;j<=ce;j++)
    printf(" %u ",mat[i][j]);
 
    for(j=rs+1,j--;i<=re;i++)
    printf(" %u  ",mat[i][j]);
 
    for(j=ce-1,i--;j>=cs;j--)
    printf(" %u ",mat[i][j]);
 
    for(i=re-1,j++;i>=rs+1;i--);
    printf(" %u ",mat[i][j]);
 
    rs++; cs++; re--; ce--;
   }
}

or similar ;-)
 
Share this answer
 
Comments
Usman Hunjra 24-Aug-14 17:18pm    
+5 ..

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