Why is my code not printing answer in float? [duplicate] - c

This question already has answers here:
Why is this simple piece of code not working?
(5 answers)
Closed 5 years ago.
#include <stdio.h>
float div ( int a,int b, int c, float x );
int main()
{
int a,b,c,x;
a=250;
b=85;
c=25;
x=div (a,b,c,x);
printf("%d ", x);
}
// your code goes here
float div (int a,int b, int c, float x)
{
x=((a-b)/c);
return(x);
}

Because x is declared as int instead of float.
Your code:
int x;
...
printf("%d ", x);
What you need instead:
float x;
...
printf("%f ", x);
And your div function is wrong too:
float div (int a,int b, int c, float x)
{
x=((a-b)/c); // << this will perform an integer division
return(x); // and therefore x will be truncated, even if it is a float
}
You need this:
float div (int a,int b, int c, float x)
{
x = ( ((float)(a-b) ) / c);
return x;
}
The (float) before (a-b) is called a cast and will ensure that (a-b) will be treaded as a float number during the division by c.
BTW the function can be simplified, you don't need the x parameter:
float div (int a,int b, int c)
{
float x = ( ((float)(a-b) ) / c);
return x;
}
or even simpler:
float div (int a,int b, int c)
{
return ((float)(a-b) ) / c;
}

Look closely at the statement
x=((a-b)/c);
Note the operands, they are all integers, so the integer division is performed and then, the result is converted to floating type upon assignment. You don't want that.
In case you want a floating point division, you need to make sure one of the operands are of type float/ double.
You can use a cast, like
x=(((float)a-b)/c);
to force floating point arithmetic.
That said, in the main(), you MUST use proper conversion specifier for float, %f.

prints out an integer:
printf("%d ", x);
prints out an floating point number:
printf("%f", x);
also x must be from type float

Related

How to use correctly struct type in c? [duplicate]

How do you divide two integers and get a double or float answer in C?
You need to cast one or the other to a float or double.
int x = 1;
int y = 3;
// Before
x / y; // (0!)
// After
((double)x) / y; // (0.33333...)
x / ((double)y); // (0.33333...)
Of course, make sure that you are store the result of the division in a double or float! It doesn't do you any good if you store the result in another int.
Regarding #Chad's comment ("[tailsPerField setIntValue:tailsPer]"):
Don't pass a double or float to setIntValue when you have setDoubleValue, etc. available. That's probably the same issue as I mentioned in the comment, where you aren't using an explicit cast, and you're getting an invalid value because a double is being read as an int.
For example, on my system, the file:
#include <stdio.h>
int main()
{
double x = 3.14;
printf("%d", x);
return 0;
}
outputs:
1374389535
because the double was attempted to be read as an int.
Use type-casting.
For example,
main()
{
float a;
int b = 2, c = 3;
a = (float) b / (float) c; // This is type-casting
printf("%f", a);
}

numerical integration using a pointer to a function always return 0

I am trying to use the function that I was given by my professor to calculate the integral of a polynomial function (polynomial such as: ax^2+bx+c). the function is:
double numbericalIntegration(double a ,double b ,double(*func)(double)){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x);
}
return sum*delta;
}
I changed a lot in order to integrate a polynomial function. but I was get the answer 0. why is that? and I'd appreciate if anybody tried to correct my work. my code is:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,3))/3 +(b*pow(x,2))/2 + (c*x);
}
double numbericalIntegration(double a ,double b ,double(*func)(double,double,double,double), double firstNum, double secondNum, double thirdNum){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x, firstNum, secondNum, thirdNum);
}
return sum*delta;
}
int main()
{
double (*func)(double,double,double,double);
func = integralPoly;
double sum = numbericalIntegration(2,4,func,1,1,4);
printf("sum = %d",sum);
return 0;
}
You need to change two things. First your polynomial function doesn't make any sense. You said it needs to be in the form of ax^2+bx+c but in your code polynomial is (ax^3)/3+(bx^2)/2+c*x. Your function should be:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,2)) +(b*x) + c;
}
Also you need to change your printf. %d is integer type specifier and you need double, so you need to use %f for example:
printf("sum = %f",sum);
Now the output of your program is:
sum = 32.666016
which is correct for your parameters.

Mean coming out to be zero in C

