# Problem with summation algorithm

Hi there, I'm trying to right a program for class that 1st assigns random single precission floats from 0 to 1 to the elements 1-d array and then sums them up. Next I'm supposed to compare to this thing called the Kahan summation algorithm for different values of N (array size) using the fractional difference.
I'm just trying to get each of the sums to print out their values now to make sure they doing what they should be doing, not worrying about the fractional difference part yet but keep getting seg faults and I have no idea why.
What's weird is that I think I'm getting them in two different instances. One seg fault seems to come randomly...like I compile and execute the code for the same value of N a few times and every now and then it seg faults, but sometimes not.
The other seg fault seems to occur if N>18.
Last thing...something weird is happening with my sums also because sometimes for som given value like N=15 it gives me the correct summation values, and then every now and again it gives me something ridiculous like sum = 4113476354484640333955072.00000.

Here's my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <assert.h>

float brute(long N,float A[N])
{
long i;
float sum = 0;
for(i=0; i<N; i++)
sum+=A[i];
return sum;
}

float kahan(long N,float A[N])
{
float sum = A;
float  c = 0;
float y,t;
long i;
for(i=1; i<N; i++)
{
y = A[i] - c ;
t = sum + y;
c = (t - sum) - y;
sum = t;
}

return sum;
}

int main(int argc,char *argv[])
{

long i, N, iSeed;
float  A[N], bsum, ksum;
if (argc != 2)
{
fprintf(stderr,"Usage: %s N, where N is the size of the array.\n",argv);
return 1;
}
N = atoi(argv);
assert(N>1);
iSeed = (int) time(NULL) % getpid() + getppid();
printf("Seed = %i\n",iSeed);
srand48(iSeed);

for(i=0;i<N;i++)
{
A[i]=drand48();
printf("elems of A are %f\n",A[i]);
}
bsum = brute(N,A);
ksum = kahan(N,A);
printf("bsum is %f, ksum is %f\n",bsum,ksum);
return 0;

The print statements I just have in there to see what's going on for now.
Any insight at all would be greatly appreciated and would help me keep my sanity

## Answers and Replies

Hurkyl
Staff Emeritus
Science Advisor
Gold Member
Code:
int main(int argc, char *argv[])
{
long i, N, iSeed;
At this point in the program, N is uninitialized: it could be anything from -2147483648 to -2147483647 (on a 32-bit machine).

Code:
   float A[N], bsum, ksum;
This initializes the array A to have length N. And since N is still uninitialized...

Incidentally, if you enable warnings for your C compiler, it would probably have told you this. The option is -Wall if you're using gcc.

Oh, one more thing: const correctness is a good habit, and there's no time like the present to start learning! Both of your functions do not modify the data; so, for example, you should make the declaration

Code:
float kahan(long N, const float A[N])

Some benefits are:
(1) It lets the compiler catch some of your mistakes
(2) It prevents headaches when you try to interface with other const-correct code
(3) It can result in a faster program
(4) It's "self-documenting": if someone is reading your program, it instantly tells them that this function will not modify A.

Last edited:
Thanks much Hurkyl. I changed it so that I initialized N when I first declared it and it seems to work just fine now. Despite that I'm not sure why still. Is the problem just that I tried to use it in A[N] before I specified what N was? I guess that makes sense then.
Also I tried gcc -Wall with my old code and it didn't give me any warning about that, all it said was:
Code:
ps2_1.c: In function 'main':
ps2_1.c:45: warning: implicit declaration of function 'getpid'
ps2_1.c:45: warning: implicit declaration of function 'getppid'

As for your last comment about const float, I've never heard that before. So if I'm using a function that's not going to be altering the value of an argument in my function I use const before hand right? And if the function was going to alter the argument I would just leave it as float with nothing before it?

Well, it works now, and i think I understand what I did wrong Thanks again.

Last edited: