

What messages or notifications are you responding to?






Your summation function makes no sense. You pass in a parameter which is ignored and then do some fairly random calculations, and return the value which will be zero, after a single iteration of the loop.





so as far as u say which way i should proceed?
actually my logic was not working so was planning to implement kahan summation.





maibam debina wrote: so as far as u say which way i should proceed? Well the first thing you need to do is to define what the summation function is supposed to do; I cannot make any sense out of it. Once you have done that, then define the steps required and convert them to code. It would probably help you immensely to use meaningful names for your variables rather than single letters which make it difficult to understand.





can u please help me out with code...:(





Well I could, but i) I have no idea what your summation function is supposed to do, and ii) you would learn more by trying it yourself.





Actually the logic which i use to implement for performing the summation was
<blockquote class="quote"><div class="op">Quote:</div>for(i=0;i<5;i++)
{ float f= ((float)rand()/(float)(RAND_MAX))*a;
y=square(f);
sum +=y;
}
printf("\n Summation Of Square Number is:=%f\t\n",sum); </blockquote>
but it doesnot seem well its not giving the exact summation value,all i want was to perform summation of the generated float random numbers .





Are you sure that sum contains zero at the beginning of the loop? In the statement:
float f= ((float)rand()/(float)(RAND_MAX))*a;
what is the value of a ?
And since you are not printing any of the intervening values, how can you be sure the final sum is not correct?






for(i=0;i<5;i++)
{
float f= ((float)rand()/(float)(RAND_MAX))*a;
printf("%f\t",f);
y=square(f);
printf("\t\t%f\n",y);
}
for(i=0;i<5;i++)
{ float f= ((float)rand()/(float)(RAND_MAX))*a;
y=square(f);
sum +=y;
}
printf("\n Summation Of Square Number is:=%f\t\n",sum);
i have print at the 1st for loop...:(





And in the second loop you will have a different set of numbers, as returned by the calls to rand() . I also cannot find a reference to square , is this another function in your code?





ya it is thank fren i got it...matter was with the printf statement i gav at second loop ,wen i pass the printf statement inside the 1st loop itself n removing the second for loop as condition where same ...den i got the exact answer..





What are you doing?
OK, you are trying to implement the "Kahan summation algorithm"[^].
However you have to read carefully and understand it, before actually coding.
For instance, as shown by the algorithm pseudocode[^], the parameter to the summation function must be an array, while you are using a float.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
 C++ FQA Lite
modified yesterday.





i tried using array but seem it not working can u please help me out with code making modification...actual thing what i want is to do the summation of all the generated random number which is store at b.





#include <stdio.h>
#define N 5
double summation(double g[], int size);
int main()
{
double a[N] =
{
0.035679,
0.079935,
0.033320,
0.042424,
0.012387
};
double s[N];
int i;
for (i=0; i<N; ++i)
{
s[i] = a[i]*a[i];
}
printf("summation = %g\n", summation(s,N));
}
double summation(double g[], int size)
{
int i;
double sum =0.0;
double c=0.0;
for(i=0; i<size ;i++)
{
double y = g[i] c;
double t = sum + y;
c = (tsum)  y;
sum = t;
}
return sum;
}
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
 C++ FQA Lite





Consider the following implementation of a summation function and various ways of creating/populating an array for it to work on.
1. static data declared at compile time
2. dynamic data allocated with the c function malloc
3. dynamic data allocated with the c++ operator new
#include <cstdlib>
#include <cstdio>
float sumArrayElements(float array[], int numItems)
{
int i;
float result = 0;
for (i=0; i<numItems; i++)
result += array[i];
return result;
}
int main ()
{
float array1[5] = {10.2, 13.7, 14.8, 99.0, 0.0};
float arraySum1 = sumArrayElements( array1, 5);
printf("Array sum: %.2f\n",arraySum1);
int i;
float *array2;
int dynamicCount1 = 5;
array2 = (float*)malloc(dynamicCount1 * sizeof(float) );
for (i=0; i<dynamicCount1; i++)
array2[i] = (float)rand()/(float)(RAND_MAX);
float arraySum2 = sumArrayElements( array2, dynamicCount1);
free(array2);
printf("Array sum: %.2f\n",arraySum2);
int dynamicCount2 = 10;
float *array3 = new float[dynamicCount2];
for (i=0; i<dynamicCount2; i++)
array3[i] = (float)rand()/(float)(RAND_MAX);
for (i=0; i<dynamicCount1; i++)
array3[i] = (float)rand()/(float)(RAND_MAX);
float arraySum3 = sumArrayElements( array3, dynamicCount2);
delete array3;
printf("Array sum: %.2f\n",arraySum3);
return 0;
}





thanks for your contribution but this is the logic i apllied for summation 'result +=array[i]' but this is giving the approximate answer not the exact one.





Except for a rather small subset of numbers, calculations with computers involving floatingpoint numbers are approximate.
Integer arithmetic doesn't suffer from this pitfall.
However, if you are talking about the result of the code I posted  then consider reviewing the printf statements  in each of them I have used the %.2f format specifier to tell printf to only print 2 digits after the decimal place. The point is  the answer is as close to exact as floats will give you, it is only the display that is (grossly) approximate.
You can get a less approximate result printed if you change the print specifiers to %f





As far as I can understand, he is trying to imlpement the "Kahan summation algorithm"[^].
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
 C++ FQA Lite





CPallini i have given just for 5 number as demo but in real my count is above 60000 ?and i am searching for the summation for all the 60000+ value which give correct result.it wouldt be possible for what?





You cannot modify my program for processing more items, can you?
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
 C++ FQA Lite





thank you finally i got it after full torture to u and others too..





You are welcome.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
 C++ FQA Lite





Seems like a reasonable assumption to me. I'd seen your mention of it earlier, but hadn't seen a confirmation of this so guessed (wrongly?) where the problem lay.
Thanks for the impetus to reinvestigate the method  I seem to remember using it for something a while back, but brainrot defeats me..




