Netid: ___________________ Section: ____________
Each blank is worth 1/6 point.
Print out this page and fill in the blanks to answer the following
questions. Hand in this assignment at the beginning of Lab 7. You
should use the material found in Lecture 10,11 and 12 of the course
notes and/or the course C book Chapters 1,2 ,3.1 -3.5,7.1-7.3. As with
all prelabs/labs, if you are not sure of the answer you may go to any
EWS lab on campus and check your results by using the gcc compiler, or
you can ask anyone of the CS101 staff for help.
The following exercises should help prepare you for the in-lab
activities and MP2.
You will use the C operations % ,+ , * , / on values of datatype
integer, use the printf and scanf functions and along with the if-else
and switch statements.
Complete the following before coming to lab. Of course you can check
your answers by opening Xemacs and copying and pasting each program
into a separate file and then use gcc to compile and run each program
by typing a.out at the Unix prompt.
#include <stdio.h>
void main(void)
{
int j = 7;
int k = 5;
int hours;
int mins;
int secs;
float x = 1.23456789; /* float type data holds about 6-7 digits */
double y = 1.23456789; /* double type data holds about 14 digits */
/* integer variables and operations */
printf( " result = %i \n", j / k ); /* a) Answer: ___result = 1_______________________ (computations involving only integers return integer values) */
printf( "%i result = \n", k / j ); /* b) Answer: ____0 result =__________________ (notice that the %i controls where the integer appears on the computer screen */
printf( "%i \n", j % k ); /* c) Answer: _____2_____________________ ( % means "remainder" just like doing 5th grade math) */
/* %f shows as default 6 digits to the right of the decimal point. %.2f will show 2 digits to the right of the decimal. */
printf( "x = %f \n", x ); /* d) Answer: ____x = 1.234568______________________ */
printf( " Your cost = $%.2f \n", x ); /* e) Answer: _____Your cost = $1.23_____________________ */
printf( " Omitting the change = $%.0f\n", x ); /* f) Answer: ____ Omitting the change = $1_______ */
/* double data-type variables holds more digits than float variables. */
printf( "x = %.9f \n", x ); /* g) Answer: ______x = 1.234567881 ________ */
printf( "y = %.9lf \n", y ); /* h) Answer: ______y = 1.234567890 ________ */
/* common errors */
printf(" Enter 9.876 :");
scanf( "%lf", &x ); /* Oops, x is of float data-type so we should have used %f . */
printf("x = %f \n",x); /* i) Answer: ______x = 0.000000____________________ */
printf(" Enter 3.14 : ");
scanf( " Hello World! %lf", &y ); /* For scanf, text inside " " doesn't display on screen and causes problems. */
printf(" y = %lf \n", y); /* j) Answer: ______y = 1.234568____________________ */
/* a useful trick (for MP 2) */
/* print time in format hh:mm:ss , for example 12:09:08 */
hours = 12;
mins = 9;
secs = 8;
printf(" %i:%i:%i \n", hours, mins, secs); /* k) Answer: _____ 12:9:8 _________ */
printf(" %02i:%02i:%02i \n", hours, mins, secs); /* l) Answer: ________ 12:09:08__________________ */
}
m) ________not equal!______________________________
n) _________The input is not valid!_____________________________
o) _________three_____________________________
Common errors in writing if, nested if and switch statements.
p) _________positive!_____________________________