Thread: Why doesn't it works?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    Post Why doesn't it works?

    I am just trying to use the command below

    .............
    ..................
    .......................
    Code:
    if (s[c]<1) {
         naccept=naccept+1;
    else {
          nreject=nreject+1;
    }
    
    printf("naccept=%lf   nreject=%lf\n",naccept, nreject);
    ....................
    .................

    I am just trying to use if conditional statement to test values used using array

    Thanx in advance

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    There's no matching closing braces before your else statement. What you need is

    Code:
    if (s[c]<1)
    {
       naccept=naccept+1;
    }
    
    else
    {
       nreject=nreject+1;
    }
    You should consider a better indentation style. I bet you would have seen the error that way.
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    11
    I am not sure if this is your problem, but in the code you provided, you are missing the closing brace for your first if statement. Since you are only using one line after both your if and else, the braces are not necessary at all.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no format specifier "&#37;lf" for printf.
    If you're looking to print a float or double, use "%f".
    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.

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

    not solved

    I have this "}" closing bracket in my source code. Since i was coding using vim accessing the linux on windows platform, it ws hard for me copy the code and paste it so i wrote it and missed "}". And %lf works with my compiler.

    And i am still having the problem. I get naccept =0 as well as nreject=0

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by curious View Post
    And %lf works with my compiler.
    Then it's a non-standard extension, so you should change it.

    And i am still having the problem. I get naccept =0 as well as nreject=0
    Then it's impossible to say why from that small snippet of code.
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    55

    not sloved

    Code:
    for (c=0; c<NUM; c++)
    	{
    		r[c]=x[c]*x[c]+y[c]*y[c];
    		printf("r=%lf\n",r[c]);
    		s[c]=r[c]*r[c];
    		printf("s[c]=%lf\n",s[c]);
    		if (s[c]<1){
    			naccept++;
    		}
    		else  {
    			nreject++;
    		}
    	}

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As many people like to say:
    Post the smallest possible compilable snippet of code that demonstrates the problem.
    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.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    55

    whole code

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define NUM 100
    #define IMAX 10
    #define ISEED 777861
    #define ISEED1 777862
    
    #define RND() ((double)random()/(double)RAND_MAX)
    
    main ()
    {
    
    	int i, naccept=0, nreject=0, ntrial=0, c;
    	double x[NUM],s[NUM],r[NUM],pi;
    
    	srandom(ISEED);
    	 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=%lf\n",r[c]);
    		s[c]=r[c]*r[c];
    		printf("s[c]=%lf\n",s[c]);
    		if (s[c]<1){
    			naccept++;
    		}
    		else  {
    			nreject++;
    		}
    	}
    		ntrial= naccept+nreject;
    		printf("naccept=%lf   nreject=%lf   ntrail=%lf\n",naccept,nreject,ntrial);
    		pi=4*(naccept/ntrial);
    		printf("pi=%.3lf\n",pi);
    }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Implicit main is bad: http://cpwiki.sourceforge.net/Implicit_main
    There is no identifier "&#37;lf" for printf.
    There is no function named srandom nor random.
    (But there are srand and rand specifically.)
    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.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Would that be something to do with the floating point number, since s[c] is float, as i assume from the above snap of code. So what values do you get printed, when you print s[c]. Whats sort of values does that vector contain.

    Some idea of values and input would help perhaps :-/

    ssharish

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You should perhaps get a quite lot linker error there when you compiler that code ????????????
    Shoudn't that be rand and srand() ??????????

    ssharish

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    55

    some ...

    It's basically values between 0.0000 to 1.70000 or something like that. my all other thing works expect that if condition statemen for s[c]. I don't why naccept and nreject aren't accumulating the values.

    Small part of my result:

    r=0.203552
    s[c]=0.041433
    r=1.747480
    s[c]=3.053686
    r=0.341290
    s[c]=0.116479
    r=1.269106
    s[c]=1.610629
    r=0.269674
    s[c]=0.072724

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    naccept, nreject and ntrial are all integers and not floats and should be printed with &#37;d.
    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.

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    try it with

    Code:
    if( s[c] < 1.0 ) {
    ssharish

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, 08: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, 10:29 PM
  5. it never works...
    By Ryce in forum Game Programming
    Replies: 5
    Last Post: 08-30-2001, 07:31 PM