Introduction to Computers & Programming (V22.0002-002)
Midterm Exam #1 - Monday, July 16, 2001
Total Number of Points: 30
1) (6 points) Do each of the following C programs compile and execute properly?
 If no, explain the reason it does not. If yes, display the result of executing the program.


a) 
#include <stdio.h>
int main()
{     int gender = 2;
gender--;
if (Gender == 1)
	printf("The student is male\n");
       else
	printf("The student is female\n");
       return 0;
}
b)
#include <stdio.h>
int main()
{     int gender = 2;
gender--;
if (gender == 1)
	printf("The student is male\n");
       else;
	printf("The student is female\n");
       return 0;
}
c)
#include <stdio.h>
int main()
{     int gender = 2;
if (--gender == 1)
	printf("The student is male\n");
       else
	printf("The student is female\n");
       return 0;
}


d)
#include <stdio.h>
int main()
{     int gender = 2;
if (gender-- == 1)
	printf("The student is male\n")
       else
	printf("The student is female\n");
       return 0;
}

e)
#include <stdio.h>
int main()
{     int gender = 2;
gender--;
if (gender == 1);
	printf("The student is male\n");
       else
	printf("The student is female\n");
       return 0;
}

f)
#include <stdio.h>
int main()
{     int gender = 2;
gender = ((gender / 2) % 2) + 0.9;
if (gender == 1)
	printf("The student is male\n");
       else
	printf("The student is female\n");
       return 0;
}
ANSWERS:
a) This program does not compile because Gender is an undeclared variable (C is case sensitive)
b) Both printf statements are executed because the else portion of the if/else statement
 is immediately followed by a semi-colon; thus, the else clause executes an empty statement.
	The student is male
	The student is female
c) Since the program uses the predecrement operator, the first printf statement is executed.
	The student is male
d) This program does not compile due to the missing semi-colon after the first printf statement.
e) This program does not compile because the dangling else clause is caused
 by the semi-colon in if (gender == 1);
f) The arithmetic expression results in the variable gender being assigned the value of 1;
 thus, the first printf statement is executed.
	The student is male
2)	(8 Points) Write a complete C program that prompts the user for an integer between 1 and 1,000.
The program should then print out all the integers that evenly divide into that integer.
Example:
Enter an integer between 1 and 1,000: 20
20 is evenly divided by 1 2 4 5 10 20
ANSWER:
#include <stdio.h>
int main()
{
	int num, counter;
	printf("Enter an integer between 1 and 1,000: ");
	scanf("%d", &num);
	printf("%d is evenly divided by ", num);
	for (counter = 1; counter <= num; counter++)
		if ((num % counter) == 0)
			printf("%d ", counter);
	printf("\n");
	return 0;
}
3) 	(3 Points) What does the following C program display when executed? Show all work 
(i.e., show the contents of the variables for each iteration of the for loop) in order to be 
eligible for partial and full credit for this problem.
#include <stdio.h>
int main()
{	int num1 = 0, num2 = 0;
	for (num1 = 65, num2 = 90; num1 <= num2; num1 += 2, num2  -= 2)
		printf("%c", (num1 + 2));
	return 0;
}
ANSWER: (Use ASCII Code Table to properly answer this question).
num1	num2	(num1 + 2)	printf
65	90	67		C
67	88	69		E
69	86	71		G
71	84	73		I
73	82	75		K
75	80	77		M
77	78	79		O
79	76	DROP OUT OF LOOP
CEGIKMO is displayed on the screen.
		
4)   (3 Points) Rewrite the program in Question 3 above using a while Repetition Structure.
ANSWER:
#include <stdio.h>
int main()
{	int num1 = 0, num2 = 0;
	num1 = 65;
	num2 = 90;
	while( num1 <= num2)
	{	printf("%c", (num1 + 2));
		num1 += 2;
		num2 -= 2;
	}
	return 0;
}
	
5) (5 Points) The following C program contains 5 syntax errors. Identify the line number and the error.
1. include <stdio.h>
2. int main();
3. {
4.      int row; col;
5.      char c;
6.      for (row = 0; row <= limit; row++)
7.           for (col = 0,  col <= 25; col++)
8.                scanf("%c", &c);
9.      return 0;
10. }
ANSWER:
- missing semi-colon on line 1
- semi-colon on line 2
- semi-colon on line 4 between row and col should be a comma
- limit is an undeclared variable on line 6
- comma on line 7 should be a semi-colon
6) (5 Points)  Write a complete C program that prompts the user to enter a series of integers 
terminated by a 0. The program should then print:
* The number of integers entered (not including the 0)
* The average of the integers (with 1 decimal point of precision)
* The maximum integer
* The minimum integer
Example:
Enter a series of integers terminated by a 0:
85  95  75  67  0 
You have entered 4 integers.
The average is 80.5
The maximum is 95
The minimum is 67
ANSWER:
#include <stdio.h>
int main()
{
	int num, min, max,
		counter = 0,
		sum = 0;
		
	printf("Enter a series of integers terminated by a 0:\n");
	scanf("%d", &num);
	while (num != 0)
	{
		if (counter == 0) /* assign max and min to value of first number entered */
		{
			max = num;
			min = num;
		}
		else
		{	if (num > max)
				max = num;
			if (num < min)
				min = num;
		}
		counter++;
		sum += num;
		scanf("%d", &num);
	} /* end of while */
	printf("\nYou have entered %d integers\n", counter);
	if (counter > 0)
	{
		printf("The average is %.1f\n", (float)sum/counter);
		printf("The maximum is %d\n", max);
		printf("The minimum is %d\n", min);
	}
	return 0;
}