C programming error float [duplicate] - c

This question already has answers here:
C: converting Farenheit to Celsius
(3 answers)
Closed 8 years ago.
This is the code. S (arithmetic mean) is done right, I have valid result. But for other calculations I'm getting this error: Floating point exception (core dumped).
Does anyone know what is wrong with my code?
#include <stdio.h>
#include <math.h>
main () {
int a,b,c;
double s,h,g,k;
printf ("unesite 3 cela broja\n");
scanf ("%d%d%d", &a, &b, &c);
s=(a+b+c)/3;
printf ("aritmeticka s.v. je: %.2lf\n", s);
/* this should be formula for medium value of harmonic number */
h=3/((1/a)+(1/b)+(1/c));
printf ("harmonijska s.v. je: %.2lf\n", h);
/* this should be http://upload.wikimedia.org/math/a/f/f/aff7a590d055d563ceea52fd66fe7ee2.png */
k=sqrt ((pow(a,2)+pow(b,2)+pow(c,2))/3);
printf ("kvadratna s.v. je: %.2lf\n", k);
/* and this http://upload.wikimedia.org/math/e/3/4/e348daea2f4f2bb60f5cb40706fcbad4.png */
g=pow(a*b*c,1/3);
printf ("geometrijska s.v. je: %.2lf\n", g);
return 0;
}

Almost all your divisions are integer divisions. And if you divide an integer with a larger value then the result will be truncated to zero. Division by zero is not good, and will abort your program.
Change those divisions to floating point divisions.

Related

C++ main function and program [duplicate]

This question already has answers here:
Reading in double values with scanf in c
(7 answers)
Closed 5 years ago.
I am trying to get this code to run, i have tried these 2 ways but to no avail. The aim is to receive an input using scanf and print out the statement using printf. Error message i get is the Compiler just hangs.
Also, i am not to use any math functions such as square root or power
Test Cases and Expected answers
Input1: 1.41421356237
Output1: Area of the circle is 3.141590
Input2: 5.65
Output2: Area of the circle is 50.143703
Trial 1
#include <stdio.h>
double function(double a){
double area;
area = 3.14159*((a/2)*(a/2)*2);
return area;
}
int main(void){
double a;
scanf("%f",a);
double result = function(a);
printf("Area of the circle is %f\n",result);
return 0;
}
Trial 2
#include <stdio.h>
int main(void){
scanf("%f",a);
area = 3.14159*((a/2)*(a/2)*2);
printf("Area of the circle is %f\n",area);
return 0;
}
would appreciate any help, not sure why the function is not working. Thank you for your time.
In your first trial you need to change your scanf("%f",a); line to:
scanf("%lf",&a);
because correct type specifier for double is %lf which stands for "long float". Also you need to send &a as argument because scanf expects an address and not a value. Same thing with second trial.

