Netid:
___________________ Section: ____________
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. However, if you obtain some or all of your answers by writing the results produced by the compiler, please make sure to understand the results. If something is puzzling, be sure to ask 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 int,
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 Eclipse and copying and pasting each program into a separate file and then running each program as you have learned in lab 6.
#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: _____________________________ (computations
involving only integers return integer values) */
printf( "%i
result =
\n", k / j ); /* b) Answer: _____________________________ (notice that
the
%i controls where the integer appears on
the computer
screen */
printf( "%i
\n", j % k ); /* c) Answer: _____________________________ ( % 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: ______________________________ */
printf( " Your cost = $%.2f \n", x ); /* e)
Answer: ______________________________ */
printf( " Omitting the change = $%.0f\n", x
); /* f) Answer: ______________________________ */
/* double data-type variables holds more digits than float variables.
*/
printf( "x = %.9f \n", x ); /* g) Answer:
______________________________ */
printf( "y = %.9lf \n", y ); /* h) Answer:
______________________________ */
/* common errors */
printf(" Enter 9.876 :");
scanf( "%lf", &x ); /* Oops, x is of
float data-type so we should have used %f rather than %f. */
printf("x
= %f \n",x); /* i)
Answer: ______________________________ */
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:
______________________________ */
/* 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: ______________________________ */
printf(" %02i:%02i:%02i \n", hours, mins, secs); /* l)
Answer:
______________________________ */
}
m) __________________________________________
n) __________________________________________
o) __________________________________________
3. Common errors in writing if, nested if and switch statements.
#include <stdio.h>
void main(void){
int
no;
int
ir, hr;
no = -1;
if (no >
0); /* ; semicolon here means
end of statement
so the printf immediately below is not
part of the if
*/
printf("positive!\n");
no = 0;
if ( no =
1) /* == means test for equality =
means
assign. If no is assigned 1 that means true in C. */
printf(" equal!\n");
else
printf("not
equal!\n");
no = 2;
switch(no){
case 1:
printf("one ");
case
2: printf("two
"); /* note that the break
statements
are missing */
case
3: printf("three ");
default:
printf( "not valid! " );
}
printf("\n");
}
Write the output of the program (above).
p) __________________________________________
q) _________________________________________
r) _________________________________________