int num1, num2;
double average;
average=(double)(num1+num2)/2;
printf("average: %d", average);
My test printf shows average as: 0
This is maybe too easy, but I can't see it. My inputs are both "int" and the average is "double" but somehow it is not calculating right ?
You're using the wrong format specifier to printf.
The %d format specifier expects an int argument, but you're passing a double. Using the wrong format specifier invokes undefined behavior.
To print a double, use %f.
printf("average: %f\n", average);
No need to modify the statement average=(double)(num1+num2)/2; to get expected result inside printf use %f instead of %d
1st (num1+num2) is performed, result of this is of integral type. lets say 15. Next when you do (double)15/2 result is of floating type which is 7.500000.
from previous step average = (double)7.500000 average holds 7.500000 but since you printed in %d you are getting 0 as its undefined behavior. instead use %f
Here is the working one
int main() {
int num1 = 7, num2 = 8;
double average;
average = (double)(num1 + num2)/2;
printf("average: %f\n", average);
return 0;
}
int num1, num2;
double average;
average=(num1+num2)/2.; // Using a decimal point forces 2 to be a double.
printf("average: %f\n", average); // Use the correct specifier for double: %f
You should use printf("average= %f",avearge); instead of using "%d" to print the average value.I think it will solve your issues...
Integer division yields an integer result: 1/2 == 0, 5/3 == 1, etc. If you want a floating point result, at least one of the operands must be a floating-point type: 1/2.0f == 0.5f, 5/3.0 == 1.6667, etc.
So, you'll want to divide your sum by 2.0, not 2:
average = (num1 + num2)/2.0;
Secondly, you need to use the %f format specifier for floating-point output:
printf( "average: %f\n", average );
If you want a floating point result, at least one of the operands must be a floating-point type
This is my solution:
average = (num1 + num2)/(double)2;
printf ("Value of 1/2 is: %.4f\n", (double)(1/2));
// Value of 1/2 is: 0.0000
printf ("Value of 1/2 is: %.4f\n", (1/(double)2));
// Value of 1/2 is: 0.5000
Related
# include<stdio.h>
int main() {
printf("%d \n", 7%9); //integer result
printf("%f", 7%9); //float result
return 0;
}
Above is the code I used to calculate the value of 7%9. I wanted to see both the integral and the float result. But, the values appearing here are different.
printf("%d \n", 7%9); //integer result
printf("%f", 7%9); //float result
Although the comments and the format specifiers indicate your intent, the compiler sees the integer calculation and passes an integer value to printf().
In the 2nd line, you've effectively written:
printf( "%f", 7 );
That is obviously incorrect.
If you wanted to see lots of zeros, you could cast the value to match what you've written as the format specifier:
printf( "%f", (float)( 7 % 9 ) );
Here is my code, when using print statements it appeared to be an infinite loop:
some potential issues i can troubleshoot are integer division, and perhaps figuring out if the algorithm terminates, and if it always has the correct output, perhaps there is some idiosyncrasy of the C language that I do not understand causing this issue?
From my understanding as the sum tends to negative infinity it will cause the break statement to be triggered ending the algorithm once it reaches an approximation of epsilon precision.
#include <stdio.h>
int main()
{
double pi, sum, epsilon;
long long i=1;
long long max = 2147483647;
printf("Pleas enter the desired accuracy epsilon : ");
scanf("%f", &epsilon);
while (i<max){
if (i%2 == 0){
sum = -4.0/(2.0*i-1);
}else if (i%2 ==1){
sum = (4.0/(2.0*i-1));
}
if (sum < epsilon){
break;
}
pi += sum;
}
printf("The value of pi approximated to epsion : %f is %f\n", epsilon, pi);
return 0;
}
You are not increment the value of i in your while loop. As a result its value always remains 1.
Increment i after your processing is done before the closing } of the while loop.
Initialize the values of pi, sum, epsilon so that you do not run into undefined behavior when you try to read their values for the first time.
Also, with proper options enabled (-Wall with GCC) the compiler will warn you, that you are trying to read a float into a double.
warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double *' [-Wformat=]
scanf("%f", &epsilon);
~^ ~~~~~~~~
So you have to use the correct conversion specifier: %lf instead of %f in your scanf statement.
If you are not very particular about the number 2147483647 and just want a large value, you can also use ULLONG_MAX (from limits.h) to initialize max instead of hard-coding the value.
You need to increment i value in while loop.
Your looping condition while(i < max) is always satisfied (infinite loop). So the value of i or max should be changed inside the loop to ever end the loop.
Your code has undefined behavior:
scanf("%f", &epsilon);
scanf %f takes a float *, not double *. The correct format for double * is %lf.
Your loop condition while (i<max) is equivalent to while (1) as neither i nor max change their value inside the loop.
This also means sum is always 4:
sum = (4.0/(2.0*i-1));
// 4 / (2 * 1 - 1) = 4 / (2 - 1) = 4 / 1 = 4
pi += sum;
also has undefined behavior: pi is uninitialized.
This question already has answers here:
return value of pow() gets rounded down if assigned to an integer
(4 answers)
Closed 5 years ago.
I'm new with C programming, and I've written this code as I've been asked to do something using mainly printf() and scanf(). I know there could be better ways to handle this, which I'll need to learn soon, but anyways, for now this is what I have:
int add1, add2, exponent, exponent_result, multiplier, parenthese, product, sub, total;
printf("Let's try a slightly more complex calculation, in which we'll use an exponent.\n\n");
printf("Type 5 whole numbers or integers.\n\n");
scanf("%i %i %i %i %i", &add1, &add2, &exponent, &multiplier, &sub);
printf("Out of the numbers you typed, we're going to make this operation: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
exponent_result = pow(add2, exponent);
parenthese = add1 + exponent_result;
product = parenthese * multiplier;
total = (add1 + exponent_result) * multiplier - sub;
printf("Per PEMDAS, the correct order of operation is Parentheses, then Exponents, then Multiplications and Divisions, and finally Additions and Subtractions.\n\n");
printf("Therefore: (%i + %i^%i) * %i - %i\n\n", add1, add2, exponent, multiplier, sub);
printf("...is equal to: (%i + %i) * %i - %i\n\n", add1, exponent_result, multiplier, sub);
printf("...is equal to: %i * %i - %i\n\n", parenthese, multiplier, sub);
printf("...is equal to: %i - %i\n\n", product, sub);
printf("...is equal to: %i", total);
If you run this code, you'll realize that the output for exponent_result, which is calculated using the pow() function, always has 1 subtracted from it. For instance, if exponent_result is supposed to be the result of 5^3, the result for that will be 124 instead of 125.
What am I doing wrong?
Fyi, I have this at the beginning of my file.
#include <stdio.h>
#include <math.h>
pow is evaluated in floating point arithmetic, and is probably implemented as pow(x, y) = exp(y * log(x)).
This can cause the result to "go off": you probably get a value just shy of 125, which is truncated to 124 when the double return type from pow is converted back to an int.
The simplest remedy is to build your own pow function for integral arguments. See The most efficient way to implement an integer based power function pow(int, int)
I'm currently learning the C language and I'm having trouble in the double multiplication topic.
I need to to print the original value and then the 2*value of the double.
double num = 34.39;
printf("Original value = %d, 2x original value = %d", num, num*2);
How do I make it so that the 2x value will be really 2x the original value?
Your multiplication is not the problem.
Your printf format string is. %d is not for floating-point values, but for integers, so you're seeing nonsense resulting from your broken contract with the compiler.
double num = 34.39;
printf("Original value = %lf, 2x original value = %lf", num, num*2);
%d - for int
You must use the "%f" for printf
int main (void)
{
int fahrenheit; // fahrenheit stands for fahrenheit
double c; // c stands for celsius
printf("Enter your fahrenheit, we'll covnvert it into celsius! ");
scanf("%f", &fahrenheit);
c = 5/9 * (fahrenheit - 32);
printf("Here is your %f in celsius!.\n");
return (0);
}
I've followed the code through break points and when it takes in my input the calculations are off, but the formula is correct. Some sort of logic error I can't put my finger on. Please help!
The scanf call uses the wrong format string. You are reading an int so you need it to be:
scanf("%d", &fahrenheit);
The expression 5/9 is evaluated using integer division. In fact the compiler can work it out at compile time. That expression evaluates to 0.
You need to perform floating point division. For instance:
5.0/9
Or:
5/9.0
Or
5.0/9.0
You just need at least one operand to be a floating point value.
Putting this into your expression, you can write:
c = 5.0/9.0 * (fahrenheit - 32);
and obtain the answer that you expect.
Your printf statement is wrong too. You should enable warnings and let the compiler tell you that. You meant to write:
printf("Here is your %f in celsius!.\n", c);
Integer math versus floating point math.
i = 5/9 // i is equal to 0
d = 5.0/9.0 // d is equal to whatever 5 divided by 9 would actually be
You also need to actually print the value:
printf("Here is your %f in celsius!.\n", c);
Short answer: Operations on integers return integers even if the variable you store the result on is a double. Divisions between integers are truncated.
You should write this instead:
c = 5.0/9.0 * (fahrenheit - 32.0);
Adding a ".0" (or even just a ".") to your constant makes them floating point values.