Beruflich Dokumente
Kultur Dokumente
Gajderowicz
Moore
Panar
Hamelin
Kokkarinen
Timorabadi
Q1
Q2
TOTAL
22
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
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).
cps125-w09-final
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.
cps125-w09-final
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