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..
This is a discussion on need easy help ASAP within the C Programming forums, part of the General Programming Boards category; Hi I'm Ali, My problem is as follows: *i need to write a program to calculate the INTEGRAL of sin(x), ...
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..
show us your best attempt
ssharish2005
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] .Originally Posted by adinclik
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;}
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..
Can u please give me a simple example because i dunno how to use rand() or double...Originally Posted by jafet
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...Originally Posted by ZuK
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); }
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().
By the way, "//" comments weren't defined in C until C99, IIRC.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
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;}
Code:#include <stdlib.h> int min(void){ int i; i= rand(); printf("%d", i); double d = i / RAND_MAX * PI; return 0; }
ssharish2005Code:X = ((double)rand() / ((double)(RAND_MAX)+(double)(1)) ); printf("%f\n",X,X*(PI)/2));
> 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.
typo?Code:int min(void){
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
What's the deal with all those casts?Originally Posted by ssharish2005
As for that integral. Correct me if I'm wrong, but for the given task this should suffice:Code:X = (double)rand() / (RAND_MAX + 1);
Code:double definite_integral = -cos(PI / 2.0) - (-cos(0.0));