këö¬t
metolhead... Tuesday, 06. January 2009
spacer spacer
columnists
cake
COD Squad
elfling
keot
madman
memoi
metolhead
rach
seed

extended content
front page
#mtuk
residentbook

hyper text terminal protocol
contacting us
copyright
website information
n | b | s

Bollocks

I got bored so am pasting my current project in here, enjoy the spam:

/* main.c */
#include < stdio.h >
#include < stdlib.h >
#include "searchs.h"
#include "sorts.h"
#include "mathf.h"
#include "mem.h"

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

/*--------Define Test Case------*/
#define TEST_SQRT
#define TEST_SQRT_QUICK
#define TEST_SQRT_LONG
/*------------------------------*/

/*----------SEARCHS-------------*/
#ifdef TEST_BINSEARCH
long array[1000],i=0;
for(i; i < 1000; i++)
{
array[i] = i*2;
}

printf("\nValue: %d \n", ar_bin_search(array, 136, sizeof(array)/sizeof(long)));
#endif /* TEST_BINSEARCH */

/*------------SORTS-------------*/
#ifdef TEST_BUBBLESORT
double array[1000];
long i=0;
/* for(i; i < 1000; i++)*/
for(i=1000; i > 0; i--)
{
array[i] = i;
#ifdef TEST_RNG
rng();
#else /* use normal RNG */

#endif /* TEST_RNG */
}

printf("Size of array is: %d bytes\n", sizeof(array)/sizeof(double));
system("PAUSE");

bubble_sort(array, sizeof(array)/sizeof(double));

for(i=0; i < 1000; i++)
{
printf("\nNumber %d is %f", i, array[i]);
}
#endif /* TEST_BUBBLESORT */

/*------------MATH FUNCTIONS-------------*/
#ifdef TEST_SQRT
double value = -25;
complex root;
#ifdef TEST_SQRT_QUICK
root = sqrt_func(value);
printf("\nSqrt %f = %f + i*%f\n", value, root.real, root.imaginary);
#endif /* TEST_SQRT_QUICK */
#ifdef TEST_SQRT_LONG
root = sqrt_long(value);
printf("\nSqrt %f = %f + i*%f\n", value, root.real, root.imaginary);
#endif /* TEST_SQRT_LONG */
#endif /* TEST_SQRT */

#ifdef TEST_QUADRATIC_SOLVER
double a=1,b=0,c=+1;
quadratic_answer *ans = quadratic_solver(a,b,c);

if( ans- > one.complex == 0)
printf("One: %f\nTwo %f\n", ans- > one.real, ans- > two.real);
else
printf("One: %f + i*%f\nTwo %f + i*%f\n", ans- > one.real,
ans- > one.complex, ans- > two.real, ans- > two.complex);
#endif /* TEST_QUADRATIC_SOLVER

/*------------MEMORY FUNCTIONS-------------*/
#ifdef TEST_MEM
#ifdef TEST_MEM_ALLOC
double *array[25];
int i=0;
for(i=0; i< 25; i++)
array[i] = (double)xmalloc(sizeof(double));
for(i=25; i > 0; i--)
*array[25-i] = i;
for(i=0; i< 25; i++)
printf("\nArray[%d] is %f at %x",i,*array[i],array[i]);
#endif /* TEST_MEM_ALLOC */
#ifdef TEST_MEM_FREE

#endif /* TEST_MEM_FREE */
#endif /* TEST_MEM */

printf("\n");
system("PAUSE");
return 0;
}

/* error.h, contatins error codes to be used to general proggies */
#ifndef ERROR_H
#define ERROR_H
/*****************************************************************/

#ifndef NULL
#define NULL 0x00
#endif /* NULL */

#ifndef size_t
#define size_t unsigned int
#endif

#define ENOERR 0
#define ENOVAL 1
#define ENOMEM 2
#define RACHIE 12 /* Hehehehe :D */
#define EFATAL 99

/*****************************************************************/
#endif /* ERRORS_H */

/* Code outlines for various search routines given arrays and alike */
#ifndef SEARCHS_H
#define SEARCHS_H

/* General includes */
#include "error.h"

/* Array section */
long ar_bin_search(long array[], long val, unsigned long sz);

#endif /* SEARCH_S */

/* Searching funtions */
#include "searchs.h"
/*
long ar_bin_search(void *array, void val)
Binary search through an array. Data has to be in increasing order already.
Return index to value on success, on fail returns a neagtive error code
*/
long ar_bin_search(long array[], long val, unsigned long sz)
{
int i = sz/2;
float j = i;

while(1)
{
printf("%d | ",i);

if( array[i] == val )
return i;
if( j < = 1 )
return -ENOVAL;
if( array[i] < val )
j = 1.5*j;
if( array[i] > val )
j = 0.5*j;
i=j;
}

return -EFATAL;
}

/* Sorts */
#ifndef SORTS_H
#define SORTS_H

#include "error.h"

long bubble_sort(double array[], unsigned long array_length);

#endif /* SORTS_H */

/* Math functions */
#ifndef MATHF_H
#define MATHF_H

#include "error.h"

typedef struct {
double real;
double imaginary;
} complex;

typedef struct {
double real;
double complex;
} answer;

typedef struct {
complex one;
complex two;
} quadratic_answer;

