midterm2_fall2004

advertisement
COEN 243 - Fall 2004
Midterm 2
>> Solve All Questions <<
>> 100 Minutes Allowed <<
>> Marks out of 30 <<
Section A
Q1. Write a program that inputs 6 integers (between 1 and 1000), identifies the prime
numbers amongst them, and then outputs the average of the primes, with two digits after
the decimal point. A prime number is one that divides evenly only by itself and 1. (Note:
1 is considered a prime number).
Test Case 1 (TC1):
TC2:
TC3:
Input
Input
Input
1 22 7 9 120 11
50 11 17 120 2 18
3 9 44 1 12 7
Output 6.33
Output 10.00
Output 3.67
(18 Marks)
Q2. Write a program that reads up to 3 sentences from a user, inputted one sentence at
a time via the keyboard, counts the number of alphanumeric characters found in each of
the sentences, and then outputs them to the screen. Assume that “!” marks the last
input. [Hint: you may wish to use an array of integers in this program.]
Test Case 1 (TC1):
Input
I love Lucy
Have a nice day!
Output 9 12
TC2:
Input
My name is Jon!
Output 11
TC3:
Input
Sugar,
Spice,
Nice!
Output 5 5 4
(12 Marks)
COEN 243 - Fall 2004
Midterm 2
>> Solve All Questions <<
>> 100 Minutes Allowed <<
>> Marks out of 30 <<
Section B
Q1. Write a program that inputs 6 integers, and then checks to see if any combination of
exactly three of the first 5 integers adds up to the last integer.
Test Case 1 (TC1):
TC2:
TC3:
Input
Input
Input
1 –2 4 –1 15 17
3 12 11 –4 16 7
0 12 23 –1 11 11
Output Yes. – 2 + 4 + 15 = 17
Output No.
Output Yes. 0 + 12 + – 1 = 11
(18 Marks)
Q2. Write a program that reads in an input file, containing one line of characters, and
then writes the characters to another output file, in reverse order. The characters will not
exceed 30 in number (including any spaces and special characters). Finally, print the
contents of the output file to the screen. [Hint: you may wish to use an array of
characters in this program.]
Test Case 1 (TC1):
TC2:
TC3:
Input
Input
Input
kkK 0123 kk
hello mummy
uoy evol I
Output kk 3210 Kkk
Output ymmum olleh
Output I love you
(12 Marks)
COEN 243 - Fall 2004
Midterm 2
>> Solve All Questions <<
>> 100 Minutes Allowed <<
>> Marks out of 30 <<
Section C
Q1. Write a program that reads in 9 symbols, where a symbol is an “X”, an “O”, or a
dash “-“. If the first 3 symbols, or the next 3 symbols or the last 3 symbols are either all
X’s or all O’s then the program should output a win for that symbol; otherwise the
program should declare a “no win”. [There will not be a case in which both X and O win
the game.]
Test Case 1 (TC1):
TC2:
TC3:
Input
Input
Input
XOXXOOXXXXXOOXX-X
X-XOXXOOO
Output no win
Output X wins
Output O wins
(18 Marks)
Q2. Write a program that reads a file of words separated by new-lines (<nl>) and
concluded with an end-of-file (<eof>). The program should compute the set of letters that
make up these words (eliminating redundancies), and then print out the results to the
screen, in the manner displayed below. [Hint: you may wish to use an array of
characters in this program.]
Test Case 1 (TC1):
TC2:
TC3:
Input
Input
Input
hello<nl>world<eof>
how<nl>are<nl>you<eof>
come<nl>around<eof>
Output {h,e,l,o,w,r,d}
Output {h,o,w,a,r,e,y,u}
Output {c,o,m,e,a,r,u,n,d}
(12 Marks)
Download