I made the following program to calculate sum,mean and standard deviation of 5 numbers.The Sum is correct but mean is coming out to be zero ALWAYS and hence the SD is also wrong.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int sum(int, int, int, int, int);
float SD(int, int , int, int,int,int);
float mean(int);
int main()
{
int a,b,c,d,e,m;
printf("Enter 5 integers succesively by pressing enter after entering a number.\n");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
m=mean(sum(a,b,c,d,e));
printf("\nThe sum is %d , mean is %f and standard deviation is %f",sum(a,b,c,d,e),m,SD(a,b,c,d,e,m));
return 0;
}
int sum(int v, int w, int x, int y, int z)
{
return(v+w+x+y+z);
}
float SD(int v, int w, int x, int y, int z,int mm)
{
float k;
k=sqrt(((mm-v)*(mm-v) + (mm-w)*(mm-w) + (mm-x)*(mm-x) + (mm-y)*(mm-y) + (mm-z)*(mm-z))/5);
return k;
}
float mean(int v)
{
return (v/5);
}
when you use division make use float data type. in mean() and SD() function you do division by 5 and both operands are int so change one of those to float. otherwise result will be truncated to be a int.
You can change /5 to /5.0 or you can use a float type cast.
The mean you are getting as 0 since you defined m as integer int and using %f to print it.
int a,b,c,d,e,m;
printf("\nThe sum is %d , mean is %f and standard deviation is %f",sum(a,b,c,d,e),m,SD(a,b,c,d,e,m));
m is mean so make it a float type variable.
int a,b,c,d,e;
float m;
Change v/5 to v/5. .
Since v and 5 are both int, then v/5 is an integer division. You want to do a floating-point division instead, so you have to make one of the operands be floating, and 5. is a constant of type double.
Same thing applies in your SD function.

-1.#IND00 output for certain input values

I'm trying to write a code that will take x as input and give cos(x) as output, using maclaurin's series.I'm using a while loop until the difference of two consecutive results is less then 0.001. I'm using double type to accomodate larger values.
the code works when x is in range [-2,2], but if x is greater or less than this range the ouput is -1.#IND00. Why is it happening? is the output value out of range ? how can i fix this ??
my code is :
#include <stdio.h>
double abs(double a);
double power(double p, int q);
int fact(int a);
int main()
{
int i=1,j=2*i;
double x,s=1.0,p,l=0.001;
printf("Enter x: ");
scanf("%lf", &x);
p = s+ power(-1,i) * power(x,j) / fact(j);
while (abs(p-s)>l){
i++; j=2*i;
s=p;
p = s+ power(-1,i) * power(x,j) / fact(j);
}
printf("cos(%f) = %f", x,p);
return 0;
}
double abs(double a)
{
if (a>=0) return a;
else return (-a);
}
double power(double p, int q)
{
int i;
double a=1.0;
for (i=0; i<q; i++){
a=a*p;
}
return a;
}
int fact(int a)
{
int i,p=1;
if (a==0 || a==1) return 1;
else
while (a!=1){
p=p*a;
a--;
}
return p;
}
update your scanf function to
scanf("%lf", &x);
Also you need to check pow and fact, these functions could overflow. Especially, fact which only use int.
As a larger |x| is use, more terms are needed and fact() overflows and strange results follow. Use double.
// int fact(int a)
double myfact(double p, int q) {
int i;
double a = 1.0;
for (i=0; i<q; i++){
a=a*p;
}
return a;
}
Eventually with values somewhere larger |x| > 30, other limitations kick in using this method. The limitation is due to precision and not range. For large values a significantly different algorithm should be used.
Potential conflict between int abs(int j) in <stdlib.h>. The prototyped may be found via stdio.h and conflicts with OP double abs(double a). In any case, abs() is a standard library function and OP should avoid that function name. Also recommend renaming power().
// double abs(double a)
double myabs(double a)

How to get fractions in an integer division?

How do you divide two integers and get a double or float answer in C?
You need to cast one or the other to a float or double.
int x = 1;
int y = 3;
// Before
x / y; // (0!)
// After
((double)x) / y; // (0.33333...)
x / ((double)y); // (0.33333...)
Of course, make sure that you are store the result of the division in a double or float! It doesn't do you any good if you store the result in another int.
Regarding #Chad's comment ("[tailsPerField setIntValue:tailsPer]"):
Don't pass a double or float to setIntValue when you have setDoubleValue, etc. available. That's probably the same issue as I mentioned in the comment, where you aren't using an explicit cast, and you're getting an invalid value because a double is being read as an int.
For example, on my system, the file:
#include <stdio.h>
int main()
{
double x = 3.14;
printf("%d", x);
return 0;
}
outputs:
1374389535
because the double was attempted to be read as an int.
Use type-casting.
For example,
main()
{
float a;
int b = 2, c = 3;
a = (float) b / (float) c; // This is type-casting
printf("%f", a);
}

Resources