i just need a good explenation for this code: #include <stdio.h> #include <math.
ID: 3830533 • Letter: I
Question
i just need a good explenation for this code:
#include <stdio.h>
#include <math.h>
#define SIZE 500
void preprocess(int brightness[SIZE][SIZE], int n)
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if (brightness[i][j] < 0)
{
double d = 0;
int count = 0;
// coordinate in above row
if (i - 1 >= 0)
{
d += brightness[i-1][j];
count++;
if (j -1 >= 0)
{
d+= brightness[i-1][j-1];
count++;
}
if (j + 1 < n)
{
d+= brightness[i-1][j+1];
count++;
}
}
// row just below
if (i + 1 < n)
{
d += brightness[i+1][j];
count++;
if (j - 1 >= 0)
{
d+= brightness[i+1][j-1];
count++;
}
if (j + 1 < n)
{
d+= brightness[i+1][j+1];
count++;
}
}
// coordinate just left
if (j - 1 >= 0)
{
d += brightness[i][j-1];
count++;
}
// coordinate just right
if (j + 1 < n)
{
d += brightness[i][j+1];
count++;
}
brightness[i][j] = (int)nearbyint(d/count);
}
}
}
}
int findBrightSpotInSmaller(int brightness[SIZE][SIZE], int i, int j, int *coord1, int *coord2)
{
int max = brightness[i][j];
*coord1 = i;
*coord2 = j;
int r, c;
for(r = i; r < i+5; r++)
{
for(c = j; c < j+5; c++)
{
if (brightness[r][c] == 0)
{
return -1;
}
if (brightness[r][c] > max)
{
max = brightness[r][c];
*coord1 = r;
*coord2 = c;
}
}
}
return max;
}
void findBrightSpots(int brightness[SIZE][SIZE], int n)
{
printf("Grid size: %dx%d ", n, n);
printf("Bright spots: ");
int i, j;
int reported[n][n];
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
reported[i][j] = 0;
}
}
for(i = 0; i <= n-5; i++)
{
for (j = 0; j <= n-5; j++)
{
int coord1, coord2;
int maxBright = findBrightSpotInSmaller(brightness, i, j, &coord1, &coord2);
if (maxBright != -1)
{
if(reported[coord1][coord2] != 1)
{
printf("[%2d,%2d] %2d ", coord1, coord2, maxBright);
reported[coord1][coord2] = 1;
}
}
}
}
}
int inputBrightness(int brightness[SIZE][SIZE])
{
int n;
scanf("%d", &n);
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &brightness[i][j]);
}
}
return n;
}
int main()
{
printf("Poject # ");
int brightness[SIZE][SIZE];
int n = inputBrightness(brightness);
preprocess(brightness, n);
findBrightSpots(brightness, n);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <math.h>
#define SIZE 500
// preprocess stage where it is calculating the brightness value by taking average of
// nearby cell if the cell value if less than 0
void preprocess(int brightness[SIZE][SIZE], int n)
{
int i, j;
// looping over 2 d array of brightness
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// checking if value needs to be changed.
if (brightness[i][j] < 0)
{
double d = 0;
int count = 0;
// coordinate in above row
if (i - 1 >= 0)
{
d += brightness[i-1][j];
count++;
if (j -1 >= 0)
{
d+= brightness[i-1][j-1];
count++;
}
if (j + 1 < n)
{
d+= brightness[i-1][j+1];
count++;
}
}
// row just below
if (i + 1 < n)
{
d += brightness[i+1][j];
count++;
if (j - 1 >= 0)
{
d+= brightness[i+1][j-1];
count++;
}
if (j + 1 < n)
{
d+= brightness[i+1][j+1];
count++;
}
}
// coordinate just left
if (j - 1 >= 0)
{
d += brightness[i][j-1];
count++;
}
// coordinate just right
if (j + 1 < n)
{
d += brightness[i][j+1];
count++;
}
brightness[i][j] = (int)nearbyint(d/count);
}
}
}
}
// checking for bright spot in 5*5 grid starting from given i, j parameter being its left and top point
int findBrightSpotInSmaller(int brightness[SIZE][SIZE], int i, int j, int *coord1, int *coord2)
{
// will hold value of max
int max = brightness[i][j];
// coord's are given as reference so that value of
// coordinate of max brightness can be returned back to caller function
*coord1 = i;
*coord2 = j;
int r, c;
// looping over 5*5 brightness matrix
for(r = i; r < i+5; r++)
{
for(c = j; c < j+5; c++)
{
// if there is any cell with 0 value it returns
if (brightness[r][c] == 0)
{
return -1;
}
// checking if current value if greater than max then resetting max parameters
if (brightness[r][c] > max)
{
max = brightness[r][c];
*coord1 = r;
*coord2 = c;
}
}
}
// returning max
return max;
}
// function to print all bright spots in brightness matrix
void findBrightSpots(int brightness[SIZE][SIZE], int n)
{
// printing grid information
printf("Grid size: %dx%d ", n, n);
printf("Bright spots: ");
int i, j;
// array to keep track of bright point already reported and initializing it to 0
// meaning nothing has been reported
int reported[n][n];
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
reported[i][j] = 0;
}
}
// looping over array to consider each i, j as top left point of a 5*5 matrix
// and finding bright spot there.
for(i = 0; i <= n-5; i++)
{
for (j = 0; j <= n-5; j++)
{
int coord1, coord2;
int maxBright = findBrightSpotInSmaller(brightness, i, j, &coord1, &coord2);
// if bright spot is found, report it and set reported true for thsi point in reported matrix.
if (maxBright != -1)
{
if(reported[coord1][coord2] != 1)
{
printf("[%2d,%2d] %2d ", coord1, coord2, maxBright);
reported[coord1][coord2] = 1;
}
}
}
}
}
// take input from user for the brightness matrix
int inputBrightness(int brightness[SIZE][SIZE])
{
int n;
scanf("%d", &n);
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &brightness[i][j]);
}
}
return n;
}
int main()
{
// print project title and all your info that you need as per assignment
printf("Poject # ");
// declare matrix, max allowed size is 500*500
int brightness[SIZE][SIZE];
// take user input
int n = inputBrightness(brightness);
// preprocess negative value to take average
preprocess(brightness, n);
// find and report bright spots
findBrightSpots(brightness, n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.