ExOfDe ExOfDe - 1 month ago 18
C Question

quadratic Sum of N nummbers: implementation in C

As you can read in the header it is about a rather simple topic. But I encountered something very odd while implementing possible different ways to get a quadratic sum of N numbers.

I implemented three different versions:


  • Quadratic Gauß sum (func: quadratGauss(int n) )

  • Iterative (func: quadratIterativeClassic(int n) )

  • Iterative via Bitoperation (func: quadratIterativeBitoperation(int n) )



All three functions have returned the same result as expected from the given Input. The Big question now is. How was it possible for the third function to work proper? The Code looks like this:

void quadratGauss(int n){

printf("+quadratGauss: \n");
int result = ((n*(n+1))*(2*n+1))/2;
printf("Result: %d\n----------------\n",result);

}

void quadratIterativClassic(int n){

int result;
printf("+quadratIterativClassic: \n");
for(int i;i<=n;i++)
result += i * i;
printf("Result: %d\n----------------\n",result);

}

void quadratIterativeBitOperation(int n){

int result;
printf("+quadratIterativeBitOperation: \n");

for(int i;i<=n;i++)
result += i^2;

printf("Result: %d\n----------------\n",result);

}


As you see I use the XOR operator. I originally wanted to use the shift-operator "<<" but i tried others as well. what where stunning when i tried the XOR operator: the result was still the same as before and the same as the other two functions. I thought this was not possible because the XOR is not an operator for the power-operation because C does not contain such an operator but still i got eventually the right result.

I tested the functions with different numbers of different "sizes". Nothing odd in the result of each function.

I've got no clue why. But there must be an explanation, doesn't it?
If you have one, I would appreciate alot the effort of writing down a possible reason, why you can implement this with the XOR operator.

here the whole main file:

#include <stdlib.h>
#include <stdio.h>

#include <iostream>

#define N 5000



void sumGauss(int n)
{
printf("+sumGauss: \n");
int result = (n*(n+1)/2);
printf("Result: %d\n----------------\n",result);
}

void sumClassic(int n)
{
printf("+sumClassic: \n");
int result = 0;
for(int i = 0; i <= n; i++)
result += i;
printf("Result: %d\n----------------\n",result);
}

void quadratGauss(int n){

printf("+quadratGauss: \n");
int result = ((n*(n+1))*(2*n+1))/2;
printf("Result: %d\n----------------\n",result);

}

void quadratIterativeBitOperation(int n){

int result;
printf("+quadratIterativeBitOperation: \n");
for(int i;i<=n;i++)
result += i^2;
printf("Result: %d\n----------------\n",result);

}

void quadratIterativClassic(int n){

int result;
printf("+quadratIterativClassic: \n");
for(int i;i<=n;i++)
result += i * i;
printf("Result: %d\n----------------\n",result);

}

void unevenSum(int n)
{
printf("+unevenSum: \n");
int uneven = 0;
int i=0;
for(i=1;i<= n;i+=2) {
uneven += i;
}

printf("Result: %d\n----------------\n",uneven*2);


}

void unevenSumModulo(int n)
{
printf("+unevenSumModulo: \n");
int result=0;
for(int i=0;i<=n;i++)
{
if( (i%1) == 0)
result += i;
}

printf("Result: %d\n----------------\n",result);


}

void unevenSumNico(int n)
{
printf("+unevenSumNico: \n");
int odd = 0;
int i=0;
for(i=1;i<= n;i++) {
odd += ((2*i)-1)/2;
}

printf("Result: %d\n----------------\n",odd);


}

void evenSum(int n)
{
printf("+evenSum: \n");
int result=0;
for(int i=0;i<=n;i++)
{
if( (i%2) == 0)
result += i;
}

printf("Result: %d\n----------------\n",result);


}

void Zins(float Kapital,int years)
{
printf("Zinstabelle fuer Grundkaptial %.2f Euro\n",Kapital);
printf("Kapitalstand zum Jahresende\n");
int i=0;
float K = Kapital;
for(int i =1;i<years+1;i++)
{

K = K *(1.f + 0.05f);
printf("Jahr: %2d Kapital: %.2f Euro\n",i,K);

}
}

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

sumGauss(N);
sumClassic(N);

quadratGauss(N);
quadratIterativeBitOperation(N);
quadratIterativClassic(N);

unevenSum(N);
unevenSumNico(N);
unevenSumModulo(N);

evenSum(N);
Zins(N);


return 0;
}

Answer

This would only be true for n = 0. Rest assured that you cannot implement squaring using just the XOR operator, even for integral types.

The behaviour of your code is currently undefined, as you don't initialise either i, or result. I suspect that your loop does not run at all and by the most amazing coincidence, result occupies the same memory in the subsequent functions as it does in the Guassian function. But don't ever rely on that behaviour. Other compilers might simply attempt to eat your cat.

Comments