A C question

Status
Not open for further replies.

Stiks

Solid State Member
Messages
17
Ok for one of my questions for my C homework I have to answer the following question:

1. What is the value of the following expression in C? Assume a=5, b=7, c=3.2, d=5.0.

a,b=int
c,d,e=double

e=b/a*c+c*d

The thing about that is i dont know what doubles and ints do to numbers, so i cant solve it. If someone could explain what the doubles and ints do then maybe I can get it. Also, I dont understand what a char does so maybe someone can explain that too. Oh yeah I also dont understand what things like %lf, %c etc do.
 
Is your teacher a retard or something? You could also look up what int, char, and double mean in your book or online in less than 5 seconds.

It will be 19.2, i would suggest you study because you clearly have neither listened in class or even opened the book.
 
Well actually I have opened my book. I have a definition of it. Im looking at it right now; however, its not that I dont pay attention or that I havent opened my book, but I just dont understand it. By insulting me or my professor, is not really helping. Im sorry I cant understand these things as well as you do. I have been trying to workout the you got and I still cant see how you got there. I just cant apply it to these equations. If you dont believe that I have the definitions then here they are:

Double-indicates that the memory cells store real numbers.
Int-integer; indicates that the main function returns an integer value.
 
Well the expression is an example of mixed mode arithmetic, you have combined integer and double values in the same expression. Int, double, float etc are different data types and they store data differently. Int will store integer values like 1, 2, 3 , 4 and cannot store 2.0 (it will truncate the value 2.0 to 2) and other similar floating point values. Double is similar to float but more precision, you can store 3.002020 or whatever and if you input 8, it will store it as 8.0.

When you do mixed mode arithmetic and you are using ints and floats with * / you need to be careful. 1/3 is considered int so it will truncate the answer to 0 and mess up your calculation. The simplest way to solve that would be to type 1.0/3.0 or store it in a float data type.

consider the following:

int num1, num2;
float avg;

num1 = 3;
num2 = 7.75; //will truncate to 7
avg = (num1+num2)/20

The result is 0, the int values 3 and 7 are added and then divided by another int. Even if avg is a float it is still being sent an int and the ans will be zero.

If we change the expression as follows:

avg = (num1 + num2)/20.0

we will get 0.5 as a result because the 20.0 ensured we would send the result as a float instead of an int.

In your example:

1. What is the value of the following expression in C? Assume a=5, b=7, c=3.2, d=5.0.

a,b=int
c,d,e=double

e=b/a*c+c*d

e = 7/5*3.2 + 3.2*5.0

7/5 = 1.4 truncates to 1 so it will be 3.2 + 16.0 = 19.2

If we wanted 1.4 we would need to change a and b to floats, even if we just change one it will work. Then the result will be 20.48.

So int/int = int, int/float = float, int*int = int, int*float = float and so on. Read this page as well to help you understand everything that is going on. http://en.wikipedia.org/wiki/Type_casting
 
Status
Not open for further replies.
Back
Top Bottom