Squaring a number [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
This program is supposed to input a number and its square value, then tell me if right or wrong. I have some problems but I can't figure out what they are.
#include <stdio.h>
#include <math.h>
int main()
{
float P;
float q;
float r;
printf("Enter the value of p\n");
scanf("%f",p);
q= p*p;
printf("Enter the square value of %f \n",p);
scanf("%f",r);
if (r = q){
printf("You are right\n");
}
else{
printf("you are wrong\n");
}
return 0;
}
so tell me my mistakes
Please compile the program with flags -Werror -Wall -Wextra although the first mistake is always a compilation error (typo): replace float P; with float p; because C is case-sensitive.
Then you need to pass the address of a variable to scanf, these two lines
scanf("%f",r);
...
scanf("%f",p);
should be
scanf("%f",&r);
...
scanf("%f",&p);
Lastly, there is a syntax error where you test for equality with
if (r = q)
but this changes r and tests if it is non-0. With an integer type you should use
if (r == q)
but with floating point types, equality tests don't work well, please see why in this question.

Trying to understand this printf formatting output [duplicate]

This question already has answers here:
Why does printf print wrong values?
(7 answers)
Closed 7 years ago.
I don't understand why this code produces the below output:
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
float c_to_f(float c);
int main(void) {
for (int c = LOWER; c <= UPPER; c += STEP) {
printf("%3.0f %6.1f\n", c, c_to_f(c));
}
return 0;
}
float c_to_f(float c) {
return c * (9.0/5.0) + 32;
}
with output:
0 5144477247317086170901765440027035767837163293591161256351693248184965237877467107389389528872273154691913581744607058050215827488351921876414407003384176234234181468372580859505320314312544948225387164490993094256968227227818959640206687395851530141696.0
0 5144477248223936133773425621301895611375254354002848388186656757998856300277376171732587490599187934010444965285645525890922748188992798901374640654313592657351174354641522290319226294263523595109393871854132336451448805097328901373303486131449817464832.0
0 5144477248704033172940775129035644940307184915397270987393402145547386862724386852855456999748731052473196874219136479453649941501096792620471234940099754293118994117960373636044235342472865231695044481634617817613232640440010635232001791344413616635904.0
0 5144477249104114038913566385480436047750460383225956486732356635171162331430229087124514924040016984525490131663712274089255935927850120719718396844921555656258843920726083090815076215980649928849753323118355718581385836558912080114250379021883449278464.0
0 5144477249344162558497241139347310712216425663923167786335729328945427612653734427685949678614788543756866086130457750870619532583902117579266693987814636474142753802385508763677580740085320747142578628008598459162277754230252947043599531628365348864000.0
0 5144477249584211078080915893214185376682390944620379085939102022719692893877239768247384433189560102988242040597203227651983129239954114438814991130707717292026663684044934436540085264189991565435403932898841199743169671901593813972948684234847248449536.0
I do understand that c should be a float, just not the output. Any help would be greatly appreciated.
The problem here, as I see it is
printf("%3.0f %6.1f\n", c, c_to_f(c));
c is an int and you're trying to print it's value using %f, which is undefined behavior.
Using an inappropriate type of argument for a format specifier is undefined behavior.
When using printf and floats you have the right token %f. But your using it wrong. To printf out 2 decimal places you would use %.2f, 3 would be %.3f and so on. I think the console is bugging out because you are using %3.0f and %6.1f

Floating point math in c getting random numbers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm doing a exercise from C primer plus that involves working with floats, I can't get the result to also be a float. I got it to read in the input fine (as far as I can tell) but the problem must be in the formula line. Can anyone tell me what I'm doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float inp1, inp2;
float result;
result = (inp1 - inp2) / (inp1 * inp2); /* formula */
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
printf("(%.3f - %.3f) / (%.3f * %.3f)\n", inp1, inp2, inp1, inp2);
printf("%f\n", result);
}
The output is giving some random number (I don't know what it's called), for example with an input of 1.255 and 1.023 I get an output of 2656044210243861500000000000000000000.000000 or something similar. The second to last printf displays the input correctly.
What am I doing wrong?
I've tried this, but I don't really get how cast operators work.
result = ((float)inp1 - (float)inp2) / ((float)inp1 * (float)inp2); //formula
and
result = (float)(inp1 - inp2) / (inp1 * inp2);
result = (inp1 - inp2) / (inp1 * inp2); /* formula */
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
Well, what about putting scanf call before result = ...; assignment statement?
result = (inp1 - inp2) / (inp1 * inp2); /* formula */
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
That's not how things work in a language like C; you don't define a formula for result that's automatically computed when you read your inputs. Rather, you first read your inputs, and then you compute the value and assign it to result:
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
result = (inp1 - inp2) / (inp1 * inp2); /* computation and assignment */
C is an imperative language, meaning you have to explicitly list out the instructions for it to follow, including those for any mathematical computations.
The analogy of math formulas in programming languages is functions (without side effects). That way, you can define formulas before the code itself. For instance:
#include <stdio.h>
#include <stdlib.h>
float formula(float inp1, float inp2)
{
return (inp1 - inp2) / (inp1 * inp2);
}
int main(void)
{
float inp1, inp2;
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
printf("(%.3f - %.3f) / (%.3f * %.3f)\n", inp1, inp2, inp1, inp2);
printf("%f\n", formula(inp1, inp2));
return 0;
}

Not understanding the output when Converting Fahrenheit to Celsius [duplicate]

This question already has answers here:
C Temperature Conversion Program Keeps Outputting 0 For Fahrenheit to Celsius [duplicate]
(2 answers)
Closed 8 years ago.
I have following C program, that takes a temperature in Fahrenheit and convert in to Celsius value. But, every time I input a value it always gives me0.00 as output. I'm not understanding where is the problem.
#include <stdio.h>
int main()
{
float cel_out, fht_in;
printf("Enter a temperature in farenheit: ");
scanf("%f", &fht_in);
cel_out = (fht_in - 32) * (5/9);
printf("Temperature in celcious: %.2f", cel_out);
return 0;
}
In this euqation
cel_out = (fht_in - 32) * (5/9);
When you use 5/9 it is integer data type which results 0. so
cel_out = (fht_in - 32) * 0;
Results 0 only!
You need to use-
cel_out = (fht_in - 32) * (5.0f/9.0f);
here it is treated as float values and gives the actual result of 5/9.

Resources