ECE 175/Spring 2015
Midterm Exam #1: Tuesday February 24, 2015
Student name:
______________________________________
Lab session (Date and Time):
______________________________________
1. Only open notes (print out).
2. No computer, calculator and smart/cell phone is allowed.
3. To receive partial credit, show all your computation steps clearly.
Where to write your answers:
4. Use the back of each page as scratch paper. The answers on the back of the page will NOT be graded.
5. Please write your answers in the allotted space/boxes for each question. Do not write your answers
anywhere else! => they will NOT be graded.
6. Read each question carefully, and do not start answering a question before understanding what that
question is asking for.
7. This exam paper has 7 single-sided pages. Please check to make sure you have all the pages NOW!
Problem
Score
1
2
3
out of 20
out of 20
out of 20
4 (Practical Exam)
out of 40
out of 100
Total
Page 1 of 7
ECE 175/Spring 2015
1a) (5 pts) What will the following program print out? Show all your computations in details to
receive (partial) credits.
#include <stdio.h>
int main( void ) {
int x=2, y=17, z=11, result=5;
result = result - (2 * z)% 13 + y / 3 + x;
printf("result = %d\n", result );
return( 0 );
}
Write your answer here:
1b) (5 pts) What will the following program print out when executed?
#include <stdio.h>
int main(void) {
char i = 'A';
do{
printf("%c ", i);
i = i + 2;
}while(i < 'F');
printf("!");
return (0);
}
Write your answer here:
Page 2 of 7
ECE 175/Spring 2015
1c) Given the following program, answer the questions below:
#include <stdio.h>
int main (void){
int x = 10 , y , n , i;
printf("Enter an integer value: " ) ;
scanf ("%d", &n);
y = 100 % n;
i = n;
while(i >= 1){
if (x == y){
printf ("Success!!\n");
}
i--;
}
return 0;
}
1c.1) (5 pts) Which of the following statements are correct about the program?
Choose (circle) the correct answer.
A. Success!! is never printed out, regardless of user input.
B. If the user enters 1 the program will print out Success!! 1 time.
C. If the user enters 90 the program will print out Success!! 10 times.
D. If the user enters 10 the program will print out Success!! 1 time.
E. If the user enters 90 the program will print out Success!! 90 times.
F. If the user enters 10 the program will print out Success!! 10 times.
G. If the user enters 10 the program will print out Success!! 90 times.
H. None of the above
1c.2) (5 pts) Rewrite part of the program below using for loop.
i = n;
while(i >= 1){
if (x == y){
printf ("Success!!\n");
}
i--;
}
Write your answer here:
Page 3 of 7
ECE 175/Spring 2015
2a) (5 pts) What will the following program print out?
#include <stdio.h>
int func1(int i, int j){
int k;
if ( i < 5 )
k = i + j;
else
k = i*j;
return (k);
}
int main (void){
int i = 4 , j = 5 , k=10, l=0;
k = func1(i, j);
l = func1(j, i) ;
printf ("%d, %d", k, l) ;
return 0 ;
}
Choose (circle) the correct answer below:
A. 10, 0
B. 9, 9
C. 9, 20
D. 20, 20
E. 20, 9
F. None of the above
2b) (5 pts) What is the smallest integer, replacing the symbol X below, in the “for” loop so that
the “while” and “for” loops (Program 1 and Program 2 shown below) produce the same final
value for the variable named output?
/* Program 1 using a while loop */
/* Program 2 using a for loop */
int i=12, output=1;
int k, output = -1;
while(i >= 5)
{
output++;
i=i-2;
}
for(k=12; k <= X ; k++)
{
output=output+2;
}
X = _______________
Page 4 of 7
ECE 175/Spring 2015
2c) (10 pts) What will the following program print out when executed?
#include <stdio.h>
int main( void ) {
int i=0, j;
while (i < 3) {
for (j=0; j < 5; ++j ) {
if (j == 2) {
i++;
}
if (i == 2) {
j += 2;
break;
}
printf( "%d %d\n", i, j );
} /*end for*/
i++;
printf( "%d %d\n", i, j );
} /*end while*/
return 0;
} /*end main*/
Write your answer here:
Page 5 of 7
ECE 175/Spring 2015
3a Given the following program, answer the questions on the next page:
#include<stdio.h>
int main(void){
FILE *ofile, *ifile;
double prc;
ifile = fopen("input.txt", "r");
________(I)______ = fopen("output.txt", "w");
if(ifile == NULL){
printf("No input file\n");
}
else{
while( ____________________(II)________________________ ){
if(prc < 0)
fprintf(ofile, "%d ", 0);
else if (prc >= 10 && prc < 20)
fprintf(ofile, "%.2lf ", prc);
}
}
fclose(ifile); fclose(ofile);
return 0;
}
3a.1) (2 pts) If the input.txt is not in the appropriate folder, how does this program handle it (i.e.
what it will do)?
3a.2) (2 pts) Complete the program at (I)
___________________
3a.3) (3 pts) Complete the code at (II) such that one number can be read in from the input.txt
file at a time?
3a.4) (3 pts) Given that the input.txt exists (in the correct folder), if below is the data in the
input.txt file, what will be in the output.txt file?
In the input.txt file,
10 -5.25 123.5 15.42 12.4 -45.5
In the output.txt file:
Page 6 of 7
ECE 175/Spring 2015
3b (10 pts) Given the following program, write a C (user-defined) function that returns
the sum of the following series for n terms:
1
1
1
1
𝑘
𝑘
𝑘
𝑘𝑛
1+ +
2 +
3…+
Note: Write your function appropriately. Look at how the function is called in the main code
given below (bottom of the page).
#include <stdio.h>
#include <math.h>
Write a C (user-defined) function here:
int main( void ) {
double y;
int k, n;
printf("This program is to find summation of series = 1+ 1/k +1/k^2 + …+ 1/k^n: ");
printf("Enter k and n value: ");
scanf("%d %d", &k, &n);
y = series(k, n);
printf("%.5lf\n", y);
return(0);
}
Page 7 of 7
© Copyright 2026 Paperzz