Name: ________________________ Netid: _______________ Section:
_______
Name: ________________________ Netid: _______________ Section: _______
Name: ________________________ Netid: _______________ Section:
_______
The following C program compiles without errors. Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int i = 17.25; /* i holds ONLY integer values */
double x;
printf(" %i ", i );
x = 8 / 3; /* 8 and 3 are integers */
printf("%lf ",x);
x = 8 / 3.0; /* 8 is an integer 3.0 is a real (double) */
printf("%lf ",x);
x = 8.0 / 3;
printf("%lf ",x);
x = 8.0/3.0;
printf("%lf ",x);
printf("\n");
}
______ 17 2.000000 2.666667 2.666667 2.666667 _________________________
The following C program compiles without errors. Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int x ;
if (0) /* 0 means FALSE */
printf("False ");
if (33) /* any non-zero value in C means TRUE */
printf("True ");
x = 0;
if (x == -1)
printf("T ");
else
if (x != 0) /* != means "not equal" */
printf("U ");
else
printf("Y ");
if (x == -1); /* BAD place for a ; as discussed in lecture */
printf("(';' Are not good after ifs) ");
if (x = 0) /* oops!, forgot == */
printf("W ");
printf("\n");
}
________True Y (';' Are not good after ifs) _____________________
The following C program compiles without errors. Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int x = 8;
int y = 3;
if ((x < 2) || ( y > 3)) /* || means OR */
printf("L ");
if ((x > 7) && (y < 4)) /* && means AND */
printf("M ");
else
printf("N ");
if ( !(x < 10)) /* ! means NOT */
printf("O ");
printf("\n");
}
________________________M_________________________________________
The following C program compiles without errors (Note: there are no typos). Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int y = 3;
while (y >= 0) /* while condition is TRUE execute code in block { } */
{
y = y - 2;
}
printf("y = %i ",y);
printf("\n");
}
_________________________y = -1_______________________________
The following C program compiles without errors (Note: there are no typos). Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int y = 56;
while (y >= 0) /* how many statements below are part of the while loop? */
y = y - 1;
printf("T ");
printf("\n");
}
___________________________T______________________________________________
The following C program compiles without errors (Note: there are no typos). Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int y = 2;
while (y >= 0) /* how many statements below are part of the while loop? */
{
y = y - 1;
printf("N ");
}
printf("\n");
}
________________N N N______________________________________________
The following C program compiles without errors. Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int i;
for(i=1; i < 6; i=i+2)
{
printf("%i ",i);
}
printf("\n");
}
_____________1________3________5__________________________________
The following C program compiles without errors. Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int i, j;
for(i=0; i < 4; i=(2*i)+1)
for(j=i; j < 2; j = j + 1)
printf("<%i,%i> ",i,j);
}
_________<0,0>___<0,1>____<1,1>____________________________
The following code fragment is placed in a complete C program
compiled (without errors) and run. Will the following loop
terminate? (Yes/ No)
__________________________YES____________________________________
Note: A loop that does not terminate is called an infinite
loop.
int a=8;
int b=8;
while(1) /* 0 means False any other value means True */
{
if(a > b)
break; /* the break statement "breaks out" of loops and switch statements */
if (a == 100)
a = 1;
else /* else matches with the closest if (unless {} are used) */
b = b - 1;
}
The following C program compiles without errors. Write the output produced by this program.
#include <stdio.h>
void main(void)
{
int i = 3;
do
{
printf("%i ",i-1);
i = i - 2;
} while( i > 0);
printf("\n");
}
______________2 __________ __________________0 ____________________________
Complete the following loop to print all the odd numbers from 1 to 99 inclusive as follows:
1 3 5 7 9 ... 99
int i; for(i = 1; ____i <= 99_____or i < 100____________; ______i = i+2_______) { printf("%i ", __________i_________ ); }OR
int i; for(i = 1; __i <= 50___or i < 51__; _i = i+1 or ++i or i++_____) { printf("%i ", __________2*i-1_________ ); }
The following C program compiles without errors. Write the
output produced by this program.
Note that abs is used for integer values but fabs is used for
values of data type float or double.
#include <stdio.h>
#include <math.h>
void main(void)
{
float x = 1.0/3.0; /* float and double data types hold real
numbers. double holds more digits than float */
double y = 1.0/3.0;
if (x == y)
printf(" x equals y ");
else if ( fabs(x - y) < 1.0e-6)
printf(" x is close to y ");
else if (fabs(x-y) < 1.0e-20)
printf(" x is very close to y ");
else
printf(" x is not even close to y! ");
printf("\n");
}
____________________x is close to y________________________________________________________
Each problem in Parts 2-4 is
worth 1 point.
1. Run your program and enter 4 as input. Write down the results
your program displays.
________32___________ _______32____________ _________54__________
_________12__________ _______52____________
2. Rewrite the program in the loop.c file using a do-while
loop. Fill in the blanks below with your code. Try compiling and
running both versions of the loop to make sure they behave exactly
the same.
/* You may not need to use all the blanks below. */
#include <stdio.h>
#include <time.h> /* library containing the "time" function */
#include <stdlib.h> /* library containing the "rand" function */
void main(void)
{
int N;
int Total = 5;
int counter = 1;
printf("Enter a number: ");
scanf("%i", &N);
________do_______________________________________________________________________
_________{_______________________________________________________________________
_____________printf(" %i ", rand()%(N+1));________________________________________
_____________counter = counter + 1;_______________________________________________
________} while(counter <= Total);________________________________________________
printf("\n");
}
3. Rewrite the program using a for loop. Fill in the
blanks below with your code. Try compiling and running both
versions of the loop to make sure they behave exactly the same.
/* You may not need to use all the blanks below. */
#include <stdio.h>
#include <time.h> /* library containing the "time" function */
#include <stdlib.h> /* library containing the "rand" function */
void main(void)
{
int N;
int Total = 5;
int counter = 1;
printf("Enter a number: ");
scanf("%i", &N);
__________for(counter = 1; counter <= Total; counter = counter +1)_____________
__________{___/* curly brace not necessary here */___________
_________________printf(" %i ", rand()%(N+1));___________________________________
__________}___/* curly brace not necessary here */___________
____________________________________________________________________________________________
printf("\n");
}
4. Run the guessing game program three times once for 'e', 'm'
and 'd', write down the computer generated 'target' (the mystery
number to be guessed):
______8_______ ________104____________ __________360_____________
What is the best strategy to win the guessing game?
Guess the middle number in the possible range so that the
range is halved after every guess.
OR any equivalent answer.
Write the single C statement that you created for step 4.
_______________glColor3f(z, 0.0f, 1.0f);_______________