What does this error mean?

invalid operands of types float' and float' to binary operator+'

I can't use the + operator when adding array elements?

This is my code lines, I'm just trying to add the four surrounding values in a  array of numbers to the element that's in the middle of the four.

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

}}

Thanks.

## All 6 Replies

I guess you defined
float stars?
You can't write stars + stars[j], because you have to write two dimensions (two brackets):
stars[j]... always.
hth

Thanks, I'm trying this code now.

stars[i][j] = stars[i][j] + stars[i+1][j+i] + stars[i-1][j+i];
stars[i][j] = stars[i][j] + stars[i+j][j+1] + stars[i+j][j-1];
cout << stars[i][j]<< " ";

Also, for 2d arrays and inside math statements, does this look right since I'm doing both at once?

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

Yes, it's ok.
But it's better to write:

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

That way you are certain that it will work.

Thanks everyone!

By trial and error, I discovered it's much better to have a separate array to store the actual values instead of reassigning it back to stars. Here is my updated code that works...

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 << "  ";
}
}`

i am have the same project and i am confuse on what to do, i get the basic of funtions and arrays but applying it to this assignment throws me off, any help?

here is the assignment;

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.

commented: start a new thread, don't revive a 3 year old thread! -3

dyboss, thanks for the assignment text, what code have you written so far?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.