So here is my assignment:
File name: project1.cpp
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.

Example output:

| |
| * |
| |
| * * |
| * * |
| |

| |

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.

The corresponding file contains the data:
1 0 1 0 1 2 0 2 15 2 2 1 0 18 0 6 0 1 16 3

2 13 2 1 0 2 1 1 0 1 0 17 1 0 1 1 2 2 0 1

2 1 0 0 0 7 3 3 1 0 1 2 2 8 2 0 14 1 2 2

0 1 2 3 20 1 13 14 2 0 9 3 0 1 2 1 2 0 1 0

2 1 0 5 17 2 0 1 12 4 0 1 2 2 1 0 5 4 2 14

1 0 1 2 9 2 1 0 18 1 2 0 2 19 0 1 0 7 0 1

2 2 0 4 19 0 1 2 15 3 2 1 1 0 1 3 1 18 2 2

0 1 2 5 8 6 16 12 0 1 0 1 0 8 0 1 0 1 1 0

15 0 1 7 11 3 9 1 0 2 1 2 3 1 4 0 13 0 1 13

2 1 0 4 7 0 1 2 0 1 3 13 10 13 18 2 1 2 2 3

1 2 6 1 10 1 0 0 1 2 9 3 1 2 2 7 3 1 0 12

0 2 5 6 13 0 7 2 1 2 15 4 0 0 1 17 0 0 1 2

2 1 0 4 17 1 0 1 0 8 19 1 0 1 0 1 2 13 2 10

1 0 13 0 3 2 2 0 0 0 12 2 1 0 1 1 0 0 1 11

2 17 1 0 1 0 1 1 0 7 13 0 0 1 0 15 2 1 2 0

1 0 1 2 2 18 0 2 3 1 14 14 0 2 0 11 3 0 2 1

0 2 20 0 6 0 0 4 7 0 2 1 14 11 16 2 0 16 0 1

18 1 2 1 16 1 0 12 0 6 1 0 12 1 2 0 1 2 1 2

3 0 1 0 3 2 1 2 1 19 0 1 2 0 1 2 0 1 0 9

6 1 0 19 1 0 1 0 2 2 1 0 6 9 0 1 1 0 11 9

and here is my code:

#include<iostream> 
#include<fstream>
#include<iomanip>

using namespace std; 

int main() 
{
const int NUM_STARS = 400; 
int i, j; 
float stars[20][20]; 
ifstream inputFile; 
ofstream outputFile; 
inputFile.open("project1.txt"); 
outputFile.open("picture.txt"); 
for(i=0;i<20;i++); 
    {
    for(j=0;j<20;j++);
    }
stars[i][j+1] + stars[i+1][j] + stars[i-1][j] + stars[i][j-1]; 

inputFile>> stars[i][j]; 
inputFile.close(); 
outputFile<<"*"; 
outputFile.close(); 
cout<< "(Array (i,j) + sum of the surrounding intensities) / 5 > 6.0" << endl; 
return 0;
}

I plan on changing this to use a function instead of running it in main, but want to get it ironed out first.
I am using CentOS on virtualbox to code and compile this. It compiles fine, but when I run it I get " segmentation fault"
I can't tell where it is accessing illegal memory locations, and CentOS can't debug (at least with my level of knowledge)
could someome debug it and give me some direction? I was about to type "could someone give me some pointers?" but was worried it would be an unintentional pun to my segmentation faul

Edited 1 Year Ago by TObannion: wrong data

ARGH!
Okay so I just realized that I initialized the arrays with the variables, rather than the sizes. The printout to the picture.txt file only shows one star though. I am going to append my code and see if anybody can help me. Thanks so much!

stars[i][j+1] + stars[i+1][j] - BOOM! The largest index in a size 20 array is 19. What happens when you access element 19+1 in either of these two sub-expressions? It is spelled "Buffer Overflow"...

I understand that, since the first index position is 0. I will look into it more since the solution still isn't apparent to me, and I'll bring back what I figure out.
Thanks.

Here is what I have now...it works at least. Any pointers on cleaning it all up would be great.

#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;



int main()
{

const int ARRAY_WIDTH = 20;
const int ARRAY_HEIGHT = 20;

int stars[ARRAY_WIDTH][ARRAY_HEIGHT];   

int i;
int j;



ifstream inputFile;
ofstream outputFile;

inputFile.open("project1.txt", ios::in);
outputFile.open("picture.txt", ios::out);


for(i=1;i<20;i++)
  { cout << endl; 
    for(j=1;j<20;j++)
    {
        stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + (stars[i][j-1])/5 > 6.0;
         if ( (stars[i][j]/5) > 6.0)
                    outputFile << " *";
                    else 
                    outputFile << "  ";
    }
  }
inputFile >> stars[i][j];

inputFile.close();
outputFile <<"*";

outputFile.close();

return 0;   
}

Edited 1 Year Ago by TObannion

This article has been dead for over six months. Start a new discussion instead.