I wanted to ask you because when I run my program, I get that the arithmetic mean of 5 and 18 is 11, not 11.5? I put my program in C:
#include <stdio.h>
int main(void) {
int a,b;
a = 5;
b = 18;
printf("La media aritmética de %d i %d es %d\n", a, b, (a+b)/2);
return 0;
}
Thanks
Modify your code like below -
#include <stdio.h>
int main(void) {
float a,b;
a = 5;
b = 18;
printf("La media aritmética de %.f i %.f es %f\n", a, b,(a+b)/2);
return 0;
}
Currently, you are getting 11. Because when this operation (a+b)/2 is happening, it is saving the result in an integer. Which is ignoring the floating point value.
EDIT: If you want to print only 2 floating points then do the following -
printf("La media aritmética de %.f i %.f es %%0.2f\n", a, b,(a+b)/2);
For more info please see this Floating point rounding
This is because you are using int instead of float or double. Integer division in C will provide only integer result by rounding off the output. Please use float instead of int or typecast the solution with float.
float c;
c = (float)(a+b)/2
However these are very basic C questions. Please go through C tutorial or C books for these kind of answers.
Change the data type of a and b to float/double.
#include
int main(void)
{
float a,b,c;
a = 5.0;
b = 18.0;
c=(a+b)/2;
printf("La media aritmética de %f i %f es %f\n", a, b,c );
return 0;
}
Problem in your code is data type.When dividing any numbers, sometimes it return whole or decimal value.
For eg,
Let divide 10/2=5 and 10/4=2.5.
Thus,be aware in allocate datatypes.
For your problem,there are lot of ways to solve,
1.Change data type of particular answer storing variable
2.Change data type for all process involving variables
3.Change format specifier
4.Typecasting
Related
My colleague and I are studying for a test, where we have to analyze C Code. Looking through the tests from the previous years, we saw the following code, which we don't really understand:
#include <stdio.h>
#define SUM(a,b) a + b
#define HALF(a) a / 2
int main(int argc, char *argv[])
{
int big = 6;
float small = 3.0;
printf("The average is %d\n", HALF(SUM(big, small)));
return 0;
}
This code prints 0, which we don't understand at all... Can you explain this to us?
Thanks so much in advance!
The compiler's warnings (format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’) give more-than-enough information. You need to correct your format-specifier, which should be %lf, instead of %d, since you are trying to print a double value.
printf("The average is %lf\n", HALF(SUM(big, small)));
printf will treat the memory you point as however you tell it to. Here, it is treats the memory that represents the float as an int. Because the two are stored differently, you should get what is essentially a random number. It needs not be 0 always.
To get correct output
Add parentheses in macro
Use correct format specifier (%f)
Corrected Code
#include <stdio.h>
#define SUM(a, b) (a + b)
#define HALF(a) a / 2
int main() {
int big = 6;
float small = 3.0;
printf("The average is %f\n", HALF(SUM(big, small)));
return 0;
}
Output
The average is 4.500000
If you don't add parentheses, output will be 7.500000 due to operator precedence.
In case you need integer output, cast to int before printing.
printf("The average is %d\n", (int)HALF(SUM(big, small)));
Output
The average is 4
wanna divide 2 numbers and get the result like this:
5 / 2 = 2.50
But it only outputs 2.
I don't now what i'm doing wrong.
Here my code:
int a;
int b;
int c;
printf("First num\n");
scanf("%d", &a);
printf("Second num\n");
scanf("%d", &b);
c = a / b;
printf("%d", c);
You need a double variable to store the result. int stores only integers. Additionally, you have to typecast the other variables also before performing the division.
Do something like this
double c;
.
.
.
c = (double)a / (double)b;
printf("%f", c);
NOTE:
You do not need the & in printf() statements.
To avoid the typecast in float you can directly use scanf with %f flag.
float a;
float b;
float c;
printf("First number\n");
scanf("%f", &a);
printf("Second number\n");
scanf("%f", &b);
c = a / b;
printf("%f", c);
The '/' - sign is for division. Whenever in C language, you divide an integer with an integer and store the data in an integer, the answer as output is an integer. For example
int a = 3, b = 2, c = 0;
c = a/b; // That is c = 3/2;
printf("%d", c);
The output received is: 1
The reason is the type of variable you have used, i.e. integer (int)
Whenever an integer is used for storing the output, the result will be stored as integer and not a decimal value.
For storing the decimal results, C language provide float, double, long float and long double.
Whenever you perform an operation and desires an output in decimal, then you can use the above mentioned datatypes for your resultant storage variable. For example
int a = 3, b = 2;
float c = 0.0;
c = (float)a/b; // That is c = 3/2;
printf("%.1f", c);
The output received: 1.5
So, I think this will help you to understand the concept.
Remember: When you are using float then the access specifier is %f. You need to convert your answer into float, just as I did, and then the answer will be reflected.
You have to use float or double variables, not int (integer) ones. Also note that a division between two integers will lead to an integer result, meanwhile a division between a float/double and an integer will lead to a float result. That's because C implicitly promote this integer to float.
For example:
5/2 = 2
5/2.0f = 2.5f
Note the .0f, this actually means that we are dividing with a float.
In C, only an int type number is displayed. 5/2 gives a floating point type number. So, the compiler compiles it only with the integer value.
I'm working on a Lab assignment for my introduction to C programming class and we're learning about casting.
As part of an exercise I had to write this program and explain the casting that happens in each exercise:
#include <stdio.h>
int main(void)
{
int a = 2, b = 3;
float f = 2.5;
double d = -1.2;
int int_result;
float real_result;
// exercise 1
int_result = a * f;
printf("%d\n", int_result);
// exercise 2
real_result = a * f;
printf("%f\n", real_result);
// exercise 3
real_result = (float) a * b;
printf("%f\n", real_result);
// exercise 4
d = a + b / a * f;
printf("%d\n", d);
// exercise 5
d = f * b / a + a;
printf("%d\n", d);
return 0;
}
I get the following output:
5
5.000000
6.000000
1074921472
1075249152
For the last two outputs, the mathematical operations that are conducted result in float values. Since the variable they're being stored in is of the type double, the cast from float to double shouldn't affect the values, should it? But when I print out the value of d, I get garbage numbers as shown in the output.
Could someone please explain?
But when I print out the value of d, I get garbage numbers as shown in the output.
You are using %d as the format instead of %f or %lf. When the format specifier and the argument type don't match, you get undefined behavior.
%d takes an int (and prints it in decimal format).
%f takes a double.
%lf is either an error (C89) or equivalent to %f (since C99).
#include<stdio.h>
int main(void) {
int m=2, a=5, b=4;
float c=3.0, d=4.0;
printf("%.2f,%.2f\n", (a/b)*m, (a/d)*m);
printf("%.2f,%.2f\n", (a/d)*m, (a/b)*m);
return 0;
}
The result is:
2.50,0.00
2.50,-5487459522906928958771870404376799406808566324353377030104786519743796498661129086808599726405487030183023928761546165866809436788166721199470577627133198744209879004896284033606071946689658593354711574682628407789000148729336462084532657713450945423953627239707603534923756075420253339949731915621203968.00
I want to know what cause this difference.
However, if I change int to float, the answer is the same as I expect.
The result is:
2.50,2.50
2.50,2.50
You are using wrong format specifiers, try this:
#include<stdio.h>
int main(void)
{
int m=2,a=5,b=4;
float fm=2,fa=5,fb=4;
float c=3.0,d=4.0;
//First expression in this printf is int and second is float due to d
printf("%d , %.2f\n\n",(a/b)*m,(a/d)*m);
//Second expression in this printf is int and first is float due to d
printf("%.2f , %d\n\n",(a/d)*m,(a/b)*m);
printf("%.2f , %.2f\n\n",(fa/b)*fm,(fa/d)*fm);
printf("%.2f , %.2f\n\n",(fa/d)*fm,(fa/b)*fm);
return 0;
}
Output:
2 , 0
0 , 1074003968
2.50 , 2.50
2.50 , 2.50
Section 7.19.6.1 p9 of the C99 standard says:
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
Note that a/b is int if both are ints and is float if at least one is float, and similarly for other arithmetic operators. Thus in a/b, if they both are ints then 5/4 = 1; if at least one is float, then 5/4.0 = 5.0/4.0 = 1.25, because the compiler automatically converts an int into a float before any arithmetics with another float. So your results were expected to be different.
But in your case you seem to use the %.2f format even when you output ints. So print takes the four bytes that have your int and tries to decode those four bytes as if they had some float encoded. Flot numbers are encoded very differently in memory from ints -- it's like taking a Hungarian text and tryint to interpret it as if it was written in English, even if the letters are the same -- the resulting "interpretation" will be just garbage.
What you need to do: any int should be output with %d and any float with %f or similar formats.
If you want the result to be in float cast them:
#include <stdio.h>
int main(void) {
// your code goes here
int m=2,a=5,b=4;
float c=3.0,d=4.0;
printf("%.2f,%.2f\n",(float)(a/b)*m,(float)(a/d)*m);
printf("%.2f,%.2f\n", (float) (a/d)*m,(float)((a/b)*m));
return 0;
}
Hope this helps..:)
You are trying to print integer values
printf("%d,%.2f\n",(a/b)*m,(a/d)*m);
printf("%.2f , %d\n\n",(a/d)*m,(a/b)*m);
In order to print integer values you should use %d using wrong format specifier lead to undefined behavior
I am willing to cast precise operations and for that purpose I need a way to
seperate a float number into an integer and a fractional part.
Is there any way for this?
There is a function included in math.h library called modf
With this function you can do just what are you trying to.
Example:
#include <stdio.h>
#include <math.h>
double ftof ()
{
double floating = 3.40, fractional, integer;
fractional = modf(floating, &integer);
printf ("Floating: %g\nInteger: %g\nFractional: %g", floating, integer, fractional); // when using printf, there are no floats
return fractional;
}
Output:
Floating: 3.40
Integer: 3
Fractional: 0.40
Note that using double in most of the cases is better than using float, despite that double
consumes twice the memory of float (4:8 bytes) hence the increased range and accuracy. Also in case you need more precise output from
bigger floating numbers when printing, you can try the printf() exponent format specifier %e instead of %g which only uses the
shortest representation of the floating decimal.
One other way using type cast.
#include <stdio.h>
#include <math.h>
void main()
{
float a = 3.4;
float a_frac = a - (int) a;
float a_int = a - a_frac;
printf("Number = %f, Integer = %f, Fraction = %f", a, a_frac, a_int);
}
A thought crossed my mind to separate them with some logic :
#include <iostream>
using namespace std;
int main()
{
double fr,i,in,num=12.7;
for(i=0;i<num;i++)
{
fr=num-i;
}
cout<<"num: "<<num;
cout<<"\nfraction: "<<fr;
in=num-fr;
cout<<"\nInteger: "<<in;
}
Hope this was what you were searching for:) .
#include <bits/stdc++.h>
using namespace std;
int main()
{
double n;
cin>>n;
double fr = n-((int)n);
cout<<"int "<<(int) n<<"\n";
cout<<"fraction "<< fr;
return 0;
}