Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help writing this program in C++. Thanks in advance. You work for the Jet P

ID: 668743 • Letter: N

Question

need help writing this program in C++. Thanks in advance.

You work for the Jet Propulsion Laboratory. They want you to write a program that will take a two-dimensional array containing the digitized representation of a picture of the night sky and locate the stars on it. The array elements are integers and will be read in from a text file. Each element of the array represents the amount of light hitting that portion of the image when the picture was taken. Intensities can range from 0 (no light) to 20 (maximum light).


A star is probably located in the area covered by the array element i,j if the following is the case:


(Array(i,j) + sum of the 4 surrounding intensities) / 5.0 > 6.0


Ignore possible stars along the edges of the matrix.


The desired output is a star map containing asterisks where you have found a star and blanks elsewhere.   


Note: Output should be sent only to the file “picture.txt”. No output should appear on the screen.

INPUT FILE: (project1.txt): An external text file containing 20 lines of data. Each line contains 20 integers. Therefore, your array(s) should be 20 x 20 in size.


OUTPUT FILE: (picture.txt): A star map (with a border). Print two blanks to indicate "no star" and one blank and an asterisk to indicate the presence of a star.

Explanation / Answer

You are simply supposed to read 20 x 20 integers from the file, project1.txt and calculate the star possibility for each array position.

A bit of complication here is, as the formula is going to consider the surronding values(left side, right side, upper side, lower side), you need to take care of extreme boundary values. If not you will go out of array boundary. Therefore the code goes like this:

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
FILE *ip,*op;
int sky[20][20],isstar,i,j;
ip = fopen("project1.txt","r");
op = fopen("picture.txt" ,"w");
//while(!feof(ip))
for(i=0;i<20;i++)
for(j=0;j<20;j++)
fscanf(ip,"%i",&sky[i][j]);
fprintf(op," ");
for(i=0;i<20;i++)
fprintf(op,"--");
fprintf(op," ");
for(i=0;i<20;i++)
{
fprintf(op,"|");
for(j=0;j<20;j++)
{
if(i == 0 && j == 0)   //No leftside and upperside value.
isstar = sky[i][j] + 0 + 0 + sky[i][j+1] + sky[i+1][j];
else if(i == 0 && j != 0) //No leftside value.
isstar = sky[i][j] + 0 + sky[i][j-1] + sky[i][j+1] + sky[i+1][j];
else if(i != 0 && j == 0) //No upperside value.
isstar = sky[i][j] + sky[i-1][j] + 0 + sky[i][j+1] + sky[i+1][j];
else if(i == 19 && j == 19) //No rightside and lowerside value.
isstar = sky[i][j] + sky[i-1][j] + sky[i][j-1] + 0 + 0 ;
else if(i == 19 && j != 19) //No lowerside value.
isstar = sky[i][j] + sky[i-1][j] + sky[i][j-1] + sky[i][j+1] + 0 ;
else if(i != 19 && j == 19) //No rightside value.
isstar = sky[i][j] + sky[i-1][j] + sky[i][j-1] + 0 + sky[i+1][j];
else           //You can consider all 4 surronding values.
isstar = sky[i][j] + sky[i-1][j] + sky[i][j-1] + sky[i][j+1] + sky[i+1][j];
if(isstar /5.0 > 6.0)
fprintf(op," *");
else
fprintf(op," ");   
}
fprintf(op,"| ");
}
fprintf(op," ");
for(i=0;i<20;i++)
fprintf(op,"--");
}