Doing operations between an int and double - c

I have a homework assignment in basic C that is asking me to calculate certain expressions and then check my answers in a program. I can't seem to get any of these answers correct by my own calculations...
They want me to solve math problems using these variables:
int a = 2;
double b = 4.7;
int c = 3;
double d = 4.2;
Here's an example question:
int answer1 = b+a/c-d;
I understand that since it has an int operand then all the variables convert to an integer, so it should look something like this I think:
(4+2)/(3-4)
I got -6 as my answer when doing it by hand, but when I input it as code it gives me an answer of 0. Can anyone explain this? Am I doing the order of operations wrong? I simply don't understand how the computer gets 0 out of that. This is the easiest question in my homework and I don't have a clue. Please help!

Your expression
b+a/c-d
is the same as
b + (a/c) - d
Since both a and c are integers, then the quotient a/c is computed using integer division. That gives 2/3 = 0. So then you have:
b - d
This is calculated using floating point since b and d are double. The result is 0.5, which when assigned to the result int, is truncated to 0.

Your orders of operations is a bit off:
int answer1 = b+(a/c)-d;
int tmp1 = a/c; ---> 2/3 --> 0
int answer1 = 4.7 + 0 - 4.2 ----> 0.5 --> 0
http://www.cplusplus.com/doc/tutorial/typecasting/

Related

Two different answers for same expression in C

