learning function and array.

i understand the basic of function and array but applying it to this assignment made me confused. Please some one help, the following below explains the assignment and what i came up with.


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.
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).

Example input:
0 3 4 0 0 0 6 8
5 13 6 0 0 0 2 3
2 6 2 7 3 0 10 0
0 0 4 15 4 1 6 0
0 0 7 12 6 9 10 4
5 0 6 10 6 4 8 0

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 > 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 to the file picture.txt.

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.

EXTRA CREDIT(2 pts): Turn the star map 1/4 turn (clockwise) and output
it again.

EXTRA, EXTRA CREDIT(2 pts): Create a mirror image of the star map and
output that.


THIS IS WHAT I AM STARTING WITH;



//Project 1
//Cindy Labossiere
//August 26, 2010
//Professor Levine
//Jet Propulsion Laboratory, take a two-dimensional array containing the digitized
//representation of a picture of the night sky and locate the stars on it


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

using namespace std;



int main()
{

//const int NUM_STARS = 20;
int i, j;

float stars[i][j];

ifstream inputFile;
//ofstream outputFile;

inputFile.open("project1.txt");
//outputFile.open("picture.txt");

for(i=0;i<6;i++)
{
for(j=0;j<8;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;
}




THANK YOU ANYONE!!!!

Last edited on
The code to read the file to a 2d array would be
1
2
3
4
5
6
7
for(i=0;i<6;i++)
{ 
   for(j=0;j<8;j++)
   {
      inputFile >> stars[i][j];
   }
}
I'm not sure what to explain here. If you don't understand it, you may be lacking some basics.

Now, about the algorithm itself. First you need a 6x8 array of chars filled with ' 's. Then, since you can ignore sides, iterate i = [1; 5) and j = [1; 7). Then if the sum of neighbors is under 30, put a '*' in the output array. The sum in your code is not right, you forgot the middle element.
would something like below be in the right path? i get the concept of arrays and functions but to apply it to this assignment seems to be confusing me because the examples in the book are not as similar. thank you kindly for assisting.

int main()
{

char stars= 20;
float sum[i][j]

ifstream inputFile;
ofstream outputFile;

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

for(i=0;i<6;i++)
{
for(j=0;j<8;j++)
{
inputFile >> stars[i][j];
}
}

for(i=1;i<19;i++)
{ cout << endl;
for(j=1;j<19;j++)
{
sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];

if ( (sum[i][j]/5) > 6.0)
cout << " *";
else
cout << " ";
}
}
inputFile >> stars[i][j];

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

outputFile.close();


//cout << (Array (i,j) + sum of the surrounding intensities) / 5 > 6.0 << endl;



return 0;
}
First issue I see; you are creating your array like this:

float sum[i][j]

You've not actually created variables named i and j, and in your first set of code way up at the top, you created them but then used them without actually setting them to any value, so they could be any value at all.


That aside, C++ does not allow you to create variable sized arrays. If your compiler lets you, it's non-standard.

How about something like this:

1
2
3
4
const int ARRAY_WIDTH = 20;
const int ARRAY_HEIGHT = 20;

int stars[ARRAY_WIDTH][ARRAY_HEIGHT];
thanks a ton, i am using xcode as my complier and it does say "j" and "i" are not declared in the scope and sum not declared in the scope but i guess i thought i was by stating float sum[i][j]. i tried this below and still have the same error.




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


using namespace std;



int main()
{
char sum [] = { i, j };
const int ARRAY_WIDTH = 20;
const int ARRAY_HEIGHT = 20;

int stars[ARRAY_WIDTH][ARRAY_HEIGHT];


float sum[i][j]

ifstream inputFile;
ofstream outputFile;

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

for(i=0;i<6;i++)
{
for(j=0;j<8;j++)
{
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];
}
}

for(i=1;i<19;i++)
{ cout << endl;
for(j=1;j<19;j++)
{
sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];

if ( (sum[i][j]/5) > 6.0)
cout << " *";
else
cout << " ";
}
}
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];

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

outputFile.close();


//cout << (Array (i,j) + sum of the surrounding intensities) / 5 > 6.0 << endl;



return 0;
}
char sum [] = { i, j };

This says: make me an array of two char objects, in which the first one is the variable i, and the second is the variable j.

At this point in your code, what is the variable i? What is the variable j? Are they int type? Are they float? They're nothing - they don't exist because you have not created them.

If you wanted them to be int, you could create them like this:

1
2
int i;
int j;


If you wanted them to be char, you could create them like this:

1
2
char i;
char j;


If you wanted them to be float, you could create them like this:

1
2
float i;
float j;

And so on. Decide what you want them to be, and then create them before you try to use them.

I'm guessing you're trying to create an array of integers (not characters - why would you want an array of characters for this problem?) named sum, which is two-dimensional, and you want it to be ARRAY_WIDTH wide and ARRAY_HEIGHT high. You can do this as follows:

char sum[ARRAY_WIDTH][ARRAY_HEIGHT];
Last edited on
it looks like it's coming along, excited. i have 8 error for this line of code listed below, all stating the same thing, error: invalid types 'int [20][20][float]' for array subscript. i tried diffrent scenarios and this one has the least amount of errors. below is the line of code with error;


{
sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];

if ( (sum[i][j]/5) > 6.0)

overall below is the revised code from all the help i received, thank you!!


//Project 1
//Cindy Labossiere
//August 26, 2010
//Professor Levine
//Jet Propulsion Laboratory, take a two-dimensional array containing the digitized
//representation of a picture of the night sky and locate the stars on it


