Why is float in c not working the way it should? - c

I was trying to do the codechef question - https://www.codechef.com/problems/FLOW009
Here, my code (submitted successfully )is this -
int main(void) {
int testCases;
scanf ("%d\n", &testCases);
while (testCases--) {
float q,p;
scanf ("%f%f", &q,&p);
if (q >= 1000){
printf("%.6f\n",q*p - (q*p*0.1));
}
else{
printf("%.6f\n", q*p);
}
}
return 0;
}
This was submitted successfully... But however when i tried this code -
int main(void) {
int testCases;
scanf ("%d\n", &testCases);
while (testCases--) {
float q,p;
scanf ("%f%f", &q,&p);
if (q >= 1000){
float a = q*p - (q*p*0.1);
printf("%.6f\n",a);
}
else{
printf("%.6f\n", q*p);
}
}
return 0;
}
It said wrong answer. In my compiler the results are same for all the test cases. What is happening. The first code- i am just printing the value.
In the second - i am using a variable to store the value.
PS - i tried typecasting the value also but with no result.

Converting a value from double to float and back does not guarantee the same result.
In
printf("%.6f\n", q*p - (q*p*0.1));
the values are all converted to type double and the calculation is done in double. The result, of type double, is sent directly to printf.
In the other case with the float variable, the values are converted to double, the calculation is done in double then converted to float for the assignment. That value is converted to double before being passed on to printf.
Always use double for floating point variables.

Related

Function to evaluate exponent

I wrote the following code in c to evaluate exponent of a number without using the math library
#include <stdio.h>
float powr(float,int);
int main(){
float a;
int b;
printf("Enter base and exponent a^b: ");
scanf("%.2f %d",&a,&b);
float p=powr(a,b);
printf("%.2f",p);
return 0;
}
float powr(float x,int y){
float r=1;
for(int i=1;i<=y;i++){
r=r*x;
}
return(r);
}
but no matter what base and exponent I input, the ouput always comes out to be 1.00. I can't find any mistake in this program and I tried running the powr function algorithm inside main() in a seperate program and it works.
In scanf() it onlys accepts field-width format, but no precision, see here.
You can just input the value, like this:
scanf("%f %d",&a,&b);
Also, you should always check the return value of scanf(). Something like this:
numOfItems = scanf("%.2f %d",&a,&b);
if(numOfItems != 2) // uh-oh
{
printf("Error while input!");
}

Double Exponent from user input

So my question relates to double, I am trying to get an input from the user in decimal point for any value and its exponent also in decimal point to display the result after calculation in another function where the variables will pass values as double and I have used double the output as well but the end result is 1.00000 even though I have used the output specifier as %lf%.
#include <stdio.h>
double pwra (double, double);
int main()
{
double number, power, xx;
printf("Enter Number: ");
scanf("%lf", &number);
printf("Enter Number: ");
scanf("%lf", &power);
xx=pwra (number,power);
printf("Result: %lf", xx);
return 0;
}
double pwra (double num, double pwr)
{
int count;
int result = 1;
for(count=1;count<=pwr;count++)
{
result = result*num;
}
return result;
}
You have used the wrong type for result in the pwrs() function.
Change:
int result = 1;
to:
double result = 1.0;
Note that this type of simple mistake is easily identified if you learn to use your debugger. Further reading: How to debug small programs.
Also note that pwr should be an int, not a double, since your function only works with integer exponents.

C - long double and printf issue

I am new C programmer and I am working on a school project where I have to approximate the value or pi. My professor stated that we have to declare all integer terms using long double. The console shows me asking the user for the number of terms to approximate pi given a function. I enter 1 for the number of terms however the code returns -0.00000000 instead of 4.00000000.
#include <stdio.h>
#include <math.h>
long double approx1(int terms)
{
long double pi = 0;
long double num = 4;
long double denom = 1;
int i;
for(i=1; i <= terms; i++)
{
if(i%2 != 0)
{
pi=pi+(num/denom);
}
else
{
pi=pi-(num/denom);
}
denom = denom + 2;
}
printf("%.8Lf\n", pi);
}
int main()
{
int terms;
long double pie;
printf("input number of terms, input 0 to cancel\n");
scanf("%d", &terms);
while(terms != 0)
{
if(terms > 0)
{
pie = approx1(terms);
printf("%.8Lf\n", pie);
printf("GG mate\n");
break;
}
else
{
printf("Incorrect input, please enter a correct input\n");
scanf("%d", &terms);
}
}
}
I haven't had any success in getting it to work( it works with float though). What am I doing wrong? (I am using Code Blocks with the included compiler btw.)
You forgot to add a return statement in your approx1() function. Withoout that return statement, if you make use of the returned value, it invokes undefined behavior.
Quoting C11 standard, chapter ยง6.9.1
If the } that terminates a function is reached, and the value of the function call is used by
the caller, the behavior is undefined.

