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