input
,output
,compute
和main
printf
或scanf
函数中没有compute
和main
我的代码当前给我错误:
#include <stdio.h>
void input(int* arr[50][50],int* r,int* c);
int compute(int* r,int* c,int result);
int ouput(int* arr[50][50],int* r,int* c,int result);
int main(void)
{
int arr_in[50][50];
int result = 0;
int r_in = 0;
int* r = &r_in;
int c_in = 0;
int* c = &c_in;
input(arr_in,&r_in,&c_in);
compute(&r_in,&c_in,result);
output(arr_in,&r_in,&c_in,result);
}
void input(int* arr[50][50],int* r,int* c)
{
printf("Enter the no. of rows: ");
scanf("%d",c);
printf("Enter the no. of columns: ");
scanf("%d",r);
printf("Enter the elements of the matrix\n");
for(int i = 0;i < *r;i ++)
{
for(int j = 0;j < *c;j++)
{
printf("Enter element[%d][%d]: ",i + 1,j + 1);
scanf("%d",arr[i][j]);
}
}
}
int compute(int* r,int* c,int result)
{
if(*r == *c)
{
result = 1;
return result;
}
else
{
result = 0;
return result;
}
}
int output(int* arr[50][50],int* r,int* c,int result)
{
if(result == 1)
{
for(int i = 0;i < *r;i++)
{
for(int j = 0;j < *c;j++)
{
if(i==j)
printf("%d\t",*arr[j][i]);
else
printf("\t");
}
printf("\n");
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}
return 0;
}
错误:
>diagonal.c:20:11: error: incompatible pointer types passing 'int [50][50]' to parameter of type 'int *(*)[50]'
[-Werror,-Wincompatible-pointer-types]
input(arr_in,&r_in,&c_in);
^~~~~~
diagonal.c:3:17: note: passing argument to parameter 'arr' here
void input(int* arr[50][50],int* r,int* c);
^
diagonal.c:22:11: error: incompatible pointer types passing 'int [50][50]' to parameter of type 'int *(*)[50]'
[-Werror,-Wincompatible-pointer-types]
ouput(arr_in,&r_in,&c_in,result);
^~~~~~
diagonal.c:6:16: note: passing argument to parameter 'arr' here
int ouput(int* arr[50][50],int* r,int* c,int result);
^
2 errors generated.
答案 0 :(得分:0)
您刚刚传递了错误的参数。如果将2D数组作为int arr[50][50]
传递,则编译器会将其作为指向指针的指针处理,您无需在数据类型之后添加(*)。因此原型必须如下:
void input(int arr[50][50],int* r,int* c);
int ouput(int arr[50][50],int* r,int* c,int result);
第二点是compute
函数。您更改compute
函数的局部变量而不是传递的变量。因此您应该按如下方式更改原型:
int compute(int* r,int* c,int *result);
并按如下所示调用函数:
compute(&r_in,&c_in,&result);
最后,整个代码如下:
#include <stdio.h>
void input(int arr[50][50],int* r,int* c);
void compute(int* r,int* c,int *result);
int ouput(int arr[50][50],int* r,int* c,int result);
int main(void)
{
int arr_in[50][50];
int result = 0;
int r_in = 0;
int* r = &r_in;
int c_in = 0;
int* c = &c_in;
input(arr_in,&r_in,&c_in);
compute(&r_in,&c_in,&result);
output(arr_in,&r_in,&c_in,result);
}
void input(int arr[50][50],int* r,int* c)
{
int i, j;
printf("Enter the no. of rows: ");
scanf("%d",c);
printf("Enter the no. of columns: ");
scanf("%d",r);
printf("Enter the elements of the matrix\n");
for(i = 0;i < *r;i ++)
{
for(j = 0;j < *c;j++)
{
printf("Enter element[%d][%d]: ",i + 1,j + 1);
scanf("%d",&arr[i][j]);
}
}
}
void compute(int* r,int* c,int *result)
{
if(*r == *c)
{
*result = 1;
}
else
{
*result = 0;
}
}
int output(int arr[50][50],int* r,int* c,int result)
{
int i, j;
if(result == 1)
{
for(i = 0;i < *r;i++)
{
for(j = 0;j < *c;j++)
{
if(i==j)
printf("%d\t",arr[j][i]);
else
printf("\t");
}
printf("\n");
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}
return 0;
}
答案 1 :(得分:0)
以下建议的代码:
现在,建议的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_VALUES 50
void input( int arr[ MAX_VALUES ][ MAX_VALUES ], int* r, int* c );
int compute( int r, int c );
void output( int arr[ MAX_VALUES ][ MAX_VALUES ], int r, int c, int result );
int main(void)
{
int arr_in[ MAX_VALUES ][ MAX_VALUES ];
int r_in;
int c_in;
input( arr_in, &r_in, &c_in );
int result = compute( r_in, c_in );
output( arr_in, r_in, c_in, result );
}
void input( int arr[ MAX_VALUES ][ MAX_VALUES ], int *r, int *c )
{
do
{
printf("Enter the no. of rows, in range 1...%d inclusive\n", MAX_VALUES );
*r = -1;
if( scanf( "%d", r ) != 1 )
{
fprintf( stderr, "scanf failed to input number of rows\n" );
exit( EXIT_FAILURE );
}
} while( *r <= 1 || *r > MAX_VALUES );
do
{
*c = -1;
printf("Enter the no. of columns, in range 1...%d inclusive\n", MAX_VALUES );
if( scanf("%d", c) != 1 )
{
fprintf( stderr, "scanf failed to input number of columns\n" );
exit( EXIT_FAILURE );
}
} while( *c <= 1 || *c > MAX_VALUES );
printf("Enter the elements of the matrix\n");
for(int i = 0;i < *r;i ++)
{
for(int j = 0;j < *c;j++)
{
printf("Enter element[%d][%d]: ", i + 1, j + 1 );
if( scanf( "%d", &arr[i][j]) != 1 )
{
fprintf( stderr, "scanf failed to input a data point\n" );
exit( EXIT_FAILURE );
}
}
}
}
int compute( int r, int c )
{
return( r == c )? 1 : 0;
}
void output( int arr[ MAX_VALUES ][ MAX_VALUES ], int r, int c, int result )
{
if( result == 1 )
{
for( int i = 0; i < r; i++ )
{
for( int j = 0; j < c; j++ )
{
printf( "%d\t", arr[i][j] );
}
printf("\n");
}
}
else
{
printf("\nMatrix is not a Square Matrix.\n");
}
}