Beruflich Dokumente
Kultur Dokumente
// input: an integer num // output: returns 1 if num is positive, 0 otherwise return num >= 0;
void calculateEX(int x, int n) { // input: two integers x and n // output: calculates e^x by Talyor series with precision level of n terms and prints the calculation on the screen float ex = 1.00, nth_term=1; int i; printf("\ne^%i=1.00", x); // the 0-th term for (i = 1; i <= n; i++) { nth_term*=(x/(float)i); ex += nth_term; printf("+%0.2f", nth_term); } printf("=%0.2f\n\n", ex); } int isInRange(int num, int min, int max) { // input: 3 integers num, min and max // output: returns 1 if the num is in the range between min and max so min<=num<=max, 0 otherwise return num >= min && num <= max; } int cBinary(int num) { // input: an integer num // output: returns the binary conversion of num int weight=1, converted = 0; while (num != 0) { converted += ((num % 2) * weight); //adding the next digit to the converted number num = num / 2;//decreaing num to get the next digit in the next loop weight *= 10; //digit weight increasing } return converted; } int isBinary(int num){ // input: an integer num // output: returns 1 whether num represents a binary number between -11111111 and 11111111, and 0 otherwise int num_digit; if(!isInRange(num,-11111111,11111111)) return 0; //checks whether num is in the required range while(num!=0){ //looping num digits and checks whether each digit equals 0 or 1 num_digit=num%10; //getting the next digit of num if (!isInRange(num_digit,-1,1)) return 0; //checks whether num_digit is a valid binary number digit num/=10; //decreasing num in order to get the next digit in he next loop } return 1; //returns 1 if no error was dedected } int cDecimal(int num) { // input: an integer num that represants a binary number // output: returns the decimal conversion of num int weight=1, converted = 0; while (num != 0) { converted += ((num % 10) * weight); //adding the next digit to the converted number num = num / 10;//decreaing num to get the next digit in the next loop weight *= 2; //digit weight increasing } return converted; } float random_value(int max){ // input: an integer max // output: returns a float between 0 and max return (float)max*rand()/RAND_MAX; } float radiusSqr(float x, float y){ // input: two floats x and y represanting a point position // output: returns the square of the distance of the point from the origin return x*x+y*y; } int throwArrow(void) { // input: no input // output: 1 if a random point inside a square of 5X5 is inside the circle blocked by the square, 0 otherwise. float x,y; x=random_value(5); y=random_value(5); return radiusSqr(x,y)<25; //returns whether the point distance from origin is less than 5 }
void calculate_pi(void){
// input: no input //output: calculates pi approximation by Monte karlo method and prints the calculation results as described in the exercise instructions const float PI=3.141592653589793; int i, in_circle_arrow=0; float pi_approximation; for(i=1;i<=2500;i++){ //throwing arrows in_circle_arrow+=throwArrow(); } pi_approximation=in_circle_arrow*0.0016; //=(in_circle_arrow/2500)*4 //printing the results to the screen printf("\nInside: %i\n",in_circle_arrow); printf("Total points: 2500"); printf("\nPI approximation: %.3f",pi_approximation); printf("\nDifference:3.141-%0.3f=%0.3f\n\n", pi_approximation, PI-pi_approximation); }