c语言中两个矩阵求和时的错误
我是C语言的新手,我想写一个程序来添加两个矩阵 计划1c语言中两个矩阵求和时的错误,c,C,我是C语言的新手,我想写一个程序来添加两个矩阵 计划1 #include <stdio.h> int main(){ int m,n,o,p,i,j; int mat1[m][n]; int mat2[m][n]; int result[m][n]; printf("enter the number of rows and columns for matrix "); scanf("%i%i",&m,&n); printf("enter elements of mat
#include <stdio.h>
int main(){
int m,n,o,p,i,j;
int mat1[m][n];
int mat2[m][n];
int result[m][n];
printf("enter the number of rows and columns for matrix ");
scanf("%i%i",&m,&n);
printf("enter elements of matrix one :");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%i",&mat1[i][j]);
}
}
printf("enter the elements of matrix two:");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%i",&mat2[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
result[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("the sum of the matrices are");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%i",result[i][j]);
}
}
return 0;
}
代码:
#包括
int mat_条目(int m,int n)
{
printf(“输入矩阵的行和列”);
scanf(“%i%i”、&m和&n);
int mat[m][n];
对于(int i=0;i您的程序1碰巧没有产生错误。您是不幸运的。您使用具有自动存储持续时间的未初始化变量中的值调用了未定义的行为,这些值是不确定的。您必须在读取m
和n
后声明mat1
、mat2
和结果
int main(){
int m,n,o,p,i,j;
printf("enter the number of rows and columns for matrix ");
scanf("%i%i",&m,&n);
/* move these declaretions after reading number of rows and columns */
int mat1[m][n];
int mat2[m][n];
int result[m][n];
printf("enter elements of matrix one :");
您的程序2通过使用没有返回语句的函数的返回值来调用未定义的行为。同时使用两个类型为int的操作数的[]
运算符也是错误的。E1[E2]
相当于(*((E1)+(E2))
(6.5.2.1数组下标,第2段),因此它的一个操作数必须是指针(包括从数组转换的操作数)
返回矩阵作为函数的返回值,考虑使用结构和元素的内存分配。 您使用了
int mat1=mat_entry(a,b);
int mat2=mat_entry(c,d);
不要将参数从一个函数发送到另一个函数,这只有在变量是指针时才起作用,在这里,你要么使用mat1
和mat2
作为指针,要么让它们成为整数矩阵。我的朋友,没有一件事你做错了。
您甚至没有使用动态内存分配,但仍尝试使用尚未定义的值声明矩阵。请尝试运行您的代码,即使您的第一个代码也不会运行。
我已为您更正了代码:
#include <stdio.h>
int main(){
int m,n;
printf("enter the number of rows and columns for matrix ");
scanf("%i%i",&m,&n);
int mat1[m][n];
int mat2[m][n];
int result [m][n];
int i,j;
printf("enter elements of matrix one :");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&mat1[i][j]);
}
}
printf("enter the elements of matrix two:");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
result[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("the sum of the matrices are");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d",result[i][j]);
}
}
return 0;
}
#包括
int main(){
int m,n;
printf(“输入矩阵的行数和列数”);
scanf(“%i%i”、&m和&n);
int mat1[m][n];
int mat2[m][n];
int结果[m][n];
int i,j;
printf(“输入矩阵一的元素:”);
对于(i=0;i而言,除非您使用动态分配(在这个阶段,您可能不想这样做),否则不可能编写一个函数来生成具有用户提供的维度的矩阵
幸运的是,您不需要这样做!因为您只需要添加矩阵,它们应该具有相同的维度,并且您只需向用户询问一次维度是什么。然后,填写两个矩阵,并执行“添加”部分
即:
int main()
{
int a,b,c,d,e,f,m,n;
printf("enter the rows and columns of matrix ");
scanf("%i%i",&m,&n);
int res[m][n];
int mat1[m][n];
int mat2[m][n];
mat_entry(m, n, mat1); // fill matrix 1
mat_entry(m, n, mat2); // fill matrix 2
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
res[i][j]=(mat1[i][j]+mat2[i][j]);
}
}
}
里面的代码与您建议的相同,减去询问维度:
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("\nenter the %i'th element %i'th row :",j+1,i+1);
scanf("%i",&mat[i][j]);
}
}
for(int i=0;iYou应该在开始编写这样的代码之前研究一下指针和作用域……为什么在编写C代码时要使用cpp
扩展?我在Dev-C++编译器中写了这篇文章
int main()
{
int a,b,c,d,e,f,m,n;
printf("enter the rows and columns of matrix ");
scanf("%i%i",&m,&n);
int res[m][n];
int mat1[m][n];
int mat2[m][n];
mat_entry(m, n, mat1); // fill matrix 1
mat_entry(m, n, mat2); // fill matrix 2
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
res[i][j]=(mat1[i][j]+mat2[i][j]);
}
}
}
void mat_entry(int m,int n, int mat[m][n])
{
...
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("\nenter the %i'th element %i'th row :",j+1,i+1);
scanf("%i",&mat[i][j]);
}
}