Thread: need easy help ASAP

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Unhappy need easy help ASAP

    Hi I'm Ali,

    My problem is as follows:

    *i need to write a program to calculate the INTEGRAL of sin(x), from x=0 to x=pi/2.

    *Also, i need to produce 1000 random numbers within 0 and pi/2.

    please help..

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    show us your best attempt

    ssharish2005

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    i'm noob in c prog.

    so all i can do is find sin(x) for different values of x. you know, the basic printf and scanf thingy..

    i can probably integrate sin(x), but i cant possibly find the integral of sin(x) for x=0 to x=pi/2...

    I know pretty much how to generate random numbers, but i cant confine them into a limit (in this case 0 to pi/2).

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by adinclik
    i'm noob in c prog.

    so all i can do is find sin(x) for different values of x. you know, the basic printf and scanf thingy..

    i can probably integrate sin(x), but i cant possibly find the integral of sin(x) for x=0 to x=pi/2...

    I know pretty much how to generate random numbers, but i cant confine them into a limit (in this case 0 to pi/2).
    Maybe try some kind of numerical integration technique like simpson's rule to find the integral of sin(x) from [0 pi/2] .

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If you're not too picky, you can use rand(), convert to double and reduce into range [0, PI/2].
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    random number generator

    OK
    i found a random number generator program and it goes like this

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    #define MAXBIT 30
    
    void sobseq(float *x, float *y) {
    
      // Returns two quasi-random numbers for a 2-dimensional Sobel
      // sequence. Adapted from Numerical Recipies in C.
    
      int j, k, l;
      unsigned long i, im,ipp;
    
      // The following variables are "static" since we want their
      // values to remain stored after the function returns. These
      // values represent the state of the quasi-random number generator.
    
      static float fac;
      static int init=0;
      static unsigned long in, ix[3], *iu[2*MAXBIT+1];
      static unsigned long mdeg[3]={0,1,2};
      static unsigned long ip[3]={0,0,1};
      static unsigned long iv[2*MAXBIT+1]=
         {0,1,1,1,1,1,1,3,1,3,3,1,1,5,7,7,3,3,5,15,11,5,15,13,9};
    
      // Initialise the generator the first time the function is called.
    
      if (!init) {
        init = 1;
        for (j = 1, k = 0; j <= MAXBIT; j++, k += 2) iu[j] = &iv[k];
        for (k = 1; k <= 2; k++) {
          for (j = 1; j <= mdeg[k]; j++) iu[j][k] <<= (MAXBIT-j);
          for (j = mdeg[k]+1; j <= MAXBIT; j++) {
    	ipp = ip[k];
    	i = iu[j-mdeg[k]][k];
    	i ^= (i >> mdeg[k]);
    	for (l = mdeg[k]-1; l >= 1; l--) {
    	  if (ipp & 1) i ^= iu[j-l][k];
    	  ipp >>= 1;
    	}
    	iu[j][k] = i;
          }
        }
        fac = 1.0/(1L << MAXBIT);
        in = 0;
      }
    
      // Now calculate the next pair of numbers in the 2-dimensional
      // Sobel sequence.
    
      im = in;
      for (j = 1; j <= MAXBIT; j++) {
        if (!(im & 1)) break;
        im >>= 1;
      }
      assert(j <= MAXBIT);
      im = (j-1)*2;
      ix[1] ^= iv[im+1];
      ix[2] ^= iv[im+2];
      *x = ix[1] * fac;
      *y = ix[2] * fac;
      in++;
    }
    
    
    int main(void) {
    
      // Test the Sobel generator.
    
      int i;
      float x, y;
    
      for (i = 0; i < 1000; i++) {
        sobseq(&x, &y);
        printf("%f %f\n", x, y);
      }
      getchar();  
      getchar();
      return 0;
    }

    but i still need to know how to limit the random number being generated to between 0 and Pi/2.

    BTW... does anybody know how to use monte carlo technique for c programming??

    I need it for my assignment..

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by jafet
    If you're not too picky, you can use rand(), convert to double and reduce into range [0, PI/2].
    Can u please give me a simple example because i dunno how to use rand() or double...

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Q: What does rand() return ?
    A: An int between 0 and RAND_MAX.
    Q:How can I convert an int to a double ?
    A: Cast.
    Q: How can I change the range ?
    A: Divide by original range and multiply the result with the new range.
    Kurt

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by ZuK
    Q: What does rand() return ?
    A: An int between 0 and RAND_MAX.
    Q:How can I convert an int to a double ?
    A: Cast.
    Q: How can I change the range ?
    A: Divide by original range and multiply the result with the new range.
    Kurt
    I've just written a simple program which returns 1 random number...

    BUT WHY DOES IT ALWAYS RETURN AROUND 27000!!!???

    is there a trick to fix this?

    also, can you show me how to limit the range of the random number from 0 to pi/2?
    Code:
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <time.h>  
    
    int main(void)
    {
      int i;
      
      srand(time(NULL));
      
      i = rand();
      
      printf ("Your random number is %d\n", i);  
    
      printf ("This compiler can generate random numbers from 0 to %d\n", RAND_MAX);
    
      return(0);
    }

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    double is basically a float, but with more precision and a higher range. It's usually implemented as a double word in a computer.

    rand() returns a pseudorandom number using a built-in algorithm. It is always seeded with 1 on program startup. You can re-seed it with anytime by passing an int to srand().

    Code:
    #include <stdlib.h>
    
    int i = rand(); //returns a random number between 1 and RAND_MAX
    printf("%d", i); //prints something like "12345"
    double d = i / RAND_MAX * PI; //something like 1.4blahblahblah
    By the way, "//" comments weren't defined in C until C99, IIRC.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Why doesn't this work??!!

    Code:
    #include <stdlib.h>
    int min(void){
        
        int i; 
        
        i= rand(); 
    
        printf("%d", i); 
    
        double d = i / RAND_MAX * PI; 
        return 0;
    }

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    X = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );
            printf("%f\n",X,X*(PI)/2));
    ssharish2005

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why doesn't this work??!!
    You didn't call srand() before calling rand()
    You don't have any print statements after your calculation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    int min(void){
    typo?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Quote Originally Posted by ssharish2005
    Code:
    X = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) );
            printf("%f\n",X,X*(PI)/2));
    ssharish2005
    What's the deal with all those casts?
    Code:
    X = (double)rand() / (RAND_MAX + 1);
    As for that integral. Correct me if I'm wrong, but for the given task this should suffice:
    Code:
    double definite_integral = -cos(PI / 2.0) - (-cos(0.0));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  3. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM