Anda di halaman 1dari 8

Ryerson University

Department of Computer Science


CPS125 Winter 2009
Final Exam
April 27, 2009 12 p.m. - Duration: 120 minutes
Family Name: ______________ Given Name: ______________
Student #: ________________ Section number : _________
Your Ryerson E-mail: ______________________@ryerson.ca
Seat Number: _______
Circle your professor's name (note penalty below)
Davoudpour

Gajderowicz
Moore
Panar

Hamelin
Kokkarinen
Timorabadi

Read all instructions:


-> READ A QUESTION CAREFULLY AND COMPLETELY BEFORE ASKING ABOUT IT.
-> All bags and communication devices must be placed at the front of the room. No
calculators or any documents are allowed. No hats that have visors, hoodies nor coats are
allowed at your seat. Put those at front of the room as well.
-> The exam has 5 questions and a duration of 120 minutes. Stop writing and put down your
pen when instructed at the end of the test.
-> A 2 mark penalty will be applied for incorrectly filled in section number and/or
failure to circle your professor's name.
-> Have your photo ID visible at all times during the test.
-> Use meaningful variable names in all programs (speed, colour, weight are meaningful;
a, x, c4 are not). Penalties up to 2 marks per question may be applied for bad naming.
-> Penalties up to 2 marks per question may be appplied for extraneous (unnecessary) code.
-> Use proper indenting in all programs. Do not write any /*comments*/.
I have read and understand the instructions. Signature:_______________________________

Q1

Q2

FOR OFFICE USE ONLY


Q3
Q4
Q5
Penalties

TOTAL

22

Do not open the exam until instructed

cps125-w09-final

Question 1 (5 marks):
Write a complete C program (main only) that will accept a
command line argument consisting of a word followed by two
integer numbers. The word can be either sum or average. If
the word is sum, the program will print out the sum of the
two integers and if the word is average, it will print out
the average of the two integers with one decimal precision.
Assume that the correct arguments are always supplied and
separated by a single space. For example if the command
line arguments are sum 4 13, the program will print out 17.
If the command line arguments are average 7 4, the program
will print out 5.5.

cps125-w09-final

Question 2 (5 marks):
Complete the function only (no main program) that takes two
square matrices of integers (up to 50x50 in size,
statically-allocated) and calculates the scalar product of
the two main diagonals. For example, for the two following
matrices,
1 2 3
9 1 2
4 5 6
3 4 5
7 8 9
6 7 8
the scalar product would be 1x9 + 5x4 + 9x8 = 101. Note
that the scalar product will be returned to the main, not
printed out in the function.
int
scaldiag
{

return (

);

}
Indicate the call to the function in the main program
assuming the matrices are called mat1 and mat2 (size
20x20) and the scalar product is called sp.

_____________________________________

cps125-w09-final

Question 3 (5 marks):
Write a complete program (main only) that will read book
titles from a file called book.dat. The format of the file
is: The first data is an integer indicating how many words
will follow (no words are longer than 15 letters). We do
not know how many lines are in the file. Here is a typical
file:
5
3
5
4
2

the left hand of darkness


the lost world
two years before the mast
peter pan and wendy
moby dick

Your program must output only the words that have exactly
five (5) letters, displaying each word on a separate line.
In the preceding file example, your program would print out
world
years
peter
wendy
Do not use arrays of strings (2D arrays).
You may use either redirection (scanf) or FILE I/O (fscanf).

Continue on next page if necessary...

cps125-w09-final

Continue question 3 here if necessary...

cps125-w09-final

Question 4 (5 marks):
Write a complete C program (main only) that asks the user
for the size of an array of integers, allocates dynamically
a one-dimensional array of that size and then fills the
array by asking the numbers at the keyboard (use proper
prompt for each entry). Your program will then traverse the
array to check if two adjacent cells contain the same
number. If you find some, print out the cell numbers and
the value.
For example if the array contains the values
34,66,77,99,55,55,44,44,44,22 the program will print
Cells 4 and 5 contain 55.
Cells 6 and 7 contain 44.
Cells 7 and 8 contain 44.

Continue on next page if necessary...

cps125-w09-final

Continue question 4 here if necessary...

cps125-w09-final

Question 5 (2 marks):
What is the exact output of this program?
#include <stdio.h>
typedef struct
{
int a, b;
}abc;
abc
func1 (abc fred, int wilma, int *dino)
{
fred.a = wilma + 1;
while (wilma > 0)
{
fred.b = *dino + wilma + 1;
wilma = wilma - 1;
*dino = *dino + 1;
}
return (fred);
}
int
main (void)
{
abc x;
int y=2, z=3;
x = func1 (x, y, &z);
printf ("%d %d\n", x.a, x.b);
printf ("%d %d\n", y, z);
return(0);
}

___________________________________
___________________________________

cps125-w09-final

Anda mungkin juga menyukai