'How to find neighbors average in matrix and assign it to a real number struct?
I have this assignment for college where i recieve the matrix A and i need to create the matrix B, where in each cell there whould be the neighbor's average of a specific cell from matrix A.
for example: https://i.stack.imgur.com/1ZuRA.png
i have the real number struct which looks like this:
typedef struct fraction
{
int num, numerator, denominator;
} fraction;
The function is yet to be complete but problem is, its easy to find the num field, but i struggling to find the numerator and denominator fields...(below is my function, im not allowed to change the decleration):
fraction neighborFractionAverage(int A[][COLS], int i, int j, int rows, int cols)
{
// your code:
fraction result;
int counter = 0;
int mone;
result.num = 0, result.numerator = 1, result.denominator = 1;
if ((i - 1 >= 0 && i - 1 < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i - 1][j - 1];
}
if ((i - 1 >= 0 && i - 1 < rows) && (j >= 0 && j < cols))
{
counter++;
result.num += A[i - 1][j];
}
if ((i - 1 >= 0 && i - 1 < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i - 1][j + 1];
}
if ((i >= 0 && i < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i][j - 1];
}
if ((i >= 0 && i < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i][j + 1];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j - 1 >= 0 && j - 1 < cols))
{
counter++;
result.num += A[i + 1][j - 1];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j >= 0 && j < cols))
{
counter++;
result.num += A[i + 1][j];
}
if ((i + 1 >= 0 && i + 1 < rows) && (j + 1 >= 0 && j + 1 < cols))
{
counter++;
result.num += A[i + 1][j + 1];
}
result.num /= counter;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|