【发布时间】:2018-02-12 08:44:06
【问题描述】:
我目前的问题是在 C 中找到方阵的转置。我使用的算法是创建一个二维数组来存储我想找出其转置的矩阵。然后我调用一个实际转置它的函数(因为数组是通过引用传递的),以便在调用函数后它被转置。 函数代码如下:-
//transpose of n by n matrix
#include<stdio.h>
void transpose(int arr[int][int]); //prototype declaration line 3
void transpose(int arr[int r][int c]){ //line 4
int i,j;
int matB[c][r];
for(i=0;i<c;i++){
for(j=0;j<r;j++){
matB[i][j]=arr[j][i];
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
arr[i][j]=matB[i][j];
}
}
}
int main(){
int n,i,j;
printf("enter the dimensions of square matrix :-");
int mat[n][n];
scanf("%d",n);
printf("please enter the elements of the matrix :-->\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&mat[i][j]);
}
}
transpose(mat);
printf("the transpose of the given matrix is:- \n ");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d\t",mat[n][n]);
}
printf("/n");
}
}
但我收到以下错误:-
第 3 行:-[错误] 'int' 之前的预期表达式
第 4 行:- [错误] 'int' 之前的预期表达式
第 3 行和第 4 行对应函数 void() 的原型声明和代码中的函数定义
我目前使用 DEV C++ 作为编译器.. 我的程序的快照:- click here to see the snapshot of my program
这里是我编译时遇到的错误:- click here to see the scanpshot of error i got
【问题讨论】:
-
语法根本上是错误的。你必须研究数组和函数是如何工作的。我们无法在 SO 帖子中教给您所有这些内容,因为这个话题太宽泛了。
标签: c