Sie sind auf Seite 1von 5

/* Question 1 */ /* Function f(x)=x^2-3 for x=0 and its inverse function f^(-1) (x)=g(x) is v(x-3 ) with x=-3.

Write a program to calculate f(4) and g(13). */ #include <stdio.h> #include <math.h> int main() { /* Declare variables */ int x; double fx, gx; /* Calculate fx */ printf("Please input an integer which is bigger or equal 0 as x to calculate f(x)\n"); scanf("%d",&x); fx= pow(x,2)-3; printf("f(x)= %f \n",fx); /* Calculate gx */ printf("Please input an integer which is bigger or equal 3 as x to calculate gx \n"); scanf("%d", &x); gx= sqrt(x-3); printf("g(x)= %f \n",gx); return 0; }

/* Question 2 */ /* Write a probram to compute the rim area of a washer with the diameters for th e inner and outer circles of 3 and 5 centimeters, respectively. */ #include <stdio.h> #include <math.h> int main(){ /* Declare variables */ int x; /* Diameter for the inner circle */ int y; /* Diameter for the outer circle */ float z; /* Rim area of the washer */ /* Request input the diameters of for the inner and outer circles. */ printf("Please input the diameter for the inner circles as x \n"); scanf(" %d", &x); printf("Your input for inner circles diameter x is %d\n",x); printf("Please input the diameter for the outer circles as y\n"); scanf(" %d", &y); printf("Your input for outer circles diameter y is %d\n",y); /* Computing */ /* Area of the circle is [(M_PI)*(pow(d,2))]/4 */

/* Rim Area is the area of circle with outer diameter - area of circle with inner diameter */ z=(((M_PI)*pow(y,2))/4)-(((M_PI)*(pow(x,2))/4)); /* Displaying output */ printf(" Area of the rim = %f cm^2\n", z); return 0; }

/* Question 3i */ /* The straight-line distance of two points (x_1,y_1) and (x_2,y_2) in a Ca rtesian plane can be calculated by the formula d= sqrt[pow(x1-x2,2)+pow(y1-y2,2)] Write a program to calculate the distance for a point (2,3) and (4,5) */ #include <stdio.h> #include <math.h> int main(){ /* Declare Variable */ int x1,x2,y1,y2; double d; x1=2; x2=4; y1=3; y2=5; /* Computing */ d= sqrt(pow(x1-x2,2)+pow(y1-y2,2)); /* Display output */ printf(" Distance for a point (2,3) and (4,5) is %f units\n",d); return 0; }

/* Question 3ii */ /* The straight-line distance of two points (x_1,y_1) and (x_2,y_2) in a Ca rtesian plane can be calculated by the formula d= sqrt[pow(x1-x2,2)+pow(y1-y2,2)] Write a program to calculate the distance for a point (4,3) and (6,5) */ #include <stdio.h> #include <math.h> int main(){ /* Declare Variable */

int x1,x2,y1,y2; double d; x1=4; x2=6; y1=3; y2=5; /* Computing */ d= sqrt(pow(x1-x2,2)+pow(y1-y2,2)); /* Display output */ printf(" Distance for a point (4,3) and (6,5) is %f units\n",d); return 0; }

/* Question 3iii */ /* The straight-line distance of two points (x_1,y_1) and (x_2,y_2) in a Ca rtesian plane can be calculated by the formula d= sqrt[pow(x1-x2,2)+pow(y1-y2,2)] Write a program to calculate the distance for a point (1,3) and (4,2) */ #include <stdio.h> #include <math.h> int main(){ /* Declare Variable */ int x1,x2,y1,y2; double d; x1=1; x2=4; y1=3; y2=2; /* Computing */ d= sqrt(pow(x1-x2,2)+pow(y1-y2,2)); /* Display output */ printf(" Distance for a point (1,3) and (4,2) is %f units\n",d); return 0; }

/* Question 4i */ /* Solve the following quadratic equation. If there is no solution in the r eal domain, the result of NaN, which stands for Not-a-Number, must be printed ou t.

2*pow(x,2)-4*x+1 */ #include <stdio.h> #include <math.h> int main(){ /* Declare Variable */ int a,b,c; double x1,x2; a=2; b=-4; c=1; x1=(-b + sqrt( pow(b,2)-4*a*c))/(2*a); x2=(-b - sqrt( pow(b,2)-4*a*c))/(2*a); /* Displaying output */ printf("x1=%f\n ", x1); printf("x2=%f\n ", x2); return 0; }

/* Question 4ii */ /* Solve the following quadratic equation. If there is no solution in the real d omain, the result of NaN, which stands for Not-a-Number, must be pr inted out. pow(x,2) + (2*x) +1 */ #include <stdio.h> #include <math.h> int main(){ /* Declare Variable */ int a,b,c; double x1,x2; a=1; b=2; c=1; x1=(-b + sqrt( pow(b,2)-4*a*c))/(2*a); x2=(-b - sqrt( pow(b,2)-4*a*c))/(2*a); /* Displaying output */ printf("x1=%f\n ", x1); printf("x2=%f\n ", x2); return 0; }

/* Question 5 */ /* Calculate the value of the following functions for each one at x=3.5 and y=4.2 (a) g1(x) = 2*x*sin(x) + cos(x) +((4*x)+3)/3*pow(x,2)+2*x+4 (b) g2(x)_= pow(x,2)*sqrt(2*x) (c) g3(x) = 2*x*sin(y) + cos(x) + (4*x+3)/(3*pow(x,2)+2*y+4) (d) g4(x) = pow(x,2)*sqrt(2*y) */ #include <stdio.h> #include <math.h> int main(){ /* Declare Variables */ double x,y,g1x,g2x,g3x,g4x; x=3.5; y=4.2; g1x=2*x*sin(x) + cos(x) +((4*x)+3)/3*pow(x,2)+2*x+4; g2x=pow(x,2)*sqrt(2*x); g3x=2*x*sin(y) + cos(x) + (4*x+3)/(3*pow(x,2)+2*y+4); g4x=pow(x,2)*sqrt(2*y); /* Display output */ printf(" %f\n %f\n %f\n %f\n", g1x,g2x,g3x,g4x); return 0; }

Das könnte Ihnen auch gefallen