I am very new to programming, taking my first computer science class. I am writing a program and for some reason whenever I try to calculate the floating point value of any operation it just results in 0.00000. If anyone can please help me I will greatly appreciate it!
#include <stdio.h>
int main(void)
{
int numa, numb;
int sum, halffirst, halfsecond, quotient, remainder;
// get input
printf("\nPlease enter two integers: ");
scanf("%d%d", &numa, &numb);
// calculate dimensions
sum = numa + numb;
halffirst = (double)numa / 2.0;
halfsecond = numb / 2;
quotient = numa / numb;
remainder = numa % numb;
// display report
printf("\n%20s%20s", "Description", "Data");
printf("\n%20s%20s", "-----------", "----");
printf("\n%20s%20d", "Sum", sum );
printf("\n%20s%20lf", "Half (1st #)", halffirst);
printf("\n%20s%20d", "Half (2nd #)", halfsecond);
printf("\n%20s%20lf", "Quotient", quotient);
printf("\n%20s%20d", "Remainder", remainder);
// format and finish
printf("\n\n");开发者_JAVA技巧
return 0;
}
That's because you're using the int
data type which is an integer. Operations performed on integers tend to produce integers (truncated) so something like:
int nom = 6;
int den = 10;
float frac = nom / den;
will give you a value of zero since the arithmetic is done on integers and only converted to a float at the end (after the damage is done).
You can get around this by casting, for example:
float frac = (float)nom / (float)den;
which will make floating point values out of the integers before the division.
However, you'd be better off using floating point for all your values up front. The float
(single precision) or double
(double precision) is what you're after. Double precision values have more ... well, precision, meaning that they can store more digits and a larger range of numbers as well. For class work, float
should probably be fine.
You should assign your variables as float or double to save the correct value instead of using ints.
double sum, halffirst, halfsecond, quotient, remainder;
Replace the line:
int sum, halffirst, halfsecond, quotient, remainder;
with the one above.
According to C-99 standard section 7.19.6.1 paragraph 9
If a conversion specification is invalid, the behavior is undefined.251) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
halffirst
and quotient
is defined as int
but in the printf
calls, you are using %lf
that is the specifier for double
printf("\n%20s%20lf", "Half (1st #)", halffirst);
printf("\n%20s%20lf", "Quotient", quotient);
Which is undefined behaviour according to the standard.
Use %d
to print integers
printf("\n%20s %d", "Half (1st #)", halffirst);
printf("\n%20s %d", "Quotient", quotient);
OR, typecast the integers to float
before passing
printf("\n%20s%20lf", "Half (1st #)", (float)halffirst);
printf("\n%20s%20lf", "Quotient", (float)quotient);
or typecast the int
to float before passing it.
A couple of things:
- If you want to use floating point numbers as variables, then you have to declare your variables as
float
rather thanint
. In C and C++, when you divide a number and assign it to anint
, the result drops all the decimals, leaving only the real number stored in the variable. If you are diving and you have an integer in your denominator, you have to cast the integer to a float point number, like so:
int num1 = 1, num2 = 2; float theDiv = num1/(float)num2;
If you don't do that it will also drop the decimals.
All your variables are declared as int
.
Try using float
or double
.
Types are a nightmare for a beginning programmer. You're dealing with two types here:
int
, an integer value, and
double
, a double-precision floating-point number.
C and similar languages are very pedantic about types, casting, and coercion. You have to be very careful when dealing with multiple types in a single expression. Breaking down your line halffirst = (double)numa / 2.0;
step by step. Let's use an example; say numa
has the value 5.
- First, cast
numa
todouble
. This is fine. You now have 5.0 as your divisor. - Divide by 2.0. The dividend, 5.0, is a double, and the divisor, 2.0, is a double, so the result (naturally) will be a double, 2.5. Also fine.
- Store the result in
halffirst
.halffirst
is anint
type; we can't directly store the value of adouble
into anint
variable. Therefore, we have to convert it;(int)2.5
yields 2, so we store 2 intohalffirst
.
If you want to store the double
result of your division, you need to declare the destination variable, halffirst
, as type double
, otherwise the implicit conversion at the end will hose you. halfsecond
is the same way, for the same reason. Therefore, in your declarations at the top of the main
method, try this:
int numa, numb;
int sum, quotient, remainder;
double halffirst, halfsecond;
Make sense?
精彩评论