#include <stdio.h>
int diameter_fn(int r)
{
return (2 * r);
}
void circumference_fn(int r)
{
float pie = 22 / 7;
float circum = (2 * pie * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r)
{
float pie = 22 / 7;
float area = (22 * r * r / 7);
printf(" & the Area = %f", area);
}
int main()
{
printf("\nName = Parth_Agrawal & UID = 22BCS10924\n");
int radius;
printf("Enter the Radius of Circle:\t\t");
scanf("%d", &radius);
printf("\nDiameter = %d", diameter_fn(radius));
circumference_fn(radius);
area_fn(radius);
return 0;
}
I want to calculate Circumference, diameter and area of circle using functions yet I get non-perfect Circumference and area values.
I already tried replacing the float with double, %f with %lf etc but I am always getting the Circumference and area in xxx.0000 format,I.e, similar to Int converted to float format.
Like the area for 4 unit radius is 50.27 but it is giving me 50.000000 which is too much annoying.
This is the Result I am getting
whereas this is the Result which I should get
... but it is giving me 50.000000 ...
OP is using integer math in many places where floating point math is needed.
void circumference_fn(int r) {
float pie = 22 / 7; // Integer math!!
float circum = (2 * pie * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r) {
float pie = 22 / 7; // Integer math!! pie not used
float area = (22 * r * r / 7);// Integer math!!
printf(" & the Area = %f", area);
}
Instead use FP math.
Scant reason to use float. Use double as the default FP type.
Rather than approximate π with 22/7, use a more precise value.
#define PIE 3.1415926535897932384626433832795
void circumference_fn(int r) {
double circum = (2 * PIE * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r) {
double area = PIE * r * r;
printf(" & the Area = %f", area);
}
Other
All three functions should take a double argument and return a double.
Use "%g" for printing. It is more informative with wee values and less verbose with large ones.
#Shawn is right: you are using integer math to calculate Pi. You should #include <math.h> and use M_PI instead of trying to calculate it yourself.
Related
I'm basically trying to make a math rotation program in C. But the output is always wrong. P(x,y) is rotated about Q(r,s); clockwise (direction=1) or anticlockwise (direction=0). The a,b,c are angles in triple ,I guess question meant c is in hundred's then b is in ten's and a is unit's.
Input:
0
7 3
0 1 1
0 0
Output: -3 7
Whereas I'm getting -5 5.
Thanks for your time if you help me.
Original question link: https://www.codechef.com/problems/DSPC305
i found another question by the same uploader which uses TRIPLE too. He further added a note :Triple is defined by a,b,c where a is base, b is height and c is hypotenuse of a triangle. Each triple corresponds to an angle given by cosA= a/c
#include<stdio.h>
#include<math.h>
int main() {
int x,y,a,b,direction,c,r,s,xnew,ynew;
scanf("%i", &direction);
scanf("%i %i", &x, &y);
scanf("%i %i %i" , &a, &b, &c);
scanf("%i %i", &r, &s);
float PI = 3.1415926535897932384626;
float theta = ((c*100+b*10+a)*PI)/180;
if (direction==1)
{
xnew= (x-r) * cos(theta) + (y-s) * sin(theta);
ynew= -(x-r) * sin(theta) + (y-s) * cos(theta);
printf("%i %i", xnew+r, ynew+s);
}
if (direction==0)
{
xnew =( (x-r) * ((cos(theta))) - (y-s) * sin(theta));
ynew =( (x-r) * ((sin(theta))) + (y-s) * cos(theta));
printf("%i %i", (xnew+r), (ynew+s));
}
return 0;
}
This
float theta = ((c*100+b*10+a)*PI)/180;
has nothing to do with the definition of a triple.
You can use this code:
#include<stdio.h>
#include<math.h>
int main()
{
double xp,yp,xq,yq,a,b,c;
double t,xn,yn;
int z;
scanf("%d",&z);
scanf("%lf%lf",&xp,&yp);
scanf("%lf%lf%lf",&a,&b,&c);
scanf("%lf%lf",&xq,&yq);
t=asin(b/c);
if(z==0)
{
xn=xp*cos(t)-yp*sin(t)-xq*cos(t)+yq*sin(t)+xq;
yn=xp*sin(t)+yp*cos(t)-xq*sin(t)-yq*cos(t)+yq;
}
else
{
xn=xp*cos(t)+yp*sin(t)-xq*cos(t)-yq*sin(t)+xq;
yn=-xp*sin(t)+yp*cos(t)+xq*sin(t)-yq*cos(t)+yq;
}
printf("%0.lf %0.lf",xn,yn);
return 0;
}
This code gave correct output for both of the test cases provided in the question.
Do tell if it worked :)
Why am I getting an output of 0? I think there's something wrong about my angle conversion and possibly my equation, yet fiddling around with it and moving some stuff always gives me the same result.
My goal is to write a C code that will compute the angle θ for any two given vectors u and v.
#include <stdio.h>
#include <math.h>
int main()
{
double ux, uy;
double vx, vy;
double inner_product(double vx, double vy, double ux, double uy);
double v;
double u;
double i;
double x;
double k;
double pi;
double angle;
double p;
ux = 1.0;
uy = 1.0;
vx = 1.0;
vy = 1.0;
printf ("input value for ux\n", ux);
scanf_s ("%f", &ux);
printf ("input value for uy\n", uy);
scanf_s ("%f", &uy);
printf ("input value for vx\n", vx);
scanf_s ("%f", &vx);
printf("input value for vy\n", vy);
scanf_s ("%f", &vy);
u = ux * vx;
v = uy * vy;
i = u * v;
x = u * u;
k = v * v;
pi = acos(-1.0);
p = acos(i / (sqrt(x * k)));
angle = ((p * 180) / pi); //converting from radians to degrees
printf("%f", angle);
return;
}
The math error is in the following:
p = acos(i / (sqrt(x * k)));
Change it to:
p = acos((ux*vx + uy*vy) / (sqrt(ux*ux + uy*uy) * sqrt(vx*vx + vy*vy)));
That's just the dot product divided by the two lengths.
You equation is wrong.
The correct is:
|U|=√[Ux^2+Uy^2]
|V|=√[Vx^2+Vy^2]
U*V=(Ux,Uy)(Vx,Vy)=Ux*Uy+Vx*Vy
cos=U*V/[|U|*|V|]
Not %f but %lf specifier have to be used to read data having type double via scanf() family.
Note that %f should be used to print data having type double via printf() family because float will be automatically converted to double for variable number arguments.
Problem: To measure the volume of liquid in the tank, a depth gauge (measuring stick) can be used. It is inserted into an opening at the top and the level of liquid on the gauge can be used to determine the amount of liquid in the tank.
The tank has width w, height h and length len (in m). In the example output shown below, we take w=8, h=4 and len=7. Your programs should work for any values of w, h and len, not just these specific values.
We look at inserting a measuring stick that is already calibrated in units of 10 centimeters. This measuring gauge can be inserted into an opening at the top of the tank and used to measure the depth of the liquid in the tank.
Your task will be to write a C program to produce a table of values showing the volume of liquid in the tank for each of the points on the gauge.
The output of your program (for the example above) should look like:
Depth 10 cm : Volume 1.188814 cubic meters
Depth 20 cm : Volume 3.336448 cubic meters
Depth 30 cm : Volume 5.992683 cubic meters
. . .
Depth 380 cm : Volume 172.547399 cubic meters
Depth 390 cm : Volume 174.657114 cubic meters
Depth 400 cm : Volume 175.743037 cubic meters
Methodology:
If the tank has width W and height H (in centimeters), the focal radii of the cross section are A = W/2 and B = H/2. Then the equation of the ellipse is:
X^2/A^2 + Y^2/B^2 = 1
To find the volume at given depth you should compute the cross-sectional area of the tank for each given depth using a numerical integration
algorithm such as the trapezoidal method.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double WIDTH;
double HEIGHT;
double LENGTH;
//Function: y in terms of x
typedef double (*DFD) (double);
double f (double x)
{
double a = HEIGHT / 2.0;
double b = WIDTH / 2.0;
double y = (b / a) * sqrt (a * a - x * x);
return y;
}
//Integrating the function -> Area
double trapezoidal_int (DFD f ,double a, double b, int n){
double x, dx, sum=0.0;
int i=0;
dx = (b-a)/ n;
sum = (f(a) + f(b))/2;
for (i=1, x = a + dx; i < n; i++, x += dx)
sum += f(x);
return 2.0 * sum * dx;
}
int main ()
{
int h_cm;
printf ("Enter Width of the tank (in m):\n");
scanf ("%lf",&WIDTH);
printf ("Enter Height of the tank (in m):\n");
scanf ("%lf",&HEIGHT);
printf ("Enter Length of the tank(in m):\n");
scanf ("%lf",&LENGTH);
for (h_cm = 0; h_cm <= HEIGHT * 100; h_cm += 10) {
double h = h_cm / 100.0;
double area = trapezoidal_int (&f, HEIGHT / 2 - h, HEIGHT / 2, 100);
double volume = area * LENGTH;
printf ("Depth %d cm: Volume %.6lf cubic metres\n",
h_cm, volume);
}
return 0;
}
There are a few mistakes here and there. First, let's use global variables for the dimensions of the tank so that function f() can use them:
double WIDTH;
double HEIGHT;
double LENGTH;
You function f() had the height and width reversed:
double f (double x)
{
double a = HEIGHT / 2.0;
double b = WIDTH / 2.0;
double y = (b / a) * sqrt (a * a - x * x);
return y;
}
The doubling of the value of the integral, which is necessary to measure both sides of the major axis, should be done in main() and not in trapezoidal_int(). It is bad practice to have a function that does not do what it's name suggests.
The bounds of integration were also wrong:
int main ()
{
int h_cm;
WIDTH = 8.0;
HEIGHT = 4.0;
LENGTH = 7.0;
for (h_cm = 0; h_cm <= HEIGHT * 100; h_cm += 10) {
double h = h_cm / 100.0;
double area = 2.0 * trapezoidal_int (&f, HEIGHT / 2 - h,
HEIGHT / 2, 100);
double volume = area * LENGTH;
printf ("Depth %d cm: Volume %.6lf cubic metres\n",
h_cm, volume);
}
return 0;
}
I am making a calculator using the formula on this link:
http://cereference.com/book/surveying-and-transportation-engineering/simple-curves-or-circular-curves#sthash.qrD1VOm6.08csgYq9.dpbs
and
https://www.easycalculation.com/engineering/civil/highways-horizontal-curve.php
EDITED QUESTION!
So I used the math.h library in order to use the sin, tan, cos, and sec function but the answers are not right based on my formula... So to test, lets say I have an angle of 36 and a radius of 286... so the answer for the tangent (utangent) must be 92.927. and my next question is that how to use the sec function? I commented it because it wont compile... Also with tan,sin and cos.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
double length, angle, radius, tangent, chord, midordinate, external, degree;
double pcurve, pintersection, ptangent;
double ulength, uangle, uradius, utangent, uchord, umidordinate, uexternal;
double pi;
double choice, choice2, given;
pi = 3.14159;
printf("Enter radius: ");
scanf("%lf",&radius);
printf("Enter angle: ");
scanf("%lf",&angle);
utangent = radius * (tan(angle/2));
uchord = 2*radius*(sin(angle/2));
umidordinate = radius - (radius*(cos(angle/2)));
//uexternal = radius * (sec(angle/2)) - radius;
printf("tangent = %lf\n",utangent);
printf("chord = %lf\n",uchord);
printf("ordinate = %lf\n",umidordinate);
//printf("%lf\n",uexternal);
getch();
return 0;
}
If you compile your code with warnings, which you absolutely should, you may see something like:
sintancos.c:15:13: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%f", &angle);
~~ ^~~~~~
%lf
This is fixed by rewriting it to scanf("%lf", &angle); as suggested by the warning message.
I assume you need to recalculate the input from degrees to radians, since you're asking for degrees. And, of course, before outputting it again you need to change it back to degrees.
It's often done in C with macros, but I prefer functions.
double to_degrees(double rad)
{
return rad * 180.0 / M_PI;
}
double to_radians(double deg)
{
return deg * M_PI / 180.0;
}
M_PI is almost always defined in math.h, with higher precision than your pi. You should also move your input and calculation to its own functions, so it is easier to read and test.
sec is not a standard C function, so you have to define it yourself. It'll be something like this:
#include<stdio.h>
#include<math.h>
double to_degrees(double rad)
{
return rad * 180.0 / M_PI;
}
double to_radians(double deg)
{
return deg * M_PI / 180.0;
}
double sec(double z_r)
{
return 1 / cos(z_r);
}
int main(){
double angle, radius, angle_r;
double utangent, uchord, umidordinate, uexternal;
//printf("Enter radius: ");
//scanf("%lf",&radius);
radius = 286;
//printf("Enter angle: ");
//scanf("%lf",&angle);
angle = 36;
angle_r = to_radians(angle);
utangent = radius * (tan(angle_r/2));
uchord = 2*radius*(sin(angle_r/2));
umidordinate = radius - (radius*(cos(angle_r/2)));
uexternal = radius * (sec(angle_r/2)) - radius;
printf("\nResults:\n");
printf("tangent = %lf\n",utangent);
printf("chord = %lf\n",uchord);
printf("ordinate = %lf\n",umidordinate);
printf("external %lf\n",uexternal);
return 0;
}
I need to get the user to input a number into a program and then need to be able to use that number in many other parts functions in the program. Any way to do that?
Here is the code:
#include <stdio.h>
#include <math.h>
#define W 8.
#define H 4.
double ellipse(double);
typedef double (*DfD) (double);
double simpsons_int (DfD, double, double, int);
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}
double ellipse(double y)
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
double simpsons_int(DfD f, double y0, double y1, int n)
{
double y, sum, dy = (y1 - y0)/n;
sum = f(y1) + f(y0);
for(y = y0; y <= y1-dy; y += dy)
sum += 2.0 * f(y+dy) + 4.0 * f(y + dy/2);
return sum * dy / 6.0;
}
but I need H and W to be number that are input by the user not 8 and 4.
You can either pass it as argument of the function, or declare it as global variable. I'd rather use the first, depending on the application.
1) passing as parameter. Your function should be:
double ellipse(double y, double W, double H )
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
And then you declare and scanf W and H within main()
2) Just declare W and H before main();
double W,H;
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
scanf("%lf %lf",&W,&H);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}
Preprocessor directives like #define must be known at compile-time. Think of them as constants: you can set them, but as soon as you run the program they're set in stone.
You should be using your variables to do this; you could possibly define w and h to be global variables, but better practice would be to pass them in as parameters to the ellipse function.