N
noze2000
Guest
I wrote this small program for C on a windows machine. It comlies and runs perfect.
yet it will not compile in Xcode for some reason. There are a few mistakes but most of them comes from xcode not recognizing #include <fstream>. Can anyone help me clean up the code a little since I cannot find any documentation on it on the web ?
regards
noze2000
yet it will not compile in Xcode for some reason. There are a few mistakes but most of them comes from xcode not recognizing #include <fstream>. Can anyone help me clean up the code a little since I cannot find any documentation on it on the web ?
regards
noze2000
Code:
/*
bits of the code has been adapted from
[url]http://www.cs.caltech.edu/~arvo/code/Rand.C[/url]
*/
/*-------------------------------------------------------------------------*
* M E T H O D 2 *
* *
* From "The Multiple Prime Random Number Generator," by Alexander Haas, *
* ACM Transactions on Mathematical Software, Vol. 13, No. 4, December *
* 1987, pp. 368-381. *
* *
*-------------------------------------------------------------------------*/
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#define pi 3.14159265358979
#ifndef ABS
#define ABS( x ) ((x) > 0 ? (x) : -(x))
#endif
int time_steps_per_periode;
double cvar,perioder,tid,gnst, s0, rr, X, dt, tilf_tal, tilf_tal_1, tilf_tal_2,stdaf,geometrisk_pris, vol,Pris_estimat,var_a ,tvar_a,var_g,tvar_g ,covarians, varians_estimat, pris_a,pris_g,pris_1=0.0,pris_2=0.0;
int m, i, j ,r , brugt_normale_tal, nye_tal,u,u0,u1,u2,u3,u1000;
clock_t tid1,tid2,temptid;
void Seed(){
m=971,i=11113,j=104322;
r=4181;
}
void initialize() {
tid1=clock();
Seed(); // sÊtter begyndelse for tilfÊldighedrÊkke
}
double random() // lav eet tilfÊldigt tal
{
if( (m += 7 ) >= 9973 ) m -= 9871;
if( (i += 1907 ) >= 99991 ) i -= 89989;
if( (j += 73939) >= 224729 ) j -= 96233;
r = ((r * m + i + j) % 100000) / 10;
return r * 1.00010001E-4;
}
void polar() // brug Polar metoden til at lave 2 normalfordelte tal
{
double v1,v2,u1,u2,n=0.0;
// s der laves 2 tal til at starte med
do {
u1=random();
u2=random();
v1=2*u1-1;
v2=2*u2-1;
n=v1*v1+v2*v2;
}while (n>1);
tilf_tal_1 = v1 * sqrt(-2 * log(n) / n);
tilf_tal_2 = v2 * sqrt(-2 * log(n) / n);
}
void taeller_af_tilf_tal () // kontrollerer at de tilfÊldige tal ikke benyttes flere gange
{
switch(nye_tal){
case 0 : {polar(); tilf_tal = tilf_tal_1 ;}
break;
case 1 : {tilf_tal = tilf_tal_2 ; nye_tal=0;}
break;
}
}
void tid_tilbage(int x,int y) {
int u1000=(int)y/500,u0=(int)y/100,u1=(int)y/3, // variable til tidstagning
u2=(int)y/2, u3=(int)(2.0/3.0*y);// rutine til estimering af tid tilbage for udregningen
double a,b,c;;
if(x==u1000){ temptid = clock () - tid1;
a=(500.0*(temptid)/((double)CLOCKS_PER_SEC));
b=fmod(a , 60); // sekunder tilbage
c=int(a/60); // minutter tilbage
printf(" 0.2%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}
if(x==u0) { temptid=clock ();
a=(100.0*(temptid)/((double)CLOCKS_PER_SEC));
b=fmod(a , 60); // sekunder tilbage
c=int(a/60); // minutter tilbage
printf(" 1%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}
if(x==u1) {temptid=clock ();;
a=(2*(temptid)/((double)CLOCKS_PER_SEC));
b=fmod(a , 60); // sekunder tilbage
c=int(a/60); // minutter tilbage
printf("33%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}
if(x==u2) {temptid=clock ();
a=((temptid)/((double)CLOCKS_PER_SEC));
b=fmod(a , 60); // sekunder tilbage
c=int(a/60); // minutter tilbage
printf("50%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}
if(x==u3) {temptid=clock ();
a=((temptid)/(2.0*(double)CLOCKS_PER_SEC));
b=fmod(a , 60); // sekunder tilbage
c=int(a/60); // minutter tilbage
printf("66%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}
}
void gem(char *file, double a)
{ using namespace std;
ofstream savefile( file, ios::binary | ios::app );
savefile << a << "\n";
savefile.close();
}
void gem(char *file, long double a,long double b)
{
using namespace std;
ofstream savefile( file, ios::binary | ios::app );
savefile << a << "\t" << b << "\n";
savefile.close();
}
void init_gem(char *file){
using namespace std;
ofstream savefile(file,ios::trunc);
savefile.close();
}
void main() {
printf("Monte Carlo simulation med Control variable\n\n");
initialize();
double b,c;
int files = 100 ;
const int tal=100000;
double e[tal];
char a[20];
for(int w = 0;w<files;w++)
{
if (w<10) sprintf(a, "normal_%d%d.txt",0, w+1);
else sprintf(a, "normal_%d.txt", w+1);
char *fil = a;
init_gem(fil);
printf("laver 100000 tal...\t");
for(int s=0;s<tal;s++) // lav tal
{
taeller_af_tilf_tal();
e[s] = tilf_tal;
}
printf("laver en fil af tallene....\n");
printf("fil #%d",w+1);
using namespace std;
ofstream savefile( fil, ios::binary | ios::app );
for(s=0;s<tal;s++)
{savefile << e[s] << "\n";}
savefile.close();
}
tid2 = clock();
double tid = (tid2 - tid1)/(CLOCKS_PER_SEC / (double) 1000.0);
b=fmod(tid , 60); // sekunder tilbage
c=int(tid/60); // minutter tilbage
printf("\ntid brugt: %2.0f min %2.5f sek\n\n",c ,b);
printf("\n%f",tid);
}