Home > Back-end >  2D Array Average - Convolution - C Program
2D Array Average - Convolution - C Program

Time:11-18

enter image description here

I need help with my program. I need it to calculate the 3x3 average and then go and and calculate the next. This is what i got so far, it‘s only to calculate the average of all and now I‘m stuck…

#include <stdio.h>
#define ROWS 5
#define COLS 7

int main(void){
    float in_sum = 0;
    float *in_matrix[ROWS][COLS];
    float in_avg;

    float matr[ROWS][COLS]={{1.5, 5, 6, 12, 13, 7, 80},
                            {50, 6.5, 23, 77, 17, 8.5, 28},
                            {43.5, 78, 8, 9, 34.5, 10, 95},
                            {75, 44, 40, 29, 39, 5, 99.5},
                            {18, 86, 68, 92, 10.5, 11, 4}};

    printf("Matrix Input:\n");

        for(int i = 0; i < ROWS; i  ){
            for (int j = 0; j < COLS; j  ){
                printf("%.2f ", matr[i][j]);
                    if(j==6){
                        printf("\n");
                    }
            }
        }
        printf("\nMatrix Output: \n");
        int j = 0, nr = 3, nc = 3;
        for (int i = 0; i < nr; i  ){
            for(j = 0; j < nc; j  ){
                in_sum = in_sum   matr[i][j];
            }
        }
        in_avg = in_sum/(ROWS*COLS);
        for (int i=0; i< ROWS; i  ){
            for (int j=0; j< COLS; j  ){
            printf("%.2f", in_avg);
            }
            printf("\n");
        }
        in_matrix[ROWS][COLS] = &in_sum;
    return 0;
}

CodePudding user response:

Updating the Sum value when the computation range moves to right.

#define ROWS (5)
#define COLS (7)

float SumOf3Elem( const float *p ){ return p[0] p[1] p[2];  }
float Add4th_Sub1st( float S, const float *p ){ return S   p[3] - p[0]; }

int main()
{
    float M[ROWS][COLS] = {
        {1.5, 5, 6, 12, 13, 7, 80},
        {50, 6.5, 23, 77, 17, 8.5, 28},
        {43.5, 78, 8, 9, 34.5, 10, 95},
        {75, 44, 40, 29, 39, 5, 99.5},
        {18, 86, 68, 92, 10.5, 11, 4}
    };

    for( int Top=0; Top 2<ROWS;   Top )
    {
        float Sum = SumOf3Elem(M[Top])   SumOf3Elem(M[Top 1])   SumOf3Elem(M[Top 2]);
        printf( "%f ", Sum/9.0f );

        for( int Left=0; Left 3<COLS;   Left )
        {
            for( int i=0; i<3;   i )
            {   Sum = Add4th_Sub1st( Sum, M[Top i] Left );  }

            printf( "%f ", Sum/9.0f );
        }
        printf( "\n" );
    }

    return 0;
}

CodePudding user response:

The code you shared isn't even close to being working. Try this:

#include <stdio.h>

#define COLS 7
#define OVER 3
#define ROWS 5

float sum(size_t cols, float *matrix) {
    float s = 0;
    for(size_t row = 0; row < OVER; row  ) {
        for(size_t col = 0; col < OVER; col  ) {
            s  = matrix[row * cols   col];
        }
    }
    return s / (OVER * OVER);
}

int main(void) {
    float matrix[ROWS][COLS] = {
        {1.5, 5, 6, 12, 13, 7, 80},
        {50, 6.5, 23, 77, 17, 8.5, 28},
        {43.5, 78, 8, 9, 34.5, 10, 95},
        {75, 44, 40, 29, 39, 5, 99.5},
        {18, 86, 68, 92, 10.5, 11, 4}
    };
    for(size_t row=0; row < ROWS - OVER   1; row  ) {
        for(size_t col=0; col < COLS - OVER   1; col  ) {
            printf("%.1f%c",
                sum(COLS, &matrix[row][col]),
                col   1 < COLS - OVER   1 ? ' ' : '\n');
        }
    }
}


and example run:

24.6 24.9 22.2 20.9 32.6
40.9 34.9 30.7 25.4 37.4
51.2 50.4 36.7 26.7 34.3

As the input matrix is hard-coded this works, but if you need a generic function you should check that the input is at least a OVER x OVER matrix.

The sum() performs 8 additions for matrix[0][0] and then another 8 additions for the matrix[0][1]. We could clearly do better. For example by retaining the last sum and subtract what is no longer in the window and add what now is. Caching of partial results.

  • Related