Use automation (format specifiers) to print decimal, octal, and hexadecimal
values of your initials.
There is no format specifier for binary output in C. You will need
to type in the binary values manually (unless you devise a smart automated
procedure). Derive the binary values by hand using expansion in powers of 2.
Example: 39(dec) = 32+4+2+1 = 1*25+0*24
+0*23+1*22
+1*21+1*20=100111(bin)
Brief about your calculation in comments, e.g. /* 39=32+4+2+1 */
Compare your values with those in a table of binary values in Chapter 2
of the Text.
In definition of your magic number, you can use standard mathematical functions
provided by the library math.h (which needs to be #include'd
in that case.) The list of available functions is found on pp. 622-623 of the
Text. See also p.231.
Let your magic number be calculated by the program from the two numeric values
of your initials. Do not type in a manually computed answer.
If you use a floating point variable (float or double)
for your magic number, use the format specifier %f to print the value.
(Sect.2.3)
Q2
Writing and running a program
is not supposed here. Submit your answer in a handwritten form.
You may add necessary lines to the given fragment of code
to obtain a working C program, which would print all the intermediate
and final results. It is actually a good way to check the work
you did with pencil and paper. (Look at program 3.3. on p.86 of Text
and a tracing table on p.85).
In any case, you must be able to trace execution of code line by line
manually. There will be such questions on exams, where you
won't be allowed to use a computer to help.
Example
Let the variables a, b, c be declared as int.
Consider the following fragment of code:
1
a=10; /*initial value*/
2
b=a/3;
3
c=b+2;
4
c*=7;
5
a=(c-a)%3;
Tracing table:
line
a
b
c
comment
1
10
?
?
b,c so far undefined
2
10
3
?
10/3=3 (rem.1)
3
10
3
5
3+2=5
4
10
3
35
5*7=35
5
1
3
35
35-10=25, 25/3=8 (rem.1)
Here is a
program written to check the above calculations.
See p.44 on integer division /, % .
The arithmetic operation % not to be confused with format specifiers,
which can only be used inside double quotes in printf
function.