Hi everyone, first post here but I've been lurking for a lot longer I just can't find an answer through searching for this. I've got to sum the values of the array in two for loops but i can't get it to work, and I don't know where to start really, I'm getting errors like "'+=' : illegal, right operand has type 'float [1000]'" and "'+=' : 'int' differs in levels of indirection from 'float [1000]'". Any help would be much appreciated!
// The size of the array
#define WIDTH 1000
#define HEIGHT 2000
float array[HEIGHT][WIDTH];
int main(int argc, char *argv[])
{
// Fill the array with data
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
array[y][x] = float(x + y);
}
}
int sum=0;
for (int i=0;i<HEIGHT;i++)
{
sum+=array[i];
}
cout << sum << endl;
return 0;
}
(By the way, why even use float in the first place? You're only dealing with whole numbers, and if you ened decimal places you should sue doubles - never floats.)
Was given to me be a lecturer as I missed the class never got round to asking him if I could change stuff like that so attempted to make it work as it is. I've actually tried that before and I'm getting "+=' : illegal, right operand has type 'float [1000]'"
int main(int argc, char *argv[])
{
// Fill the array with data
for (int y = 0; y < HEIGHT; ++y)
{
for (int x = 0; x < WIDTH; ++x)
{
array[y][x] = float(x + y);
}
}
float sum = 0;
for (int y = 0; y < HEIGHT; ++y)
{
for (int x = 0; x < WIDTH; ++x)
{
sum += array[y][x];
}
cout << "Sum: " << sum;
}
return 0;
}
So this is what I've got although its definately not the numbers I was expecting. Any clues where to go from here? Should I do the rows and columns seperate?