Why doesn't it works?

This is a discussion on Why doesn't it works? within the C Programming forums, part of the General Programming Boards category; i have no problem getting random number and r and s but i didn't get naccept , nreject and ntrail....

  1. #16
    Registered User
    Join Date
    May 2008
    Posts
    55
    i have no problem getting random number and r and s but i didn't get naccept , nreject and ntrail.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    The following code correct(er):
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    #define NUM 100
    #define IMAX 10
    #define ISEED 777861
    #define ISEED1 777862
    
    #define RND() ((double)rand()/(double)RAND_MAX)
    
    int main ()
    {
    
    	int i, naccept=0, nreject=0, ntrial=0, c;
    	double x[NUM],s[NUM],r[NUM],pi;
    
    	srand( time(NULL) );
    	naccept=0; nreject=0; ntrial=0;
    	for (i=0; i<NUM; i++)
    	{
    		x[i]=RND();
    	}
    
    	int j;
    	double y[NUM];
    	//srandom(ISEED1);
    
    	for (j=0; j<NUM; j++)
    	{
    		y[j]=RND();
    	}
    	for (c=0; c<NUM; c++)
    	{
    		r[c]=x[c]*x[c]+y[c]*y[c];
    		printf("r=&#37;f\n",r[c]);
    		s[c]=r[c]*r[c];
    		printf("s[c]=%f\n",s[c]);
    		if (s[c]<1.0){
    			naccept++;
    		}
    		else  {
    			nreject++;
    		}
    	}
    	ntrial= naccept+nreject;
    	printf("naccept=%d   nreject=%d   ntrail=%d\n",naccept,nreject,ntrial);
    	pi=4.0*((double)naccept/(double)ntrial);
    	printf("pi=%.3f\n",pi);
    }
    I suggest you update the code because are sure using some non-standard stuff.
    It almost produces correct pi (ca 3.04x something I believe).

    And only initialize the seed once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    55
    hi Elysia , you are right it worked but it's not printing the value of pi

    Thank you Elysia and ssharish

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    55
    Sorry Elysia it worked

    Thank you very much

  5. #20
    Registered User
    Join Date
    May 2008
    Posts
    55

    One more time

    This is my second program after learning c on my own. Can you please clarify those things . I am still confuse with rand and random stuff as well as seed.

    Those stuff about %d and double.

    Thanx,

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    When printing stuff with printf, you need to tell printf what type you're passing (it's not psychic).
    &#37;d tells printf the type is int. %f tells printf it's a double (or float).
    And the random stuff works by initializing the seed once, and then calling rand. Rand will use the seed to generate random numbers. If you seed twice or more, you will see that you might actually get the same number as before (thus not really random).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    &#37;lf in C89 is undefined behavior. %lf in C99 is legal, but the l is ignored.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM
  2. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM
  3. Works only part of the time?
    By scr0llwheel in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2003, 07:34 PM
  4. Programs works one way, not another. VC++ 6.0 help.
    By Cheeze-It in forum Windows Programming
    Replies: 4
    Last Post: 12-10-2002, 09:29 PM
  5. it never works...
    By Ryce in forum Game Programming
    Replies: 5
    Last Post: 08-30-2001, 07:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21