使用固定大小的数组时为垃圾值,而如果使用动态大小的数组则结果正确

问题描述 投票:0回答:1
#include<stdio.h>

void readMatrix(int m,int n,int a[][n]);
void displayMatrix(int m,int n,int a[][n]);
void addMatrix(int m,int n,int a[][n],int b[][n]);

int main()
{
    int a[5][5],b[5][5],m,n,p,q;
    printf("Enter the no. of rows and columns of matrix A\n");
    scanf("%d%d",&m,&n);

    printf("Enter the no. of rows and columns of matrix B\n");
    scanf("%d%d",&p,&q);

    if(m!=p||n!=q)
    {
        printf("Matrix addition not possible\n");
        return 0;
    }

    printf("Enter %d elements to matrix A\n",m*n);
    readMatrix(m,n,a);

    printf("Enter %d elements to matrix B\n",m*n);
    readMatrix(p,q,b);

    printf("Matrix A\n");
    displayMatrix(m,n,a);

    printf("Matrix B\n");
    displayMatrix(p,q,b);

    addMatrix(m,n,a,b); 
}

void readMatrix(int m,int n,int a[m][n])
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
}
void displayMatrix(int m,int n,int a[m][n])
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}


void addMatrix(int m,int n,int a[m][n],int b[m][n])
{
    int c[5][5]; //I guess this is causing problem
    //int c[m][n]; //gives correct answer
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
    }

    printf("Sum Matrix\n");

    /*Gives correct result
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",c[i][j]);
        }
        printf("\n");
    }
    */

    displayMatrix(m,n,c);
}

displayMatrix(m,n,c)是正确的,如果我将数组定义为c[m][n],则上面给出了代码。

如果我使用c[5][5],它将给出垃圾值。

我正在使用gcc编译器。我在addMatrix函数本身中显示了总和,这给出了正确的结果。无法找出错误。请帮助。谢谢。

c arrays function
1个回答
0
投票

例如,如果m6n7,会发生什么?

您的循环将到达数组外部的元素。这是不确定的行为。

因此,您可以用初始化数组时使用的静态大小替换mn

女巫不是很好,因为数组的大小取决于用户输入的大小。

您为纠正错误并在mn上保持数组大小动态减少而进行的操作是正确的解决方法。

因此,如果我理解正确,您希望将值保存在结果矩阵中,然后通过函数main将其打印在displayMatrix()函数中。

最简单的方法是在array函数中声明main()并将其作为参数传递给addMatrix()函数,而不用过多地干扰您的代码。

最后一件事,您应该使用更有意义的名称来命名变量,例如abc之类的名称非常容易混淆。

在此处实时查看:

Live sample

#include <stdio.h>

void readMatrix(int m, int n, int a[][n]);
void displayMatrix(int m, int n, int c[][n]);
void addMatrix(int m, int n, int a[][n], int b[][n], int c[][n]);

int main()
{
    int m, n, p, q;
    printf("Enter the no. of rows and columns of matrix A\n");
    scanf("%d%d", &m, &n);
    printf("Enter the no. of rows and columns of matrix B\n");
    scanf("%d%d", &p, &q);

    int a[m][n], b[p][q], result[m][n];

    if (m != p || n != q)
    {
        printf("Matrix addition not possible\n");
        return 0;
    }

    printf("Enter %d elements to matrix A\n", m * n);
    readMatrix(m, n, a);

    printf("Enter %d elements to matrix B\n", m * n);
    readMatrix(p, q, b);

    printf("Matrix A\n");
    displayMatrix(m, n, a);

    printf("Matrix B\n");
    displayMatrix(p, q, b);

    addMatrix(m, n, a, b, result);

    printf("Sum Result\n");
    displayMatrix(m, n, result);
}

void readMatrix(int m, int n, int a[m][n])
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
}
void displayMatrix(int m, int n, int a[m][n])
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
}

void addMatrix(int m, int n, int a[m][n], int b[m][n], int c[m][n])
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.