complex sqrt_func(double val);
complex sqrt_long(double val);
double nroot(double val, double power);

quadratic_answer* quadratic_solver(double a, double b, double c);

double pow(double x, double y);

/*
double abs(double val)
Inline Function
Returns absolute value, modulus, of the value.
*/
#define ABS(val) (((val) > 0)?(return (val)):(return (0-(val))))

#endif /* MATHF_H */

/* Math functions */
#include "mathf.h"
#include "mem.h"

/*
double sqrt_func(double val)
Returns the sqrt of the value to a set accuracy.
*/
complex sqrt_func(double val)
{
#ifdef ACC
#error ACC already previously defined
#endif /* ACC */
#define ACC 15 /* small values */
int i=0;
double foo = abs(val);
complex root;
root.real = val;
root.imaginary = 0;

if( val==0 || val==1 )
return root;
if( val < 0 )
{
root.imaginary = 1;
if( root.real == -1 )
{
root.real = 0;
return root;
}
root.real = 0-root.real;
}

for (i=0; i < ACC; i++)
root.real = 0.5*(root.real + foo/root.real);

if( val < 0 )
{
root.imaginary = root.real;
root.real = 0;
}

return root;
#undef ACC
}

/*
double sqrt_func(double val)
Returns the sqrt of the value which is as accurate as possible.
*/
complex sqrt_long(double val)
{
complex root,root1;
double foo = abs(val);

root.real = val;
root.imaginary = 0;
root1.real = 0;
root1.imaginary = 0;

if( val==0 || val==1 )
return root;
if( val < 0 )
{
root.imaginary = 1;
if( root.real == -1 )
{
root.real = 0;
return root;
}
root.real = 0-root.real;
}

val = abs(val);
while (root1.real-root.real != 0)
{
root1.real = root.real;
root.real = 0.5*(root.real + foo/root.real);
}

if( val < 0 )
{
root.imaginary = root.real;
root.real = 0;
}

return root;
}


/*
double nroot(double val, double power)
Returns the power'th of val.
*/
double nroot(double val, double power)
{
/* Using newton-rhapson approximation for the root of
f(x) = x^y-k = 0 = > x_(r+1) = x_r - f(x)/f'(x)
= > x_(r+1) = x_r - f(x)/[(f(x+h) - f(x-h))/2h]
= > x_(r+1) = x_r - 2h*f(x)/(f(x+h) - f(x-h))
We'll let h be really small say 0.001.
*/
#ifdef H
#error H already previously defined
#endif /* H */
#define H 0.001 /* small values */
double root = val/4, root1=0;

if( val==0 || val==1 )
return val;

while(root1-root != 0)
{
root1 = root;
root = root - 2*H*(pow(root,power)-val)/(((pow(root+H,power)-val))
- (pow(root-H,power)-val));
}
return root;
#undef H /* small value */
}

/*
answer* quadratic_solver(double a, double b, double c, answer *ans)
Returns a pointer to the solution in the form of and "answer" struct
*/
quadratic_answer* quadratic_solver(double a, double b, double c)
{
quadratic_answer *ans;
complex root;

if((ans = xmalloc(sizeof(quadratic_answer))) == NULL)
return -ENOMEM;

c = b*b-4*a*c;
root = sqrt_long(c);
if (c< 0)
{
ans- > one.real = (0-b)/2*a;
ans- > two.real = (0-b)/2*a;
ans- > one.imaginary = root.imaginary/2*a;
ans- > two.imaginary = (0-root.imaginary)/2*a;
}
else
{
ans- > one.imaginary = 0;
ans- > two.imaginary = 0;
ans- > one.real = (0-b + root.real)/2*a;
ans- > two.real = (0-b - root.real)/2*a;
}
return ans;
}

/*
double pow(double x, double y)
Returns x to the power y.
*/
double pow(double x, double y)
{
unsigned int i=0;
double val = x;

if (y==0)
return 1;
if (y==1)
return x;

if ((int)y == y)
{
for(i=1; i< y; ++i)
val = val*x;
return val;
}
else
{

return val;
}

return -EFATAL;
}

/* memory functions */
#ifndef MEM_H
#define MEM_H

#include "error.h"

void* xmalloc(size_t sz);

#endif /* MEM_H */

/* memory functions */
#include "mem.h"

#define USE_STDMALLOC

#ifdef USE_STDMALLOC
#include < malloc.h >
#endif /* USE_STDMALLOC */

/*
int* xmalloc(size_t sz)
Allocates memory.
*/
void* xmalloc(size_t sz)
{
/* Temp until I get around to doing it properly */
return malloc(sz);
}

/* Sorts */

#include "sorts.h"

/*
long bubble_sort(double *array[], unsigned long array_length)
Famous bubble sort, no more needs to be said
*/
long bubble_sort(double array[], unsigned long array_length)
{
int i,j=0;
double tmp;
for (i=0; i< array_length-1; i++)
{
for (j=0; j< array_length-1-i; j++)
{
if (array[j+1] < array[j])
{
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}

return ENOERR;
}

by metolhead, posted on Wednesday, 21. July 2004 @ 2125.08 bst

content copyright it's respective owner. everything else is copyright ©2002-2005 këö¬t. are you copying us? foad, kthx&bye.