# Array Sum

Discussion in 'C++' started by ElissaT@gmx.de, Mar 18, 2009.

1. ### Guest

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

, Mar 18, 2009

2. ### Tim LoveGuest

writes:

>I don't understand why the result displayed by the console is :
>1.#QNAN

With g++ (gcc version 4.3.1) I get

3.800000e+02

Tim Love, Mar 18, 2009

3. ### Pascal J. BourguignonGuest

writes:

> 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
*/
------------------------------------------------------------------------

Finally, since you're programming in C++, it might be worthwhile to
write a matrix class with bound checking to avoid such errors.

--
__Pascal Bourguignon__

Pascal J. Bourguignon, Mar 18, 2009
4. ### Lionel BGuest

On Wed, 18 Mar 2009 03:34:44 -0700, ElissaT wrote:

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

--
Lionel B

Lionel B, Mar 18, 2009
5. ### Lionel BGuest

On Wed, 18 Mar 2009 12:18:27 +0100, Pascal J. Bourguignon wrote:

> 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:

>> j=rows i=cols

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 )

[...]

--
Lionel B

Lionel B, Mar 18, 2009

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.