You are on page 1of 8

EECS 10, Fall 2014

Midterm Examination (Close Book) Time: 80 minutes Total Points: 120


Name: __________________________ SID ________________

Page 1
Score

Total

1. (10%) What is output produced by the following C program?


#include <stdio.h>
void main (void) {
int i=4;
float f=0;
f = 10/i+1;
printf("f=%3.1f",f);
}
(A) f=2.0

(B) f=3.5

(C) f=3.0

(D) f=2.5

2. (10%) Identify and fix the problem(s) with the following C code to get 8, 7, 6, 5, 4,
as output
#include <stdio.h>
void main (void) {
int i=8;
while (i <=4 );
printf("%d\n",i);
i = i - 1;
}
#include <stdio.h>
void main (void) {
int i=8;
while (i <=4 );
printf("%d, ",i);
i = i - 1;
}
+5 points for while loop condition
+5 points for printf statement

3. (10%) Convert the following switch statement to an if else statement.


#include <stdio.h>
void main (void) {
int i=1;
char ch;
switch(i) {
case 1:
ch='T';
break;
case 0:
ch='F';
break;
default:
ch='O';
break;
}
printf("%c",ch);
}

Not using if-else, no credit


Using if-else but in a wrong way, half credit, which is 5 point
Using if-else correctly but using = instead of ==, 8 point

4. (20%) Here is the solution for assignment 3. Answer the questions below.

1. There is a bug at Line 12. Fix the bug.


2. There is another bug at Line 24. Fix the bug.
3. Change the code from Line 14 to 18, so the program will keep asking for the input
until the user enters a number which is greater than 20.

Sub-problem a and b is pretty straightforward;


For c :
1. Using both while loop and scanf, get full credit
2. Missing one of them, half credit
3. No while loop and scanf, no credit.

5. (20%) Write a program to sum the digits for a given number from the user. For
example, the sum of digits for 123 is 1 + 2 + 3 = 6
#include <stdio.h>
void main(void){
int input,sum=0;
printf("Please enter a number: ");
scanf("%d",&input);
while (input>0){
sum+=input%10;
input/=10;
}
printf("the sum of digits is %d",sum);
}

no scanf --> -1
no printf for result --> -1
wrong calculation -->-5
no loop --> -5
using multiple variables to read some digits -->-10

6. (20%) Write a program that prompts the user for their assignment scores, midterm
score and final score. Then calculate the users letter grade assuming the final grade isnt
curved.
Letter Grade
Range
Programming assignments** (30%)
A
100-90
Midterm (30%)
B
89.99-80
Final (40%)
C
79.99-70
D
69.99-60
**There are 6 assignments
F
59.99-0

2% Variable Declaration, initializing variables


6% reading in assignment scores, midterm and final
1% assignment average calculation
3% number grade calculation
8% if statement for letter grade
-1% for input not matching the data type
-1% for missing & for inputs
-2% for reading in one value for assignment grade
-1% for not dividing the sum of assignment scores by 6
-1~2% for wrong number grade calculation
-2 %for wrong if-else
-4% for using switch

7. (10%) What is the output of the following snippet of code?


int x = 0;
if (x == 0) {
printf("x is true\n");
}
else
printf("x is false\n");
printf("x is invalid\n");

Output:
x is true
x is invalid
10 points for having both
5 points for X is invalid

8. (20%) Write a program that asks the user for a (positive) input and prints out all the
multiples of that input up to 10 * input. For example, if the user inputs 3, it should
output 3, 6, 9, 12, 15,18, 21, 24, 27, 30.
#include <stdio.h>
int main() {
int i = 1, result = 0, input = 0;
printf("Please enter a positive number greater than 0: ");
scanf("%d", &input);
while (input <= 0) {
printf("Input is not greater than 0!\n");
printf("Please enter a positive number greater than 0: ");
scanf("%d", &input);
}
for (i = 1; i < 10; i++) {
printf("%d, ", i*input);
}
printf("%d.\n", i*input);
return 0;
}
You could've also hardcoded all the prints like the following instead of doing a for/while loop.
printf("%d, ", input);
printf("%d, ", 2*input);
printf("%d, ", 3*input);
printf("%d, ", 4*input);
printf("%d, ", 5*input);
printf("%d, ", 6*input);
printf("%d, ", 7*input);
printf("%d, ", 8*input);
printf("%d, ", 9*input);
printf("%d.\n", 10*input);
5% for 1st prompt (printf + scanf combination).
-2% printf, -3% scanf.
5% for enforcing positive input (normally, some kind of while loop to reprompt printf and scanf if
the user enters something less than or equal to 0).
-1% for incorrect condition, -1% for not using while loop, -1% for incorrect scanf.
10% for implementation of print output (did not enforce any restriction on output in terms of
commas, tabs, newlines, etc. as long as they could print the correct sequence).
-1% if sequence is just off at the ends (e.g. starting 0 or printing 1 additional beyond).
-5% for implementation if it completely prints out a different set of numbers.
-6% to -9% depending on how confusing their logic was.
-1% for each incorrect major syntax error (missing & in scanf, incorrect order of comparison (>=
vs =>), etc.)
-1% to -3% for small syntax errors in cumulative amounts (missing semicolons, missing curly

braces, missing parantheses, etc). I did these in incremental (e.g. 2 missing semicolons = -1%, 4
missing semicolons = -2%, etc.)
-3% for a program that is logically incorrect but portions of the parts were there (e.g. infinite loops,
weird for loop statements, portions of code never reached).

You might also like