Sie sind auf Seite 1von 8

Massachusetts Institute of Technology

Department of Electrical Engineering and Computer Science


6.087: Practical Programming in C
IAP 2010
Problem Set 2 Solutions
Types, operators, expressions
Out: Tuesday, January 12, 2010.

Due: Wednesday, January 13, 2010.

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

digit (Answer: c>=0&& c<=9)

white space (includes space,tab,new line) (Answer: c==\n|| c==\t|| c==)


Problem 2.3
Consider int val=0xCAFE; Write expressions using bitwise operators that do the following:
(a) test if atleast three of last four bits (LSB) are on
(b) reverse the byte order (i.e., produce val=0xFECA)
(c) rotate fourbits (i.e., produce val=0xECAF)
Answer:
(a) We have to test if last three or four bits are on. The possible values are
0x7,0xB,0xD,0xE,0xF. To test this, rst we extract the last four bits. ( int
bits=val&0xF; /last four bits/). Next we test if it is one of the possible patterns. (
bits==0x7 || bits==0xB || (bits>=0xD)).
(b) val = ((0xFF & val) << 8) | (val>>8)
(b) val = (val >> 4) | ((val&0xF)<<12)
Problem 2.4
Using precedence rules, evaluate the following expressions and determine the value of the vari
ables(without running the code). Also rewrite them using parenthesis to make the order explicit.
(a) Assume (x=0xFF33,MASK=0xFF00).Expression: c=x & MASK ==0;
(b) Assume (x=10,y=2,z=2;).Expression: z=y=x++ + ++y2;
(c) Assume (x=10,y=4,z=1;).Expression: y>>= x&0x2 && z

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;

(b) Assume (x=0,y=0,alliszero=1). alliszero =(x=1) && (y=0);

(c) Assume (x=10,y=3,z=0;). y=++x+y;z=z>x;


(d) Assume that we want to test if last four bits of x are on. (int

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

6.087 Practical Programming in C


January (IAP) 2010

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

NO. "main" is NOT a keyword in C language likes "for", "if"


and "while" are keywords. The main" is just a name of the
function that by convention is usually used by linker as the entry
point of the regular program but not all types of programs use
"main". We can declare variables called "main" in a program:
#include <stdio.h>
int main() {
char* main = "Hello\n";
printf(main);
return 0;
}
Explanation
The name main is not a keyword in C or Java. From the
compiler's perspective, main () is a function. Just like any other
function that you may define in your program, such as factorial(),
sort() etc.The compiler compiles the code for all functions in the
same way. The object code file (.obj or .o) file generated by the
compiler will have some way of indicating each function's object
code, which differs between different object file formats
error: expected declaration specifiers or '...' before 'val2'

printf(%4d, %x, %7.3, %8.3e, val2, val2, num, num) ;


^
error: expected declaration specifiers or '...' before 'val2'
printf(%4d, %x, %7.3, %8.3e, val2, val2, num, num) ;
^
error: expected declaration specifiers or '...' before 'num'
printf(%4d, %x, %7.3, %8.3e, val2, val2, num, num) ;
^
error: expected declaration specifiers or '...' before 'num'
printf(%4d, %x, %7.3, %8.3e, val2, val2, num, num) ;

%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 :

We have to calculate Minimum, maximum and mean


weights. So we have to define 5 variables as follows
Minimum
Maximum
Total
Count
Mean (Total / Count)
Take care that Mean weight can be derived only after we
have checked all the weights for minimum / maximum

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

Das könnte Ihnen auch gefallen