S
sln
Xah said:C:
#include <stdlib.h>
#include <math.h>
void normal(int dim, float* x, float* a) {
float sum = 0.0f;
int i;
float divisor;
for (i = 0; i < dim; ++i) sum += x * x;
divisor = sqrt(sum);
for (i = 0; i < dim; ++i) a = x/divisor;
}
Due to the low level of C, this C example should perhaps then accept a
sequence of numbers separated by space...
In other words, you want a REPL.
Why don't we have another challenge that involves handling a non-trivial
input format, i.e. parsing?
Maybe you could speed it up.
sln
----------------------------------
void normal (int Dimension, double *X, double *A)
{
double *xp, *ap, divisor, sum;
int i;
for ( i=0, sum=0., xp=X; i<Dimension; i++, xp++)
sum += (*xp) ** 2;
divisor = sqrt ( sum );
for ( i=0, ap=A, xp=X; i<Dimension; i++, ap++, xp++)
*ap = *xp / divisor;
// if Dimension is greater than
// sizeof double X[] or double A[] it will GPF.
// this is a really, really bad design.
}