Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
not understanding why it will not compile
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="noze2000" data-source="post: 45538"><p>I wrote this small program for C on a windows machine. It comlies and runs perfect.</p><p>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<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite3" alt=":(" title="Frown :(" loading="lazy" data-shortname=":(" /> ?</p><p>regards</p><p></p><p>noze2000</p><p></p><p>[code]/*</p><p></p><p> bits of the code has been adapted from</p><p> [url]http://www.cs.caltech.edu/~arvo/code/Rand.C[/url]</p><p>*/</p><p></p><p>/*-------------------------------------------------------------------------*</p><p> * M E T H O D 2 *</p><p> * *</p><p> * From "The Multiple Prime Random Number Generator," by Alexander Haas, *</p><p> * ACM Transactions on Mathematical Software, Vol. 13, No. 4, December *</p><p> * 1987, pp. 368-381. *</p><p> * *</p><p> *-------------------------------------------------------------------------*/</p><p></p><p></p><p>#include <fstream></p><p></p><p>#include <stdlib.h></p><p>#include <stdio.h></p><p>#include <math.h></p><p>#include <time.h></p><p>#define pi 3.14159265358979</p><p> </p><p></p><p>#ifndef ABS</p><p>#define ABS( x ) ((x) > 0 ? (x) : -(x))</p><p>#endif</p><p></p><p>int time_steps_per_periode;</p><p>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;</p><p>int m, i, j ,r , brugt_normale_tal, nye_tal,u,u0,u1,u2,u3,u1000;</p><p>clock_t tid1,tid2,temptid;</p><p></p><p></p><p>void Seed(){</p><p> </p><p> m=971,i=11113,j=104322;</p><p> r=4181;</p><p>}</p><p></p><p></p><p></p><p></p><p>void initialize() {</p><p></p><p> </p><p> tid1=clock();</p><p> Seed(); // sÊtter begyndelse for tilfÊldighedrÊkke</p><p> </p><p></p><p>}</p><p> </p><p></p><p></p><p>double random() // lav eet tilfÊldigt tal</p><p> {</p><p> </p><p> if( (m += 7 ) >= 9973 ) m -= 9871;</p><p> if( (i += 1907 ) >= 99991 ) i -= 89989;</p><p> if( (j += 73939) >= 224729 ) j -= 96233;</p><p> r = ((r * m + i + j) % 100000) / 10;</p><p> return r * 1.00010001E-4;</p><p> }</p><p></p><p></p><p></p><p></p><p>void polar() // brug Polar metoden til at lave 2 normalfordelte tal</p><p>{</p><p></p><p></p><p> double v1,v2,u1,u2,n=0.0;</p><p></p><p> // s der laves 2 tal til at starte med</p><p> do {</p><p> u1=random();</p><p> u2=random();</p><p></p><p> v1=2*u1-1;</p><p> v2=2*u2-1;</p><p></p><p> n=v1*v1+v2*v2;</p><p> }while (n>1);</p><p> tilf_tal_1 = v1 * sqrt(-2 * log(n) / n);</p><p> tilf_tal_2 = v2 * sqrt(-2 * log(n) / n);</p><p></p><p>}</p><p></p><p></p><p></p><p></p><p></p><p></p><p>void taeller_af_tilf_tal () // kontrollerer at de tilfÊldige tal ikke benyttes flere gange</p><p>{</p><p></p><p> switch(nye_tal){</p><p> case 0 : {polar(); tilf_tal = tilf_tal_1 ;}</p><p> break;</p><p> case 1 : {tilf_tal = tilf_tal_2 ; nye_tal=0;}</p><p> break;</p><p> }</p><p>}</p><p> </p><p>void tid_tilbage(int x,int y) { </p><p> int u1000=(int)y/500,u0=(int)y/100,u1=(int)y/3, // variable til tidstagning</p><p> u2=(int)y/2, u3=(int)(2.0/3.0*y);// rutine til estimering af tid tilbage for udregningen</p><p> double a,b,c;;</p><p> if(x==u1000){ temptid = clock () - tid1;</p><p> a=(500.0*(temptid)/((double)CLOCKS_PER_SEC));</p><p> b=fmod(a , 60); // sekunder tilbage</p><p> c=int(a/60); // minutter tilbage</p><p> printf(" 0.2%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}</p><p> </p><p> if(x==u0) { temptid=clock ();</p><p> a=(100.0*(temptid)/((double)CLOCKS_PER_SEC));</p><p> b=fmod(a , 60); // sekunder tilbage</p><p> c=int(a/60); // minutter tilbage</p><p> printf(" 1%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}</p><p> </p><p> </p><p> if(x==u1) {temptid=clock ();;</p><p> a=(2*(temptid)/((double)CLOCKS_PER_SEC));</p><p> b=fmod(a , 60); // sekunder tilbage</p><p> c=int(a/60); // minutter tilbage</p><p> printf("33%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}</p><p> </p><p> </p><p> </p><p> </p><p> if(x==u2) {temptid=clock ();</p><p> a=((temptid)/((double)CLOCKS_PER_SEC));</p><p> b=fmod(a , 60); // sekunder tilbage</p><p> c=int(a/60); // minutter tilbage</p><p> printf("50%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}</p><p> </p><p> </p><p> if(x==u3) {temptid=clock ();</p><p> a=((temptid)/(2.0*(double)CLOCKS_PER_SEC));</p><p> b=fmod(a , 60); // sekunder tilbage</p><p> c=int(a/60); // minutter tilbage</p><p> printf("66%% : Estimeret tid tilbage: %2.0f min %2.0f sek\n\n",c ,b);}</p><p> } </p><p></p><p></p><p></p><p></p><p></p><p></p><p>void gem(char *file, double a)</p><p>{ using namespace std;</p><p> ofstream savefile( file, ios::binary | ios::app );</p><p> savefile << a << "\n";</p><p> savefile.close();</p><p>}</p><p></p><p>void gem(char *file, long double a,long double b)</p><p>{</p><p> using namespace std; </p><p> ofstream savefile( file, ios::binary | ios::app );</p><p> savefile << a << "\t" << b << "\n";</p><p> savefile.close();</p><p>}</p><p></p><p>void init_gem(char *file){</p><p> using namespace std;</p><p> ofstream savefile(file,ios::trunc);</p><p> savefile.close();</p><p>}</p><p></p><p></p><p> </p><p></p><p></p><p>void main() {</p><p> printf("Monte Carlo simulation med Control variable\n\n");</p><p> initialize();</p><p> </p><p> double b,c;</p><p></p><p> int files = 100 ;</p><p> const int tal=100000;</p><p> </p><p> </p><p> </p><p> double e[tal];</p><p> char a[20];</p><p> for(int w = 0;w<files;w++)</p><p> {</p><p> if (w<10) sprintf(a, "normal_%d%d.txt",0, w+1);</p><p> else sprintf(a, "normal_%d.txt", w+1);</p><p> char *fil = a;</p><p> init_gem(fil);</p><p> printf("laver 100000 tal...\t");</p><p> for(int s=0;s<tal;s++) // lav tal</p><p> {</p><p> taeller_af_tilf_tal();</p><p> e[s] = tilf_tal;</p><p> </p><p> }</p><p></p><p> printf("laver en fil af tallene....\n");</p><p> printf("fil #%d",w+1);</p><p> using namespace std;</p><p> ofstream savefile( fil, ios::binary | ios::app );</p><p> </p><p> </p><p> for(s=0;s<tal;s++)</p><p> {savefile << e[s] << "\n";}</p><p> </p><p> savefile.close();</p><p> }</p><p> </p><p> </p><p>tid2 = clock();</p><p>double tid = (tid2 - tid1)/(CLOCKS_PER_SEC / (double) 1000.0);</p><p> b=fmod(tid , 60); // sekunder tilbage</p><p> c=int(tid/60); // minutter tilbage</p><p> printf("\ntid brugt: %2.0f min %2.5f sek\n\n",c ,b);</p><p> printf("\n%f",tid);</p><p> </p><p>}</p><p>[/code]</p></blockquote><p></p>
[QUOTE="noze2000, post: 45538"] 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 [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); } [/code] [/QUOTE]
Verification
What is Apple's smallest desktop computer called?
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
not understanding why it will not compile
Top