Home > Back-end >  Why is my matrix in my function and in main different values?
Why is my matrix in my function and in main different values?

Time:12-04

So I'm writing a simple C program that essentially creates a 2D array and allows the user to input values into the 2D array. Then other functions find the smallest and largest value within that array, as well as their position in the array. When I print the matrix in the function, it prints correctly as it should. However, whenever I print it in main as a test or try to access it in my other functions, my array goes from 1, 2, 3, 4, etc. to 1, 1, 1, 1. I used the same function in a previous code I wrote, and it worked just fine, so I'm kind of stumped. Also, I'm not allowed to modify main, I just put a simple loop to print the array there as a test. This is my first time posting here, so I apologize if my formatting is wrong. Any help would be greatly appreciated.

Here is my code:

#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;
}

Code for the same function I used on my previous problem:

#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;
}

Output when printed from generateMTX function:

1 2 3
4 5 6
7 8 9
10 11 12 

Output when printed from main:

1 1 1
1 1 1
1 1 1
1 1 1

CodePudding user response:

This is your Problem:

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

You are overwriting the contents of your array in elementPosition

It should be:

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

CodePudding user response:

As you wrote in your current code

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];
}

When you write if (arr[i][j] = num) , it overwrites the value of the array since = is assigning operator and == is used for equal condition checking

  •  Tags:  
  • c
  • Related