我是一名有用的助手,可以翻译文本。
在这里,我将数组名称作为参数传递,我认为这是按引用传递。但是我无法添加数组。在add函数中出现了问题,我无法添加数组。
您可以使用int*
或int[][]
,但您需要根据每种情况的要求编写程序。我将为您展示两种情况的示例。
关于您的程序
- 不要使用
void
。它通常是浪费,有时是错误。返回值。
- 编写通用代码。比较:
void display(int* sum)
{
printf("The sum is:\n");
for(int i =0;i<3;i++)
{
for(int j =0;j<3;j++)
{
printf("%d ",(sum[i]+j));
}
printf("\n");
}
}
使用
void display_a(int s[][3], int l, const char* msg)
{
if( *msg != 0) printf("%s\n",msg);
for(int i =0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%d ", s[i][j]);
}
printf("\n");
}
printf("\n");
return;
};
在第二种情况下,您可以使用相同的函数来显示任何数组
s
并编写简单的标题。请注意,在向量中,您可以省略除最后一个维度以外的所有内容。实际上,C语言没有多维数组,例如FORTRAN所具有的功能。通过将行数和消息作为参数传递,您可以获得比您所写的更多的东西。
始终测试scanf()的返回值。scanf()不是用来从键盘读取输入的。在每个提示中使用至少一个空格。这对用户来说更加舒适。
- 使用常量来测试您的程序。避免为用户读取输入,因为这将浪费大量测试时间。
一个例子
输出
Using arrays
Matriz A at [1][1] 1
Matriz A at [1][2] 2
Matriz A at [1][3] 3
Matriz A at [2][1] 4
Matriz A at [2][2] 5
Matriz A at [2][3] 6
Matriz B at [1][1] 6
Matriz B at [1][2] 5
Matriz B at [1][3] 4
Matriz B at [2][1] 3
Matriz B at [2][2] 2
Matriz B at [2][3] 1
The 1st matrix:
1 2 3
4 5 6
The 2nd matrix:
6 5 4
3 2 1
The sum (using arrays) is:
7 7 7
7 7 7
Using pointers
Matriz A at [1][1] 2
Matriz A at [1][2] 3
Matriz A at [1][3] 4
Matriz A at [2][1] 5
Matriz A at [2][2] 6
Matriz A at [2][3] 7
Matriz B at [1][1] 7
Matriz B at [1][2] 6
Matriz B at [1][3] 5
Matriz B at [2][1] 4
Matriz B at [2][2] 3
Matriz B at [2][3] 2
The 1st matrix:
2 3 5
5 6 7
The 2nd matrix:
7 6 4
4 3 2
The sum (using pointers) is:
9 9 9
9 9 9
Using t1 t2
The t1 matrix:
1 2 3
4 5 6
7 8 9
The t2 matrix:
9 8 7
6 5 4
3 2 1
t1 + t2 (using pointers) is:
10 10 10
10 10 10
10 10 10
代码
这段代码应该使用了我试图解释的所有内容。当使用指针时,请注意编写代码的方式:只需使用地址和偏移量,无需使用括号。
#include<stdio.h>
void add_a(int[][3],int[][3],int[][3],int);
void display_a(int[][3],int,const char*);
int input_a(int[][3], int, const char*);
void add_p(int*,int*,int*,int);
void display_p(int*,int,const char*);
int input_p(int*, int, const char*);
int main(void)
{
int t1[][3] =
{
{ 1,2,3 },
{ 4,5,6 },
{ 7,8,9 }
};
int t2[][3] =
{
{ 9,8,7 },
{ 6,5,4 },
{ 3,2,1 }
};
int a[3][3];
int b[3][3];
int sum[3][3];
printf("Using arrays\n\n");
input_a(a,2,"Matriz A");
input_a(b,2,"Matriz B");
display_a(a, 2, "The 1st matrix:");
display_a(b, 2, "The 2nd matrix:");
add_a( a,b,sum, 2 );
display_a(sum, 2, "The sum (using arrays) is:");
printf("Using pointers\n\n");
input_p((int*) a,2,"Matriz A");
input_p((int*) b,2,"Matriz B");
display_p( (int*) a, 2, "The 1st matrix:");
display_p( (int*) b, 2, "The 2nd matrix:");
add_p( (int*) a, (int*) b, (int*) sum, 2 );
display_p( (int*) sum, 2, "The sum (using pointers) is:");
printf("\nUsing t1 t2 \n\n");
display_p( (int*) t1, 3, "The t1 matrix:");
display_p( (int*) t2, 3, "The t2 matrix:");
add_p( (int*) t1, (int*) t2, (int*) sum, 3 );
display_p( (int*) sum, 3, "t1 + t2 (using pointers) is:");
return 0;
};
void add_a(
int a[][3],
int b[][3],
int sum[][3],
int l
)
{
for(int i =0;i<l;i++)
for(int j=0;j<3;j++)
sum[i][j] = a[i][j] + b[i][j];
return;
};
void display_a(int s[][3], int l, const char* msg)
{
if( *msg != 0) printf("%s\n",msg);
for(int i =0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%d ", s[i][j]);
}
printf("\n");
}
printf("\n");
return;
};
int input_a(int a[][3], int l, const char* msg )
{
for(int i = 0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%s at [%d][%d] ", msg, i+1,j+1);
int res = scanf("%d",&a[i][j]);
if ( res != 1 ) return -1;
}
};
return 0;
};
void add_p( int*a, int* b, int* sum, int l )
{
for(int i =0;i<l;i++)
for(int j=0;j<3;j++)
*(sum + i*l + j) = *(a + i*l + j) + *(b + i*l + j);
return;
};
void display_p(int* s, int l, const char* msg)
{
if( *msg != 0) printf("%s\n",msg);
for(int i =0;i<l;i++)
{
for(int j =0;j<3;j++)
printf("%d ", *( s + i*l + j) );
printf("\n");
};
printf("\n");
return;
};
int input_p(int* a, int l, const char* msg )
{
for(int i = 0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%s at [%d][%d] ", msg, i+1,j+1);
int res = scanf("%d", (a + i*l + j) );
if ( res != 1 ) return -1;
}
};
return 0;
};