Sei sulla pagina 1di 6

Name: Student #:

Engineering 1020
Introduction to Programming
Mid-Term Test
S. Alawneh
S. Foote
June 15, 2012
Instructions: Answer all questions. Write your answers to all questions in the space provided on
this paper. You may use the backs of the pages if you need more space.
This is a closed book test, no textbooks, notes, calculators, electronic translators, or other aides
are permitted.
Notes
1. Write your name and student number in the space provided on this sheet. Write your student
number only in the space provided on other sheets of this paper.
2. If you nd a question to be ambiguous or feel that you must make assumptions in order to
complete your answer, please clearly state those assumptions on your paper.
3. Unless otherwise specied you may not make use of standard library functions other than
those in the <cmath> library.
4. With the exception of Question 3, all code on the exam has been compiled and tested.
5. For all questions you may assume that the usual header les are included, as follows:
#include <iostream>
using namespace std;
#include <cmath>
6. Comments and function header block comments are not required unless explicitly asked for.
Q1 / 6
Q2 / 6
Q3 / 5
Q4 / 5
Q5 / 8
Total / 30
1020 Mid-Term Test 1 of 6
Student #:
1. [6 points] For each of the following terms give a brief, concise denition as it is used in this
course.
a) [2 points] Variable
A named location for holding a piece of computer data taken from a set of values.
b) [2 points] Logical expression
A combination of variables, constants and functions which can be progressively reduced to
a single boolean value.
c) [2 points] Function Declaration
A function prototype followed by ; It gives the function return type, name, and the names
and types of all parameters.
2. [6 point]
a) [1 point] Given the following function denition, what is the value of the expression
foo(13, 12, 2)?
i nt f oo ( i nt x , i nt y , i nt z ){
i nt a ;
i f ( x%y > z ){
a = y/z ;
} el se i f ( x/y<z ){
z=z 2;
a=zz ;
}
el se {
a = y%z ;
}
return a ;
}
16
b) [1 point]What does (a,b) do when a and b are positive?
double f i ( double a , i nt b){
i nt c = ( a pow( 10. 0 , b) + . 5 ) ;
return ( ( double) c )/ pow( 10. 0 , b ) ;
}
It rounds a to b decimal places
1020 Mid-Term Test 2 of 6
Student #:
c) [3 points] The call fu(x, y, z) is issued, where x, y & z are all int variables with values of 5,
5 and 20 respectively. What are the values of x, y and z after the call is made?
void f u ( i nt& a , i nt& b , i nt c ){
c = a b ;
i f ( a % b){
a = c + a % b ;
b = a5;
}
el se {
a = c + b % a ;
b = c + a /3;
}
}
x=25
y=33
z=20
d) [1 point] Given the function denition below and that initially a = 10, b = 4 and c = 11,
what is the value of the expression fum(a, b, c)?
bool fum( i nt x , i nt y , i nt z ){
return x > y && ! ( y==x) | | z < x ;
}
True or 1
1020 Mid-Term Test 3 of 6
Student #:
3. [5 points] The program below contains one logical error and four syntax errors. Indicate the
location of each error in the code with a letter, a - e, and on the corresponding line below explain
the error and/or the correction.
i nt e uc l i d ( i nt l ar ge r , bs mal l e r ){
while ( s mal l e r ==a 0)e
cremai nder = l a r g e r % s mal l e r ;
l a r g e r = s mal l e r ;
s mal l e r = remai nderd
}
return l a r g e r ;
}
a) [1 point] Logical error: Condition in while should be smaller != 0.
b) [1 point] Syntax error: Missing type (int) in prototype.
c) [1 point] Syntax error: Missing type (int) of the variable remainder.
d) [1 point] Syntax error: Missing semi-colon at end of statement.
e) [1 point] Syntax error: Missing open brace for the while loop.
1020 Mid-Term Test 4 of 6
Student #:
4. [5 points] We would like a single function that computes the components of a static force, fx,
fy and fz, along the x, y and z axes given the magnitude of the original force, f, and the angles
theta and phi at which it is applied. Create the contract for such a function (that is the header
comment and the function prototype). Do not provide the code for the function (it wont be
marked). You do not have to know any formulae, only that fx, fy and fz can be computed for
any f, theta and phi.
/ forceComponents
@params : f magnitude of f or c e i n Newtons .
t he t a hor i z ont al angl e i n degr ees .
phi v e r t i c a l angl e i n degr ees .
f x t he component of f al ong t he x ax i s i n Newtons .
f y t he component of f al ong t he y ax i s i n Newtons .
f z t he component of f al ong t he z ax i s i n Newtons .

@modi f i es : f x , f y and f z w i l l be s e t by t he f unct i on

@returns : not hi ng
/
void f orceComponents ( double f , double theta , double phi ,
double& f x , double& f y , double& f z )
1020 Mid-Term Test 5 of 6
Student #:
5. [8 points] The Maclaurin series approximation of cos(x) is given by

i=0
(1)
i
(2i)!
x
2i
Write a function, in good C++ code, that returns the n
th
order approximation of cos(x) using
this method (that is, the summation such that the highest exponent of x
i
is n). You need to
provide the function prototype, but you should not write the header comment (it will not be
marked).
double macl auri nCos ( double x , i nt n) {
double sum = 0;
double s i gn = 1;
i nt twoI = 0;
double xTo2I = 1;
double twoIFact = 1;
while ( twoI <= n) {
sum += s i gn xTo2I /twoIFact ;
s i gn = 1;
twoI += 2;
xTo2I = ( xx ) ;
twoIFact = twoI ( twoI 1);
}
return sum;
}
1020 Mid-Term Test 6 of 6