Problem with function declaration, K&R problem 1-15 - c

Originally there was a problem to Fahrenheit to Celsius for multiple values of Fahrenheit.
Now in problem 1-15 we have to use a function for this task.
Following is my code:
#include<stdio.h>
float temp_conv(float n);
int main()
{
float lower, upper, step;
int i;
lower= 0;
upper= 300;
step= 20;
for(i=0; i<=(upper-lower)/step; ++i)
printf("%5.1f\t%6.2f\n", i*step, temp_conv(i*step));
}
float temp_conv(float n)
{
float fahr, celsius;
celsius= (5.0/9.0)*(fahr-32.0);
return celsius;
}
And is producing the following output:
0.0 -17.78
20.0 -17.78
40.0 -17.78
60.0 -17.78
80.0 -17.78
100.0 -17.78
120.0 -17.78
140.0 -17.78
160.0 -17.78
180.0 -17.78
200.0 -17.78
220.0 -17.78
240.0 -17.78
260.0 -17.78
280.0 -17.78
300.0 -17.78
I am passing different values in function temp_conv, but then too it is producing the converted value of 0 Fahrenheit. Maybe there is some problem with the function, but then how it is computing the Celsius value for 0 Fahrenheit?
Please help.

float temp_conv(float n) {
float fahr, celsius;
celsius = (5.0/9.0)*(fahr-32.0);
return celsius;
}
You ignore the argument n that you passed to the function and calculate celsius from fahr. fahr is uninitialized (there is no fahr = something), it is having some uninitialized garbage value that just so happens to result in -17.78.
Just calculate it from the argument instead:
float temp_conv(float n) {
float celsius;
celsius = (5.0 / 9.0) * (n - 32.0);
return celsius;
}
or with better naming:
float temp_conv(float fahr) {
float celsius;
celsius = (5.0 / 9.0) * (fahr - 32.0);
return celsius;
}
or really just:
float temp_conv(float fahr) {
return (5.0 / 9.0) * (fahr - 32.0);
}

Related

Function call a for loop

I'm trying to function call the for loop and print AngleValue and SinValue, but whatever I do I can't get it to work. All I need is to print those 2 from the loop into the main function.
int trigof(int x, double y, double AngleValue, double SinValue);
int main(void)
{
printf("%35s","***************************\n");
printf("%35s","****TABLE OF SIN VALUES****\n");
printf("%35s","***************************\n");
printf("%17s", "ANGLE(DEG)");
printf("%18s","SIN(ANGLE)\n");
printf("Press any key to exit");
getchar();
return 0;
}//endprogram
int trigof(int x, double y, double AngleValue, double SinValue)
{
for (x = 0; x < 18; x++)
{
y = AngleValue * PI / 180;
SinValue = sin(y);
printf("%13.1f", AngleValue);
printf("%18.4f\n", SinValue);
AngleValue = AngleValue + 20.0;
}
return AngleValue;
return SinValue;
}
First of all there are lots of mistakes.
You have declared function but you haven't called it in your main. To call function inside main use this statement inside main function trigof(0,0,45,0); 45 is the angle value. Secondly you don't need x, y, signvalue as function parameters. You can declare them locally inside your trigof function. Lastly there should be only one return statement returning only one object.
#define PI (4*atan(1))
static void trigOf(void);
int main(void) {
printf("%35s","***************************\n");
printf("%35s","****TABLE OF SIN VALUES****\n");
printf("%35s","***************************\n");
printf("%17s", "ANGLE(DEG)");
printf("%18s","SIN(ANGLE)\n");
trigOf();
printf("Press any key to exit");
getchar();
return 0;
}//endprogram
static void trigof() {
int x;
double y=0.0,AngleValue=0.0,SinValue=0.0;
for (x = 0; x < 18; x++) {
y = AngleValue * PI / 180;
SinValue = sin(y);
printf("%13.1f", AngleValue);
printf("%18.4f\n", SinValue);
AngleValue = AngleValue + 20.0;
}
}
For starters...
You have to define global variables.
#define PI 3.14159265358979323846
Your function prototype is incorrect, it requires no parameters in this scenario.
//this is wrong
int trigof(int x, double y, double AngleValue, double SinValue);
//this is right
int trigof();
You never call the function trigof(); in main therefore you get no results.
printf("%35s","***************************\n");
printf("%35s","****TABLE OF SIN VALUES****\n");
printf("%35s","***************************\n");
printf("%17s", "ANGLE(DEG)");
printf("%18s","SIN(ANGLE)\n");
//you should have function call here
trigof();
You simply need to define your local variables in the function.
example:
int trigof()
{
//these are local variables
int x;
double y, AngleValue, SinValue;
for (x = 0; x < 18; x++)
{
y = AngleValue * PI / 180;
SinValue = sin(y);
printf("%13.1f", AngleValue);
printf("%18.4f\n", SinValue);
AngleValue = AngleValue + 20.0;
}
return AngleValue;
return SinValue;
}
If you want your output to resemble this:
***************************
****TABLE OF SIN VALUES****
***************************
ANGLE(DEG) SIN(ANGLE)
0.0 0.0000
20.0 0.3419
40.0 0.6425
60.0 0.8658
80.0 0.9847
100.0 0.9850
120.0 0.8666
140.0 0.6437
160.0 0.3434
180.0 0.0016
200.0 -0.3404
220.0 -0.6413
240.0 -0.8650
260.0 -0.9844
280.0 -0.9852
300.0 -0.8673
320.0 -0.6450
340.0 -0.3448

