points integer

advertisement
Intro to Computers & Programming (V22.0002-003)
Midterm Exam #1
October 25, 2001
1. (25 points) Write a complete C program that prompts the user for an integer between 1 and 999. The
program should then print out all the integers (in descending order) that evenly divide into that integer and
then prompt the user for another integer to test. If the user does not enter a valid integer between 1 and
999, the program should display a message that the input was not valid and repeat the prompt. The
program must continue to prompt the user for valid integers until the user enters the sentinel value of 0 to
terminate the program.
Sample Run:
Enter an integer between 1 and 999 (0 to end program): 1257
*** 1257 is not a valid integer for this program ***
Enter an integer between 1 and 999: 20
20 is evenly divided by 20 10 5 4 2 1
Enter an integer between 1 and 999 (0 to end program): -5
*** -5 is not a valid integer for this program ***
Enter an integer between 1 and 999 (0 to end program): 25
25 is evenly divided by 25 5 1
Enter an integer between 1 and 999 (0 to end program): 0
*** END OF PROGRAM ***
#include <stdio.h>
int main()
{
int number, divisor;
/* prompt for an integer to test */
printf("\n\nEnter an integer between 1 and 999 (0 to end program): ");
scanf("%d", &number);
while (number != 0)
{
if ( (number >= 1) && (number <= 999) )
{
/* integer in valid range has been entered */
printf("%d is evenly divided by ", number);
/* test all divisors from number down to 1 */
for (divisor = number; divisor >= 1; divisor--)
if ( (number % divisor) == 0)
printf("%d ", divisor);
}
else
/* invalid integer was entered */
printf("*** %d is not a valid integer for this program ***", number);
/* prompt for another integer to test */
printf("\n\nEnter an integer between 1 and 999 (0 to end program): ");
scanf("%d", &number);
}
printf("*** END OF PROGRAM ***\n");
return 0;
}
Intro to Computers & Programming (V22.0002-003)
Midterm Exam #1
October 25, 2001
2. (25 points) Here is the grading scheme for a fictional class taught at NYU:
A: 90 to 100
B: 80 to 89
C: 70 to 79
D: 64 to 69
F: 0 to 63
Write a complete C program that prompts the user to enter 30 integer grades from 0 to 100. After the
user enters the 30th grade, the program must then display the number of A’s, B’s, C’s, D’s and F’s for
the class. The program must use both a for loop and appropriate if and/or if/else statements; do
NOT use while loops, do/while loops or switch statements in your answer. Assume the user enters a
valid grade each time. Do NOT use 30 different variables to capture each of the 30 integer grades.
#include <stdio.h>
#define NUM_OF_EXAMS 30
int main ()
{
int exam, score;
int A=0, B=0, C=0, D=0, F=0;
for (exam = 1; exam <= NUM_OF_EXAMS; exam++)
{
/* prompt user for score */
printf("Enter grade for Exam #%d: ", exam);
scanf("%d", &score);
if (score >= 90)
A++;
else if (score >= 80)
B++;
else if (score >= 70)
C++;
else if (score >= 64)
D++;
else
F++;
} /* end of for loop */
/* Display results */
printf("Number of As: %d\n", A);
printf("Number of Bs: %d\n", B);
printf("Number of Cs: %d\n", C);
printf("Number of Ds: %d\n", D);
printf("Number of Fs: %d\n", F);
return 0;
}
Intro to Computers & Programming (V22.0002-003)
Midterm Exam #1
3. (20 points) What output does the following C program produce? Show all work.
#include <stdio.h>
#define TEN 10
int main()
{
int x=100, y=100;
x = 0;
while (x <= TEN)
for (y=1; y <= TEN; y*=2)
printf ("%d %d\n", x++, y);
printf ("x = %d\n", x);
return 0;
}
x
0
1
2
3
4
4
5
6
7
8
8
9
10
11
12
x <= TEN
T
T
T
y
1
2
4
8
16
1
2
4
8
16
1
2
4
8
16
y <= TEN
T
T
T
T
F
T
T
T
T
F
T
T
T
T
F
printf x++ y
01
12
24
38
41
52
64
78
81
92
10 4
11 8
x = 12
October 25, 2001
Intro to Computers & Programming (V22.0002-003)
Midterm Exam #1
October 25, 2001
4. (15 points) There are at least 5 syntax errors in the C source code listed below. Identify the line number
and the corresponding error for each of the 5 syntax errors you find.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
-
include <stdio.h>
define LIMIT 10
int main()
{
int row=1; col=1;
for (row = 0; row <= LIMIT; row++)
for (col = 0, col <= LIMIT; col++)
if (row <> col)
printf(“%d\n”, row * col);
return 0;
}
line 1. Missing # in front of the include preprocessor directive
line 2. Missing # in front of the define preprocessor directive
line 5. Semi-colon should be a comma
line 7. Comma should be a semi-colon
line 8. <> is not a valid operator; should be !=
Intro to Computers & Programming (V22.0002-003)
Midterm Exam #1
October 25, 2001
5. (15 points) What is displayed when each of the following code fragments execute? Show all work.
a)
int a=5, b=10, c=15, d=20;
if ( ( (d - c) == (b - a) ) && ( (d - b) == (c – a) ) )
printf (“FALSE”);
else
printf (“TRUE”);
(d – c) is (20 – 15) is 5 AND (b – a) is (10 – 5 ) is 5, so first condition is TRUE.
(d – b) is (20 – 10) is 10 AND (c – a) is (15 – 5) is 10, so second condition is TRUE.
TRUE && TRUE is TRUE, so overall condition is TRUE, and therefore FALSE is printed.
b)
int result = 30;
result = result % 12 % 3;
printf (“%d”, result);
result = 30 % 12 % 3  30 % 12 is 6  6 % 3 is 0. 0 is printed.
c)
float result = 30.0;
result = 30 / 100;
printf (“%5.2f”, result);
30 / 100 is integer division (even though result is a float variable). Therefore, 0.00 is printed.
d)
int result = 51;
result = result * 2 - 3 / (10 + 1);
printf (“%d”, result);
result = 51 * 2 – 3 / (10 + 1) = 102 – 3/11 = 102 – 0 (remember integer division) = 102. 102 is printed.
e)
result
1
2
4
8
int result = 1, x = 2, y = 3;
while (y-- > 0)
result *= x;
printf (“%d”, result);
x
2
2
2
2
Therefore, 8 is printed.
y
3
2
1
0
y-- > 0
T
T
T
F
result *=x
result = 2* 1 = 2
result = 2 * 2 = 4
result = 4 * 2 = 8
Download