C语言 为什么我的矩阵在我的函数中和在main中有不同的值?

ncgqoxb0  于 2022-12-11  发布在  其他
关注(0)|答案(2)|浏览(100)

因此,我编写了一个简单的C程序,它实际上创建了一个2D数组,并允许用户将值输入到2D数组中。然后,其他函数会找到该数组中的最小值和最大值,以及它们在数组中的位置。当我在函数中打印矩阵时,它会正确地打印出来。然而,每当我在main中打印它作为测试,或者试图在其他函数中访问它时,我的数组从1,2,3,4等到1,1,1,1。我在之前写的代码中使用了相同的函数,它工作得很好,所以我有点难住了。另外,我不允许修改main,我只是放了一个简单的循环来打印数组作为测试。这是我第一次在这里发帖,所以如果我的格式有误,我向你道歉。2任何帮助都将不胜感激。
下面是我的代码:

#include <stdio.h>
#define ROWS 4
#define COLS 3

void generateMtx(int mtx[ROWS][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            printf("Enter row %d, column %d: ", i, j);
            scanf("%d", &mtx[i][j]);
        }
    }
    
    //Test print in function
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            printf("%d ", mtx[i][j]);
        }
        printf("\n");
    }
    

}

int matrixSmallest(int arr[ROWS][COLS])
{
    int smallest = arr[0][0];
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            if (smallest > arr[i][j])
            {
                smallest = arr[i][j];
            }
        }
    }
    return  smallest;
}

int matrixLargest(int arr[ROWS][COLS])
{
    int largest = arr[0][0];
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            if (largest < arr[i][j])
            {
                largest = arr[i][j];
            }
        }
    }
    return  largest;
}

int elementPosition(int arr[ROWS][COLS], int num, int pos[2])
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            if (arr[i][j] = num)
            {
                pos[0] = i;
                pos[1] = j;
            }
        }
    }
    return  pos[2];
}

int main()
{
    int mtx[ROWS][COLS];
    generateMtx(mtx);
    int smallest = matrixSmallest(mtx);
    int smallPosition[2] = {-1, -1};
    elementPosition(mtx, smallest, smallPosition);
    int largest = matrixLargest(mtx);
    int largePosition[2] = {-1, -1};
    elementPosition(mtx, largest, largePosition);
    printf("Largest element: %d\n", largest);
    printf(" found at row %d, column %d\n", largePosition[0], largePosition[1]);
    printf("Smallest element: %d\n", smallest);
    printf(" found at row %d, column %d\n", smallPosition[0], smallPosition[1]);
    
    //Test print in main
    //Can't modify main  
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            printf("%d ", mtx[i][j]);
        }
        printf("\n");
    }

    return 0;
}

我在前面的问题中使用的相同函数的代码:

#include <stdio.h>

#define ROWS 5
#define COLS 3

float generateMtx(float arr[ROWS][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            printf("Enter row %d, column %d: ", i, j);
            scanf("%f", &arr[i][j]);
        }
    }
    printf("\n");
    
    return  arr[ROWS][COLS];
}

float columnAverages(float arr[ROWS][COLS], float colavg[COLS])
{
    float sum = 0;
    float avg = 0;
    for (int i = 0; i < COLS; i++)
    {
        for (int j = 0; j < ROWS; j++)
        {
            sum += arr[j][i];
        }
        avg = sum/5.0;
        colavg[i] = avg;
        sum = 0;
    }
    
    return colavg[COLS];
}

float rowAverages(float arr[ROWS][COLS], float rowavg[ROWS])
{
    float sum = 0;
    float avg = 0;
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            sum += arr[i][j];
        }
        avg = sum/3.0;
        rowavg[i] = avg;
        sum = 0;
    }
    
    return rowavg[ROWS];
}

void regionAverage(float arr[ROWS][COLS], int top, int bottom, int left, int right)
{
    printf("\n\nEnter top region boundary: ");
    scanf("%d", &top);
    printf("Enter bottom region boundary: ");
    scanf("%d", &bottom);
    printf("Enter left region boundary: ");
    scanf("%d", &left);
    printf("Enter right region boundary: ");
    scanf("%d", &right);
    
    float sum = 0;
    float avg = 0;
    float count = 0;
    for (int i = top; i <= bottom; i++)
    {
        for (int j = left; j <= right; j++)
        {
            sum += arr[i][j];
            count++;
        }
    }
    avg = sum/count;
    
    printf("Region average: %.1f", avg);
}

int main(void)
{
    float mtx[ROWS][COLS];
    generateMtx(mtx);
    float rowavg[ROWS];
    float colavg[COLS];
    int top, bottom, left, right;
    
    columnAverages(mtx, colavg);
    rowAverages(mtx, rowavg);
    
    printf("         ");
    for (int c = 0; c < 3; c++)
    {
        printf("Col %d   ", c);
    }
    
    printf("\n");
    
    for (int i = 0; i < ROWS; i++)
    {
        printf("Row %d ", i);
        for (int j = 0; j < COLS; j++)
        {
            printf("%8.1f", mtx[i][j]);
        }
        printf("\n");
        
    }
    printf("\n");
    
    printf("         ");
    for (int c = 0; c < 3; c++)
    {
        printf("Col %d   ", c);
    }
    printf("  Avg");    
    printf("\n");
    
    for (int i = 0; i < ROWS; i++)
    {
        printf("Row %d ", i);
        for (int j = 0; j < COLS; j++)
        {
            printf("%8.1f", mtx[i][j]);
        }
        printf("%8.1f", rowavg[i]);
        printf("\n");
        
    }
    printf("  Avg ");
    
    for (int i = 0; i < COLS; i++)
    {
        printf("%8.1f", colavg[i]);
    }
    
    regionAverage(mtx, top, bottom, left, right);
    return 0;
}

从generateMTX函数打印时的输出:
从主打印时的输出:

lb3vh1jj

lb3vh1jj1#

这是你的问题:

if (arr[i][j] = num)

您正在覆盖elementPosition中数组的内容
它应该是:

if (arr[i][j] == num)
w8biq8rn

w8biq8rn2#

正如您在当前代码中所写

int elementPosition(int arr[ROWS][COLS], int num, int pos[2])
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            if (arr[i][j] = num)
            {
                pos[0] = i;
                pos[1] = j;
            }
        }
    }
    return  pos[2];
}

当您写入if (arr[i][j] = num)时,它会覆写数组的值,因为= is assigning operator== is used for equal condition checking

相关问题