Write a C program that detects and reports on areas of brightness in the night s
ID: 3830906 • Letter: W
Question
Write a C program that detects and reports on areas of brightness in the night sky. You will input a portion of the night sky (from standard input) represented by an n × n grid of integers where n <= 500. Each value in the grid will indicate the detected brightness of that portion of the night sky with a 0 value indicating no light. Unfortunately the detection hardware sometimes reports erroneous values; these values will show up as negative numbers and must be ltered out prior to nding the bright spots. After preprocessing your data to lter out all of the noise (more on this below), your program must report the brightest coordinate and the brightness value for that coordinate in every 5×5 patch of the sky that contains no zeros. Report “(none)” if no bright spots exist. Keep in mind that bright areas may overlap, and that any bright spot should only be reported once. You should also report bright spots of the 5×5 areas you encounter as you scan the data in a top to bottom, left to right order
You should preprocess your data to eliminate noise in following manner: for every negative value you encounter, average the (maximum of) 8 squares around it and replace the value with the average (rounded o to the nearest whole number; use nearbyint from math.h). If one of the surrounding squares also is negative, use the value of 0 in computing the average.
Your input will consist of a single integer s representing the size of the grid followed by s×s data values. Your output should be presented exactly like the sample run (using your name, of course, instead of mine). You also must use functions to input your data into an array, to preprocess your array, and one to nd and report the bright spots. In other words you should have at least 3 separate functions dened that each are passed your two-dimensional data array where one function only inputs the array, one function only preprocesses the data to eliminate noise, and one function only nds and reports the bright spots. These functions must all be called from main. You must dene your array local to your main function and pass it into all functions that need it. You may not declare and use any global data (other than possibly preprocessor denitions to dene the maximum size of the sky and debugging ags if used).
2 Sample Run
Data le:
20
0 0 0 0 0 0 0 13 18 18 15 5 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 6 16 19 16 6 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 12 16 18 17 5 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 12 14 12 14 9 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 8 10 4 6 10 0 0 0 0 0 0
0 0 0 0 3 3 5 3 -2 10 12 11 11 6 0 0 0 0 0 0
0 0 0 0 5 6 6 6 5 7 13 14 13 4 0 0 0 0 0 0
0 0 0 0 2 7 8 6 4 8 12 13 11 9 0 0 0 0 0 0
0 0 0 0 2 7 7 7 5 9 4 10 6 4 0 0 0 -2 0 0
0 0 0 0 4 4 3 4 3 6 6 4 5 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 6 8 8 7 5 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 5 7 9 8 6 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 5 7 7 8 3 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 3 5 4 4 4 0 0 0 0 0 0 0
0 0 0 0 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 -1 -1 -2 8 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 -1 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0
the code the you gonna write i need it completely different then this code, i don't want it the same as this one:
#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 MAX 500
void get_data( int data[MAX][MAX] , int dimension);
void show_result( int data[MAX][MAX], int dimension);
void find_and_print_brightest( int data[MAX][MAX], int row, int column , int marked[MAX][MAX] )
{
int i,j, m, n;
m = 0;n = 0;
for ( i = 0; i < 5;i++ )
for ( j = 0; j < 5; j++ )
{
if ( data[row+i][column+j] == 0 )
return;
if ( data[row+m][column+n] < data[row+i][column+j] )
m = i; n = j;
}
if( marked[row+m][column+n] == 0 )
{
// mark the spot and print
marked[row+m][column+n] =1;
printf( "[%2d,%2d] %2d ", row+m, column+n, data[row+m][column+n] );
}
}
void filter_errors( int data[MAX][MAX], int dimension )
{
int i, j, k;
float tempF;
int buf[MAX][MAX];
// correct the corners
if( data[0][0] < 0 )
{
tempF = 0;
tempF += (data[0][1] > 0)?data[0][1]:0;
tempF += (data[1][0] > 0)?data[1][0]:0;
tempF += (data[1][1] > 0)?data[1][1]:0;
buf[0][0] = nearbyint( tempF / 3 );
}
else
buf[0][0] = data[0][0];
if ( data[0][dimension-1] < 0 )
{
tempF = 0;
tempF += (data[0][dimension-2] > 0)?data[0][dimension-2]:0;
tempF += (data[1][dimension-1] > 0)?data[1][dimension-1]:0;
tempF += (data[1][dimension-2] > 0)?data[1][dimension-2]:0;
buf[0][dimension-1] = nearbyint( tempF / 3 );
}
else
buf[0][dimension-1] = data[0][dimension-1];
if( data[dimension-1][0] < 0 )
{
tempF = 0;
tempF += (data[dimension-1][1] > 0)?data[dimension-1][1]:0;
tempF += (data[dimension-2][0] > 0)?data[dimension-2][0]:0;
tempF += (data[dimension-2][1] > 0)?data[dimension-2][1]:0;
buf[dimension-1][0] = nearbyint( tempF / 3 );
}
else
buf[dimension-1][0] = data[dimension-1][0];
if( data[dimension-1][dimension-1] < 0 )
{
tempF = 0;
tempF += (data[dimension-1][dimension-2] > 0)?data[dimension-1][dimension-2]:0;
tempF += (data[dimension-2][dimension-1] > 0)?data[dimension-2][dimension-1]:0;
tempF += (data[dimension-2][dimension-2] > 0)?data[dimension-2][dimension-2]:0;
buf[dimension-1][dimension-1] = nearbyint( tempF / 3 );
}
else
buf[dimension-1][dimension-1] = data[dimension-1][dimension-1];
// correct the edges
for ( i = 1; i < dimension -1; i++ )
if ( data[0][i] < 0 )
{
tempF = 0;
tempF += (data[0][i-1] > 0)?data[0][i-1]:0;
tempF += (data[0][i+1] > 0)?data[0][i+1]:0;
tempF += (data[1][i-1] > 0)?data[1][i-1]:0;
tempF += (data[1][i] > 0)?data[1][i]:0;
tempF += (data[1][i+1] > 0)?data[1][i+1]:0;
buf[0][i] = nearbyint( tempF/5 );
}
else
buf[0][i] = data[0][i];
for ( i = 1; i < dimension-1; i++ )
if ( data[i][0] < 0 )
{
tempF = 0;
tempF += (data[i-1][0] > 0)?data[i-1][0]:0;
tempF += (data[i+1][0] > 0)?data[i+1][0]:0;
tempF += (data[i-1][1] > 0)?data[i-1][1]:0;
tempF += (data[i][1] > 0)?data[i][1]:0;
tempF += (data[i+1][1] > 0)?data[i+1][1]:0;
buf[i][0] = nearbyint( tempF/5 );
}
else
buf[i][0] = data[i][0];
for ( i = 1; i < dimension-1; i++ )
if( data[dimension-1][i] < 0 )
{
tempF = 0;
tempF += (data[dimension-1][i-1] > 0)?data[dimension-1][i-1]:0;
tempF += (data[dimension-1][i+1] > 0)?data[dimension-1][i+1]:0;
tempF += (data[dimension-2][i-1] > 0)?data[dimension-2][i-1]:0;
tempF += (data[dimension-2][i] > 0)?data[dimension-2][i]:0;
tempF += (data[dimension-2][i+1] > 0)?data[dimension-2][i+1]:0;
buf[dimension-1][i] = nearbyint( tempF/5 );
}
else
buf[dimension-1][i] = data[dimension-1][i];
for( i = 1; i < dimension -1; i++ )
if( data[i][dimension-1] < 0 )
{
tempF = 0;
tempF += (data[i-1][dimension-1] > 0)?data[i-1][dimension-1]:0;
tempF += (data[i+1][dimension-1] > 0)?data[i+1][dimension-1]:0;
tempF += (data[i-1][dimension-2] > 0)?data[i-1][dimension-2]:0;
tempF += (data[i][dimension-2] > 0)?data[i][dimension-2]:0;
tempF += (data[i+1][dimension-2] > 0)?data[i+1][dimension-2]:0;
buf[i][dimension-1] = nearbyint( tempF/5 );
}
else
buf[i][dimension-1] = data[i][dimension-1];
// correct the rest
for ( i = 1; i < dimension -1; i++ )
for ( j = 1; j < dimension -1; j++ )
if( data[i][j] < 0 )
{
tempF = 0;
tempF += (data[i-1][j-1] > 0)?data[i-1][j-1]:0;
tempF += (data[i-1][j] > 0)?data[i-1][j]:0;
tempF += (data[i-1][j+1] > 0)?data[i-1][j+1]:0;
tempF += (data[i] [j-1] > 0)?data[i][j-1]:0;
tempF += (data[i] [j+1] > 0)?data[i][j+1]:0;
tempF += (data[i+1][j-1] > 0)?data[i+1][j-1]:0;
tempF += (data[i+1][j] > 0)?data[i+1][j]:0;
tempF += (data[i+1][j+1] > 0)?data[i+1][j+1]:0;
buf[i][j] = nearbyint( tempF / 8 );
}
else
buf[i][j] = data[i][j];
for ( i = 0 ; i < dimension; i++ )
{
for ( j = 0; j < dimension; j++ )
{
data[i][j] = buf[i][j];
}
}
}
int main()
{
int data[MAX][MAX];
int dimension;
scanf( "%d", &dimension );
get_data( data, dimension );
filter_errors( data, dimension );
show_result( data, dimension );
return 0;
}
void get_data( int data[MAX][MAX], int dimension )
{
int i, j;
for ( i = 0; i < dimension; i++ )
for ( j = 0; j < dimension; j++ )
scanf( "%d", &data[i][j] );
}
void show_result( int data[MAX][MAX], int dimension)
{
int i, j;
int marked[MAX][MAX]; // to mark if the spot was printed before
printf( "Project #6; name " );
printf( "Grid size: %dx%d ", dimension, dimension );
printf( "Bright spots: " );
// unmark every spot
for ( i = 0; i < dimension;i++ )
for ( j = 0; j < dimension; j++ )
marked[i][j] = 0;
for ( i = 0; i < dimension-4; i++ )
for ( j = 0; j < dimension-4;j++ )
find_and_print_brightest( data, i, j, marked );
// if there is no marked spot. then print (none)
for ( i = 0; i < dimension;i ++ )
for ( j = 0; j < dimension; j++ )
if( marked[i][j] != 0 )
return;
printf( "(none) " );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.