调用函数的问题 [英] problem in calling function
本文介绍了调用函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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--;
}
}
推荐答案
这不是真正的OOP:不要在类的功能中使类成员参数对象:doh:
并重写电话
Thats no real "OOP": dont make class members argument in functions of the object :doh:
and rewrite the call
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 ;-)
你问一对夫妇一样的问题几天前。在我的回答中:
如何调用此功能 [ ^ ]
解决方案3中的
,你会发现完整的故事,为什么用int **转移数组并不是一个好主意。
使用简单的int *,或者当你可以作为类matrix的成员变量访问它时,甚至更好地不将数组作为参数传递
,就像KarstenK建议的那样。
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 classmatrix
, just like KarstenK suggested.
这篇关于调用函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文