Sides of triangle based on coordinates in C

So this code should calculate the size of a and b side and Height of triangle and I am too dumb to realize what is wrong, can someone help me locate misstake? Thanks for help.
#include <stdio.h>
#include <math.h>
float distance(float, float, float, float);
int main()
{
float a, b, c, d;
float aX, aY, alfa;
float bX, bY, beta;
scanf("%f %f %f" , &aX, &aY, &alfa);
scanf("%f %f %f", &bX, &bY, &beta);
distance(aX, aY, bX, bY);
float gamma = 180 - alfa - beta;
c = distance(aX, aY, bX, bY);
b = ((c/sin(gamma)) * sin(beta));
a = ((c/sin(gamma)) * sin(alfa));
d = (sin(alfa))*c;
printf("Side a:%.2f\n Side b:%.2f\n Side c:%.2f Height:%.2f\n", a, b, c, d);
return 0;
}
float distance(float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
return sqrt(dx*dx + dy*dy);
}
The problem is that you use degrees instead of radians. Trigonometric functions have to be applied to values measured in radians.
Thank you. I am just a student so I do not know how these functions work at all. But I could read it on the website where I find it ... I am soo dumb ...

Fahrenheit to Celsius table C

I am working on a code that displays a conversion table from Fahrenheit to Celsius.How can I change it so that it will start from negative value and go towards positive something like this
Fahrenhite Celcius
-10 -23.3333
-20 -28.8889
..
..
..
..
100 37.7778
my code:
#include <stdio.h>
int main()
{
float fahrenheit, celsius;
int lower, upper, step;
lower = 10;
upper = 100;
step = 10;
printf("F C\n\n");
fahrenheit = lower;
while(fahrenheit <= upper)
{
celsius = (5.0 / 9.0) * (fahrenheit - 32.0);
printf("%3.0f %6.1f\n", fahrenheit, celsius);
fahrenheit = fahrenheit + step;
}
return 0;
}
Firstly you must learn how to work this code it is so simply an clear.
Your code works for lower=10 and upper=100 you can change these values what are you want you can reach table. If you want 4 digits after the conviction
printf("%3.0f %6.4f\n", fahrenheit, celsius);
you must chage %6.1f like above
-10 is greater than -100. So,
You can put lower = -100.
Then your answer will come as expected.
F C
-100 -73.3
-90 -67.8
-80 -62.2
-70 -56.7
-60 -51.1
-50 -45.6
-40 -40.0
-30 -34.4
-20 -28.9
-10 -23.3
0 -17.8
10 -12.2
20 -6.7
30 -1.1
40 4.4
50 10.0
60 15.6
70 21.1
80 26.7
90 32.2
100 37.8

