for example a number with four neighbors should return the average of those 4 neighbors, instead it is not. A number with 3 neighbors should return the average of those 3 neighbors.
this though is only working for corner values and top bottom values. The sides and the middle are not calculating correctly. Here is the output.
please enter number of columns and rows
5 5
1 83 92 66 38
87 27 98 36 80
54 55 33 97 5
26 93 40 79 55
21 34 54 85 25
The smoothed image is
85 40 82 55 73
14 89 51 81 20
71 38 83 24 76
73 40 68 64 52
30 56 53 52 70
as you can see the 1 in the first matrix has 83 and 87 as its neighbors and returns 85 correctly in the 2nd matrix, the second number 87 has 1,27,54 as its neighbors but incorrectly returns 14 as the average. Can someone take a look at my code below and please fix this with either an edit, or detailed instructions, I've been looking at this for hours and can't seem to understand the problem. I will be eternally grateful if you can fix this!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
// function that randomly generates numbers
void fillArray(int a[10][20], int m, int n)
{
int random;
int i,j;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
random=rand()%100;
a[i][j]=random;
}
}
}
// function that prints the first matrix of random numbers
void printarray (int a[10][20], int m, int n)
{
int i,j;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
}
// function that finds the mean for any number and its 4 nieghbors
void corner1 (int a[10][20], int c[10][20], int n, int m)
{
int i,j;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (i<=0&&j<=0){
c[i][j]=(a[i+1][j]+a[i][j+1])/2;
}
}
}
}
void middle(int a[10][20], int c[10][20], int n, int m)
{
int i,j;
for (i=1;i<m-1;i++)
{
for (j=1;j<n-1;j++)
{
c[i][j]=(a[i-1][j]+a[i][j-1]+a[i+1][j]+a[i][j+1])/4;
}
}
}
void side1 (int a[10][20],int c[10][20], int n, int m)
{
int i,j;
for (i=1;i<m-1;i++)
{
for (j=0;j<n-1;j++)
{
if (i>=1&&j<=0){
c[i][j]=(0+0+a[i-1][j]+a[i+1][j]+a[i][j+1])/3;
}
}
}
}
void corner2(int a[10][20], int c[10][20], int m, int n)
{
int i,j;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (i>=1 && j>=0){
c[i][j]=(a[i-1][j]+a[i][j+1])/2;
}
}
}
}
void top (int a[10][20], int c[10][20], int m, int n)
{
int i,j;
for (i=0;i<m;i++)
{
for (j=1;j<n-1;j++)
{
c[i][j]=(a[i][j-1]+a[i][j+1]+a[i+1][j])/3;
}
}
}
void bottom (int a[10][20], int c[10][20], int m, int n)
{
int i,j;
for (i=1;i<m;i++)
{
for (j=1;j<n;j++)
{
c[i][j]=(a[i][j-1]+a[i-1][j]+a[i][j+1])/3;
}
}
}
void side2(int a[10][20], int c[10][20], int m, int n)
{
int i,j;
for (i=1;i<m-1;i++)
{
for (j=0;j<n;j++)
{
c[i][n-1]=(0+0+a[i-1][j]+a[i+1][j]+a[i][j-1])/3;
}
}
}
void corner3(int a[10][20], int c[10][20], int m, int n)
{
int i,j;
for (i=1;i<m;i++)
{
for (j=0;j<n;j++)
{
c[i][n-1]=(a[i-1][j]+a[i][j-1])/2;
}
}
}
void corner4(int a[10][20], int c[10][20], int m, int n)
{
int i,j;
for (i=0;i<m-1;i++)
{
for (j=0;j<n;j++)
{
c[i][n-1]=(a[i+1][j]+a[i][j-1])/2;
}
}
}
int main()
{
int a[10][20];
int c[10][20];
int m,n;
srand(time(NULL));
//User input
printf("please enter number of columns and rows\n");
scanf("%d %d", &m,&n);
fillArray(a,m,n);
printarray (a,m,n);
printf("The smoothed image is\n");
corner1(a,c,m,n);
side1(a,c,m,n);
middle (a,c,m,n);
corner2(a,c,m,n);
top(a,c,m,n);
bottom(a,c,m,n);
side2(a,c,m,n);
corner3(a,c,m,n);
corner4(a,c,m,n);
printarray(c,m,n);
getch();
return 0;
}

New Topic/Question
Reply



MultiQuote





|