#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 sum[ARRAY_WIDTH][ARRAY_HEIGHT];
float i;
float j;

ifstream inputFile;
ofstream outputFile;

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

for(i=0;i<6;i++)
{
for(j=0;j<8;j++)
{
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];
}
}

for(i=1;i<19;i++)
{ cout << endl;
for(j=1;j<19;j++)
{
sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];

if ( (sum[i][j]/5) > 6.0)
cout << " *";
else
cout << " ";
}
}
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];

inputFile.close();
outputFile >> sum[i][j];

outputFile.close();


//cout << (Array (i,j) + sum of the four surrounding intensities) / 5 > 6.0 << endl;



return 0;
}
i made some more revisions and now i am down to 3 errors that i seem to be blind to the results.

on these line of codes below the following errors appears;


sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];
-error:invalid types 'float[in]' for array subscript

if ( (sum[i][j]/5) > 6.0)

-error:invalid types 'float[in]' for array subscript

outputFile >> *>>"*"; -error: expected primary-expression before '>>' token. (with this code i need to put an 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. )

below is my revised code.






//Project 1
//Cindy Labossiere
//August 26, 2010
//Professor Levine
//Jet Propulsion Laboratory, take a two-dimensional array containing the digitized
//representation of a picture of the night sky and locate the stars on it


#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;
float sum;

ifstream inputFile;
ofstream outputFile;

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

for(i=0;i<6;i++)
{
for(j=0;j<8;j++)
{
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];
}
}

for(i=1;i<19;i++)
{ cout << endl;
for(j=1;j<19;j++)
{
sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];

if ( (sum[i][j]/5) > 6.0)
cout << " *";
else
cout << " ";
}
}
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];

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

outputFile.close();


//cout << (Array (i,j) + sum of the four surrounding intensities) / 5 > 6.0 << endl;



return 0;
}
i found the two errors, i am getting better as a beginner. i am still blind sided by the following lited below.

outputFile >> *>>"*"; -error: expected primary-expression before '>>' token. (with this code i need to put an 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. )

below is my revised code.


//Project 1
//Cindy Labossiere
//August 26, 2010
//Professor Levine
//Jet Propulsion Laboratory, take a two-dimensional array containing the digitized
//representation of a picture of the night sky and locate the stars on it


#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;
float sum[i][j];

ifstream inputFile;
ofstream outputFile;

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

for(i=0;i<6;i++)
{
for(j=0;j<8;j++)
{
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];
}
}

for(i=1;i<19;i++)
{ cout << endl;
for(j=1;j<19;j++)
{
sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];

if ( (sum[i][j]/5) > 6.0)
cout << " *";
else
cout << " ";
}
}
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];

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

outputFile.close();


//cout << (Array (i,j) + sum of the four surrounding intensities) / 5 > 6.0 << endl;



return 0;
}
closed account (D80DSL3A)
You still have a number of problems.
1) Where you declare the sum array:
1
2
3
int i;
int j;
float sum[i][j];

i and j have not been assigned values. They should also be constants, not variables.
I think you want a 20x20 array for sum so float sum[ARRAY_WIDTH][ARRAY_HEIGHT]; would be better. Actually, you don't need this array at all.
Instead of:
1
2
sum[i][j] = stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1];
if ( (sum[i][j]/5) > 6.0)

Why not just:
 
if ( (stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1])/5 > 6.0)


2) The stream operators for file I/O point in the direction that the data is flowing.
When reading from a file the data flows from the file to the place you are going to store it:
inputFile >> stars[i][j];
When writing to the file the data flows from the array to the file:
 
outputFile << " *";// if a star is detected 


3) Why are you reading from the file to 6x8?
1
2
3
4
5
6
7
for(i=0;i<6;i++)// why 6?
{
for(j=0;j<8;j++)// why 8?
{
inputFile >> stars[ARRAY_WIDTH][ARRAY_HEIGHT];// should be stars[i][j]
}
}

Your data in the file is 20x20, not 6x8

4) Where you use cout that will send output to the console, not to outputFile. There's no reason to use cout in your program.

I hope this straightens out some of the issues.
No more errors, this help a ton but when it came time to run it nothing showed, i don't know if it's because i am using xcode on mac but i guess the best way to try it is to use the program at school which is centos. overall the program below gives no errors but won't compile. please take a look,


#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<19;i++)
{ cout << endl;
for(j=1;j<19;j++)
{
if ( (stars[i][j] + stars[i+1][j] + stars[i-1][j] + stars[i][j+1] + stars[i][j-1])/5 > 6.0);
}
}
inputFile >> stars[i][j];

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

outputFile.close();


//cout << (Array (i,j) + sum of the four surrounding intensities) / 5 > 6.0 << endl;



return 0;
}
closed account (D80DSL3A)
Your programs logic is still way off. Look carefully at the code and see if you can tell what it will do.
Things are being done out of order. You need those nested for loops in 2 places. First to read the data from the file into your stars array, then to do the part where you add the intensities to see if a star is there. These loops will have different limits for the values of i and j. Can you see why?

You are probably getting just 1 * in your outputFile. Try to improve the logic so it's closer to working right.
Last edited on
i have some problem about array..i need to built a program about wedding boutique and the program will use array. i want to use array for customer name and the package that the customer have chose.so i think i need to use two dimension array but i don't know the coding..can you help me with example of coding in two dimension array with name and package chose.
Topic archived. No new replies allowed.