I have an expression which does the same calculation. When I try to do the whole calculation in a single expression and store it in variable "a", the expression calculates the answer as 0. When I divide the equations in two different parts and then calculate it, it gives the answer -0.332087. Obviously, -0.332087 is the correct answer. Can anybody explain why is this program misbehaving like this?
#include<stdio.h>
void main(){
double a, b, c;
int n=0, sumY=0, sumX=0, sumX2=0, sumY2=0, sumXY=0;
n = 85;
sumX = 4276;
sumY = 15907;
sumX2 = 288130;
sumY2 = 3379721;
sumXY = 775966;
a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
b = ((n*sumXY) - (sumX*sumY));
c = ((n*sumX2) - (sumX*sumX));
printf("%lf\n", a);
printf("%lf\n", b/c);
}
Output:
0.000000
-0.332097
In your program
a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
all the variables in the right hand side are of type int, so it will produce a result of type int. The true answer -0.332097 is not a int value, so it will be converted to a valid int value, namely 0. And this 0 is assigned to variable a.
But when you do
b = ((n*sumXY) - (sumX*sumY));
c = ((n*sumX2) - (sumX*sumX));
printf("%lf\n", b/c);
The variable b and c are of type double, so the expression b/c produce a double typed value and the true answer -0.332097 is a valid double value. Thus this part of your code give a right result.
In first equation i.e. a = ((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX)) both numerator and denominator will give integer results and the value stored in a will also be integer as integer/integer is integer. In second and third expression as you are solving them individually both b and c will be stored in double and double/double will result in a double i.e. a decimal value.
This problem can be solved by using type casting - or better still using float for the variables.
Add double before your calculation, so after you do your integer calculation in "a", it will convert it to double.
a = (double)((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
First of all (INTEGER)/(INTEGER) is always an INTEGER. So, you can typecast it like a = (double)(((n*sumXY) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX)));
OR
We know that any number (n ∈ ℂ), multiplied by 1.0 always gives the same number (n). So, your code shall be like:
a = ((n*sumXY*1.0L) - (sumX*sumY)) / ((n*sumX2)-(sumX*sumX));
Multiplying by 1.0L or 1.0f converts the whole arithmetic operation to long double data type.
Now you can print the number (-0.332097) to stdout.
Non-standard Code
Your code is:
void main()
{
//// YOUR CODE
}
Which is non-standard C. Instead your code should be like:
int main(int argc, char **argv)
{
//// YOUR CODE
return 0;
}
Change all your INTEGERS to DOUBLES. That should solve the problem
The type of 1st expression is int whereas in 2nd expression it is double.

Series: 1 + 1/3 + 1/5 +...upto N terms

I was recently asked this question in a programming test. I can't seem to understand why I am getting the answer '1'. I am a beginner in the C programming language.
Here is my code:
#include<stdio.h>
int main()
{
float c = 0;
int n, i = 1;
printf("Enter the number here: ");
n = getchar();
while (i <= 2*n - 1)
{
c = c + (1/i);
i = i + 2;
}
printf("%f", c);
}
I have already tried using a for loop, but the answer remains the same. Any help would be appreciated!
The problem in your code lies on this line:
c = c + (1/i);
Here, the operation performed inside the parentheses is integer division! So, when i has any value greater than 1, the result will be zero. This zero is then converted to a float value.
To force the compiler to use floating point division, use this:
c = c + (1.0/i);
I agree with Adrian's answer.
Another issue is because of the way floating point numbers are represented in a system when they are added in arbitrary order, precision can be lost.
To have maximum precision, floating point numbers should be added from smallest first to largest last.

C giving wrong answer to calculation

I am coding a very simple program in C but I keep getting wrong answers to calculations that I am doing. The final output that I want needs to have no decimal places so I am using int as the data type even though the answer will not be an integer. Here is the code:
int numberOfInches = (100/254)*101;
I either get the answer 0 if I use int as the data type or crazy long numbers if I try using float or double. Any ideas on what I am doing wrong?
100 / 254
This is integer division, which will get 0. You then multiply by 101.
To do floating point division, at least one of the operands of / must be floating point:
int n = (int)((100. / 254.) * 101.);
When both operands of the / operator are integer, it performs integer division, i.e. the result is the quotient with the fraction part trucated. The result of 100/254 is less that 1, so it rounds down to 0.
You can either make one of the constants floating point:
(100.0/254)*101
Or you can do the division last:
(100*101)/254
You use an int. The results are rounded to the nearest int value:
100/254 = 0 THEN 0 * 101 = 0 SO, final result is 0.
I think you can do something like :
int numberOfInches = 100 * 101 / 254;
result: 100 * 101 = 10100 THEN 10100 / 254 = 39.7xxxxxx SO, final result is 40.
Obviously it will give 0.
int numberOfInches = (100/254)*101;
When it will calculated first it will evaluate inner bracket.
so (100/254) will be 0 And when you multiply 0 to 101, i.e 0*101 = 0.
It will be 0.
To get the correct output, use the following.
int numberOfInches =(int)((100.0/254)*101);
Division in parentheses is integer-type, so 100/254=0. If you want to calculate value with a fractional part try:
int numberOfInches = (100.0 / 254.0) * 101.0;
Why not using float ? try 100.0f/254.0f

Take two numbers to one number

I have two int values that I want to combine into a decimal number. So for example, I have A = 1234 and B = 323444. Both are int and I do not want to change it if possible.
I want to combine them to get 1234234233.323444.
My initial method was to divide b by 1e6 and add it to A to get my value.
I assigned
int A = 1234234233;
int B = 323444;
double C;
A = 1234;
B = 323444;
C = A + (B/ 1000000);
printf("%.6f\n", C);
I get 1234234233.000000 as a result. It rounds my C and I do not want that as I want 1234234233.323444
how can I solve this?
Try like this:
C = A + (B/ 1000000.0);
ie, make the denominator as double so that when integer by integer division is made it does not return weird results like you are getting.
NOTE:-
Integer/Integer = Integer
Integer/Double = Double
Double/Integer = Double
Double/Double = Double
B is an integer and dividing an integer by another integer (10000 here) will always give an integer and that's why you are getting unexpected result. Changing 10000, which is of type int, to 10000.0 (double type) will solve this problem. It seem that 10000 and 10000.0 are integer by mathematical definition but both are of different type in programming languages, former is of type int while latter is of type double.
C = A + (B/ 1000000.0);
or
C = A + ((double)B/ 1000000);
to get the expected result.

Using type #define values for division in C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does this integer division yield 0?
I have a C header file,which contains various constant values used in my project.The values are defined using #define (e.g. #define MAX_DATA_VAL 5).
When I use this value in another file for division,I get the wrong answer.
//- In file MyParameters.h
#define MAX_DATA_VAL 5
#define MIN_DATA_VAL -5
// - In file MyFunctions.c
#include "MyParameters.h"
void Function()
{
float temp = 0;
temp = 2/MAX_DATA_VAL; // Wrong Output
}
The value of temp remains 0 (checked while debugging).However if I did:
temp = 0;
float a = MAX_DATA_VAL;
temp = 2/MAX_DATA_VAL; // Correct Output
I get the right answer!!...Could anyone please explain what's happening...
Thanks in advance :)
MAX_DATA_VAL is being treated as an integer and thus your division was doing integer division (rounds down to nearest whole number). To make sure it knows it's a float, do:
#define MAX_DATA_VAL 5.0f
You need to cast the defined value to a float, otherwise it is a integer.
void Function()
{
float temp = 0;
temp = 2 / (float)MAX_DATA_VAL;
}
In computer world, 2/5 == 0 because of integer division.
2 and 5 are both integers, so its natural for the computer to think that the result is also an integer. Unfortunately mathematically it's not, so it truncates off the remainder.
In
temp = 2/MAX_DATA_VAL;
you are dividing an int by an int, therefore the result is an int - probably not what you want. To get the right result you should do
temp = 2.0/MAX_DATA_VAL;
I saw that the other answers all suggested changing the define of MAX_DATA_VAL. However, I would advise against this, unless you checked your entire codebase and made sure that you are not depending on it being an int. My solution only makes a change at the place where OP said his results were not as expected.

Resources