Value of tan(90) in c?

the value it give is 557135813.94455. does the value will remain same every time?? why its not showing infinity??
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
double param, result;
param = 90.0;
result = tan ( param * PI / 180.0 );
printf ("The tangent of %f degrees is %f.\n", param, result );
return 0;
}
You are not passing the value of Pi/2, you are passing 90.0 * 3.14159265 / 180.0, an approximation.
Code is not asking for the tangent of 90°, but the tangent of a number, in radians, close to 90°. The conversion to radians is not exact since π/2 radians is not representable exactly as a double.
The solution is to perform degrees range reduction first and then call tan(d2r(x)).
#include <math.h>
static double d2r(double d) {
return (d / 180.0) * ((double) M_PI);
}
double tand(double x /* degrees */) {
if (!isfinite(x)) {
return tan(x);
} else if (x < 0.0) {
return -tand(-x);
}
int quo;
double x45 = remquo(fabs(x), 90.0, &quo);
//printf("%d %f ", quo & 3, x45);
switch (quo % 4) {
case 0:
return tan(d2r(x45));
case 1:
return 1.0 / tan(d2r(- x45));
case 2:
return -tan(d2r(-x45));
case 3:
return -1.0 / tan(d2r(x45));
}
return 0.0;
}
#define PI 3.14159265
int main(void) {
double param, result;
param = 90.0;
result = tan(param * PI / 180.0);
printf("Angle %.*e radian\n", DBL_DECIMAL_DIG - 1, param * PI / 180.0);
printf("Pi/2 = 1.5707963267948966192313216916398...\n");
printf("The tangent of %f degrees is %f.\n", param, result);
int i;
for (i = -360; i <= 360; i += 30) {
printf("The tangent method 1 of %.1f degrees is %.*e\n",
1.0*i, DBL_DECIMAL_DIG - 1, tan(d2r(-i)));
printf("The tangent method 2 of %.1f degrees is %.*e\n",
1.0*i, DBL_DECIMAL_DIG - 1, tand(-i));
}
return 0;
}
OP's output
Angle 1.5707963250000001e+00 radian
Pi/2 = 1.5707963267948966192313216916398...
The tangent of 90.000000 degrees is 557135183.943528.
Better results
The tangent method 1 of -360.0 degrees is -2.4492935982947064e-16
The tangent method 2 of -360.0 degrees is 0.0000000000000000e+00
The tangent method 1 of -330.0 degrees is -5.7735026918962640e-01
The tangent method 2 of -330.0 degrees is -5.7735026918962573e-01
The tangent method 1 of -300.0 degrees is -1.7320508075688770e+00
The tangent method 2 of -300.0 degrees is -1.7320508075688774e+00
The tangent method 1 of -270.0 degrees is 5.4437464510651230e+15
The tangent method 2 of -270.0 degrees is -inf
The tangent method 1 of -240.0 degrees is 1.7320508075688752e+00
The tangent method 2 of -240.0 degrees is 1.7320508075688774e+00
The tangent method 1 of -210.0 degrees is 5.7735026918962540e-01
The tangent method 2 of -210.0 degrees is 5.7735026918962573e-01
The tangent method 1 of -180.0 degrees is -1.2246467991473532e-16
The tangent method 2 of -180.0 degrees is 0.0000000000000000e+00
...
Floating point arithmetic is not an exact arithmetic. You cannot even compare two floating point numbers using ==; e.g. 0.6 / 0.2 - 3 == 0 should be true but on most systems it will be false. Be careful when you perform floating point calculations and expect exact results; this is doomed to fail. Consider every floating point calculation to only return an approximation; albeit a very good one, sometimes even an exact one, just don't rely on it to be exact.

How to use sin, tan, cos, and sec in C program?

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;
}

Resources