How i can handle garbage?

I am novice to C. I am trying to write a program to calculate the Hypotenuse, but when I run it I get garbage values and I don't know how to handle it.
double Hypotenuse(double base, double perpendicular)
{
double hyp = sqrt((base*base) + (perpendicular*perpendicular));
return hyp;
}
int _tmain(void)
{
double b, p;
printf("Enter the lenght of base\n");
scanf_s("%f",&b);
printf("Enter the lenght of perpendicular\n");
scanf_s("%f",&p);
printf("The hypotenuse of the triangle is %3.3f",Hypotenuse(b,p));
return 0;
}
problem is in your scanf statements.
As a thumb rule you should always use "%lf" for doubles.
This works perfectly:
double Hypotenuse(double base, double perpendicular)
{
double hyp = sqrt((base*base) + (perpendicular*perpendicular));
return hyp;
}
int _tmain(void)
{
double b, p;
printf("Enter the lenght of base\n");
scanf_s("%lf",&b);
printf("Enter the lenght of perpendicular\n");
scanf_s("%lf",&p);
printf("The hypotenuse of the triangle is %3.3lf",Hypotenuse(b,p));
return 0;
}
So, the best way to see what is going on in your code or where u get garbage, after all scanf print the value
ex:
double a;
scanf("%f",&a);
printf("a=%f",a); //OR
printf("a=%3.3f",a);
and you can easy see if the problem is in your scanf or somewhere else..

How do you return a value from a called function to the main?

I use the return command then try to print the value from the main. It returns a value of zero (0).
This program is about temperature conversion from Celsius to Fahrenheit.
Also how can you use a rounding function to round the answer to an integer so it is not a floating point number with decimals.
#include <stdio.h>
int Cel_To_Fah(int a, int b); // function declaration
int main (void)
{
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
Cel_To_Fah(a,b); // function call
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(a,b)
{
b=1.8*a+32;
return b;
} // Cel_To_Fah
You just have to use the assignment operator:
b = Cel_To_Fah(a);
Your program has a lot of problems, though, including your Cel_To_Fah function not having a correct signature. You probably want something like:
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
You should probably get a good beginner C book.
No need of second argument to function(b).
You can do this by...
#include<stdio.h>
int Cel_To_Fah(int a); // function declaration, as it returns a values;
int main (void)
{
int a; int b;
printf(" Enter temperatrure: ");
scanf("%d", &a);
b = Cel_To_Fah(a); /* the returned value is stored into b, and as b is an integer so it is automatically rounded. no fraction point value can be stored into an integer*/
printf("The temperature is: %d\n", b);
return 0;
} // main
int Cel_To_Fah(int a)
{
return 1.8 * a + 32;
}
there are several issues. First you need to use float, not int, so that you can have values with a decimal point. otherwise your calculations will come out wrong. Also use 32.0 instead of 32 for the same reason.
Second, you need to understand that the a and b in your function are NOT the same as the a and b in main. They have the same name but are not in the same "scope". So changing the one in your function doesn't affect the one in main. That's why in main you have to say b=Cel... so that b in main will get the returned value.
finally, in c, you're supposed to put your functions above/before main. Otherwise it's technically not defined "yet", though some modern compilers will fix that for you. Read about function prototypes.
Since your function Cel_To_Fah(a,b); is returning a value (int type), you must have to assign it to a variable of its return type (int type).
int a;
int b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b = Cel_To_Fah(a); // function call
printf("The temperature is: %d\n", b);
and your function should be
int Cel_To_Fah(a)
{
int b = 1.8*a+32;
return b;
} // Cel_To_Fah
And do not forget to change your function prototype to
int Cel_To_Fah(int a);
I saw two issues in your code. Firstly, it is variable type. I assume that you want Celsius as integer; but Fahrenheit = 1.8*Celsius+32 should be float. Therefore b should be float.
Secondly, you should not return a value from a function via its input parameters (unless you learn pointer or call by ref). I rewrite your code as following:
include<stdio.h>
float Cel_To_Fah(int a); // function declaration
int main (void)
{
int a;
float b;
printf(" Enter temperatrure: "); scanf("%d", &a);
b=Cel_To_Fah(a); // function call
printf("The temperature is: %.2f\n", b); //showing 2 decimal places
return 0;
} // main
float Cel_To_Fah(int a)
{
float b;
b=1.8*(float)a+32; //cast a from int to float
return b;
} // Cel_To_Fah

Resources