Documente Academic
Documente Profesional
Documente Cultură
Problem 2.1
Determine the size,minimum and maximum value following data types. Please specify if your
machine is 32 bit or 64 bits in the answer.
char
unsigned char
short
int
unsigned int
unsigned long
oat
Hint: Use sizeof() operator,limits.h and oat.h header les
Answer: On my 32-bit machine (/usr/include/limits.h,/usr/include/oat.h), the sizes and limits
are as follows. Results may dier if you used a 64 bit machine.
Data type
char
unsigned char
short
int
unsigned int
unsigned long
oat
size (bytes)
1
1
2
4
4
4
4
min
SCHAR MIN (-128)
0
SHRT MIN (-32768)
INT MIN (-2147483648)
0
0
FLT MIN(1.175494e-38)
max
SCHAR MAX (127)
UCHAR MAX(255)
SHRT MAX (32767)
INT MAX (2147483647)
UINT MAX(4294967295)
ULONG MAX(4294967295)
FLT MAX(3.402823e+38)
Problem 2.2
Write logical expressions that tests whether a given character variable c is
lower case letter (Answer: c>=a&& c<=z)
upper case letter (Answer: c>=A&& c<=Z)
1
Answer:
(a) The operator precedence is ==>&>=. Thus, the expression is equivalent to c= (x &
(MASK==0)). Therefore x=0xFF33,c=0.
(b) The operator precedence is ++>*>+. Thus, the expression is equivalent to z =(x++) + ((+
+y)2). Therefore x=11,y=3,z=10+32=16.
(b) The operator precedence is &>&&>>>=. Thus, the expression is equivalent to y>>= (x & 0x2) &&
z. Therefore x=10,y=2,z=1.
Problem 2.5
Determine if the following statements have any errors. If so, highlight them and explain why.
(a) int 2nd
value=10;
MASK=0xF;ison=x&MASK==MASK)
Answer:
(a) Variable names cannot start with a number.
(b) = operator should be replaced with ==. The correct version is alliszero =(x==1) && (y==0);.
(c) There is nothing wrong with the statement. While > may look suspicious, the expression
symplies to y= (++x)+y; z=(z)>x.
(c) There is nothing syntatically wrong with the statement. However, what we want is ison=(x&MASK)==MASK. Based
on operator precedence, the current expression simplies to
ison=x&(MASK==MASK)
These exercises should have convinced you to use () always.
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
%f prints the corresponding number as a decimal floating point number, e.g. 321.65.
%e prints the number in scientific notation, e.g. 3.2165e+2.
%g prints the number in the shortest of these two representations
In above case, using the same number, the decimal floating point is shorter and hence 321.65 would be
printed. %g would print the scientific notation is when the number is 6000000000. In scientific notation, this
would 6e+9 which is shorter and hence, 6e+9 would be printed.
float a = 321.65;
float b = 16000000000;
printf("%f\n", a);
printf("%e\n", a);
printf("%g\n", a);
printf("%f\n", b);
printf("%e\n", b);
printf("%g", b);
Points to considered :
and ch
s
We shall define first weight as Minimum, Maximum and Total
and keep comparing each variable to check if its less than
minimum or more than maximum, and than we shall add it to
total. Also count shall be increased by 1 for every number
checked.
Since question says last weight is 0, we shall check if we
have reached at weight = 0, once we reach there we shall
calculate Mean and Stop. Till than the Loop shall keep running.
So effectively we shall have 3 decision boxes same as last ques