Array Sum

E

ElissaT

Hi all,
I'm trying to build the following sums with an Array A[640][480]:

j=rows i=cols
Sum Sum ((A[j]-A[i+1][j])*(A[j]-A[i+1][j]))+((A[j]-A[j
+1])*(A[j]-A[j+1]))
j=0 i=0
where the first sum runs from j=0 to j=number of rows and the second
sum from i=0 to i= number of columns.
As a test I first build the sum with a smaller array A[3][4] this way:

#include <iostream>
#include <ios>
#include <cmath>

using namespace std;

int main()
{
double Sum;
double A[3][4]={{ 1, -2, 3, 4},{ 5, -6, 7, -8},{ 9, -10, 11, 12}};
for (int j=0; j<3; j++) {
Sum = 0.0;
for (int i=0; i<4; i++){
Sum += ((A[j] - A[i+1][j]) * (A[j] - A[i+1][j]))+((A[j] - A
[j+1]) *(A[j] - A[j+1]));
}
}
cout<< scientific<< Sum <<endl;
return 0;
}

I don't understand why the result displayed by the console is :
1.#QNAN
Is something wrong with the code?

Thank you

Lisa
 
P

Pascal J. Bourguignon

Hi all,
I'm trying to build the following sums with an Array A[640][480]:

j=rows i=cols
Sum Sum ((A[j]-A[i+1][j])*(A[j]-A[i+1][j]))+((A[j]-A[j
+1])*(A[j]-A[j+1]))
j=0 i=0
where the first sum runs from j=0 to j=number of rows and the second
sum from i=0 to i= number of columns.
As a test I first build the sum with a smaller array A[3][4] this way:

#include <iostream>
#include <ios>
#include <cmath>

using namespace std;

int main()
{
double Sum;
double A[3][4]={{ 1, -2, 3, 4},{ 5, -6, 7, -8},{ 9, -10, 11, 12}};
for (int j=0; j<3; j++) {
Sum = 0.0;
for (int i=0; i<4; i++){
Sum += ((A[j] - A[i+1][j]) * (A[j] - A[i+1][j]))+((A[j] - A
[j+1]) *(A[j] - A[j+1]));
}
}
cout<< scientific<< Sum <<endl;
return 0;
}

I don't understand why the result displayed by the console is :
1.#QNAN
Is something wrong with the code?


Yes. You are indexing elements out of bounds.

You write first: A[3][4]
and then: i<4 A[j]
and worse: a[i+1][j]

Valid indices in a C array A[N] go from 0 to N-1, not N, and much less N+1.

Therefore you are adding uninitialized data beyond the array A, and
it's no surprise if you get random results such a NAN.


If you have indices in the form i+k, then your loop should be (i=0;i<max-k;i++).

If you have several indices, you must match the bounds with the rank
of the indices (it may help using memonics instead of random variables
such as i and j.


------------------------------------------------------------------------
#include <iostream>
#include <ios>
#include <cmath>

using namespace std;

int main()
{
const int rows=3;
const int columns=4;
double A[rows][columns]={{ 1, -2, 3, 4},{ 5, -6, 7, -8},{ 9, -10, 11, 12}};
double Sum=0.0;
for (int c=0; c<(columns-1); c++) {
for (int r=0; r<(rows-1); r++){
Sum += ((A[r][c] - A[r+1][c]) * (A[r][c] - A[r+1][c]))+((A[r][c] - A[r][c+1]) *(A[r][c] - A[r][c+1]));
}
}
cout<< scientific<< Sum <<endl;
return 0;
}

/*
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Wed Mar 18 12:12:15

SRC="/tmp/ss.c++" ; EXE="ss" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
6.460000e+02
status = 0

Compilation finished at Wed Mar 18 12:12:15
*/
 
L

Lionel B

Hi all,
I'm trying to build the following sums with an Array A[640][480]:

j=rows i=cols
Sum Sum ((A[j]-A[i+1][j])*(A[j]-A[i+1][j]))+((A[j]-A[j
+1])*(A[j]-A[j+1]))
j=0 i=0
where the first sum runs from j=0 to j=number of rows


No it doesn't! It runs from 0 to rows-2
and the second sum from i=0 to i= number of columns.

No it doesn't! It runs from 0 to cols-2

In C++ if you declare an array A[n] of size n, then the index runs from 0
to n-1. Now in your case you need to access elements with indices i+1 and
j+1; so these must be < the corresponding array size. So e.g. you need
j+1 < rows, hence j <= rows-2.
As a test I first build the sum with a
smaller array A[3][4] this way:

#include <iostream>
#include <ios>
#include <cmath>

using namespace std;

int main()
{
double Sum;
double A[3][4]={{ 1, -2, 3, 4},{ 5, -6, 7, -8},{ 9, -10, 11, 12}};

Part of the problem is awful coding style: at least give your array sizes
meaningful names:

const int rows = 4;
const int cols = 3;
double A[cols][rows];

[As an aside, it is probably more conventional to address arrays as
A[rows][cols] perhaps because this corresponds with the convention for
mathematical matrix notation. It is just a convention, though.]
for (int j=0; j<3; j++) {

Note that j indexes the second array dimension, which is 4. This is
actually correct, but I suspect "by accident"; better to write:

for (int j=0; j<rows-1; j++) {
Sum = 0.0;
for (int i=0; i<4; i++){

Whoa! i indexes the first array dimension, which is 3; you actually need
i < 2, since you access elemnts A[i+1][...] (see comments above). Better
to write:

for (int i=0; i<cols-1; i++){
Sum += ((A[j] - A[i+1][j]) * (A[j] - A[i+1][j]))+((A[j] -
A[j+1]) *(A[j] - A[j+1]));
}
}
cout<< scientific<< Sum <<endl;
return 0;
}

I don't understand why the result displayed by the console is : 1.#QNAN
Is something wrong with the code?


You've got your array sizes in a twist. You're attempting to access array
elements beyond the array bounds. Partly, this is down to awful coding
style.
 
L

Lionel B

(e-mail address removed) writes:
[...]

int main()
{
const int rows=3;
const int columns=4;
double A[rows][columns]={{ 1, -2, 3, 4},{ 5, -6, 7, -8},{ 9, -10,
11, 12}};

Just to point out (see my reply) that the OP says explicitly:

and then indexes the array as A[j] - which implies that it should be:

const int rows = 4;
const int cols = 3;
double A[cols][rows]; // (!)

I comment in my reply that this is unconventional - and horrendously
confusing :eek:)

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top