Vous êtes sur la page 1sur 5

MAKERERE UNIVERSITY

FACULTY OF TECHNOLOGY

DEPARTMENT OF MECHANICAL ENGINEERING

MEC 2203: COMPUTER PROGRAMING

MARKING GUIDE

TEST TWO DONE FRIDAY 16TH APRIL 2010

1
Problem One
(a) Define structured programming and list its three structures.
Structured programming (sometimes known as modular programming) is a subset of
procedural programming that enforces a logical structure on the program being
written to make it more efficient and easier to understand and modify. A program
therefore no longer consists of only one single part. It is now divided into several smaller
parts which interact through procedure calls and which form the whole program. The
three structures of structured programming are sequence, selection and repetition.

(b) Using an appropriate example, define a post-test loop.


A post test loop is that in which the expression is tested after the loop body. An example
is a do while loop (student required to write any do… while loop).

(c) Write the statements, including a loop required to calculate y(t) from the following
equation:

for values of t between -9 and 9 in steps of 3. Display each value of t and y(t). Note: You
do not need to write a complete program but you should include declaration
statements for all the variables that you use.

double t, y, increment;
t = -9.0;
increment = 3.0;
while( t <= 9.0)
{
if( t >= 0.0 )
y = -3.0*t*t + 5.0;
else
y = 3.0*t*t + 5.0;
cout << t << " " << y << endl;
t += increment;
}

Problem Two
(a) Observe the following code fragment and identify the possible error with it.

unsigned short SomeArray[5][4];


for (int i = 0; i<=5; i++)
for (int j = 0; j<=4; j++)
SomeArray[i][j] = 0;

The programmer intended to write i<5, but instead wrote i<=5. The code will run when i
== 5 and j == 4, but there is no such element as SomeArray[5][4].

2
(b) Consider a 2-by-3 integer array b. Write a series of C++ statements that
determine and print the smallest value in array b.

int smallest = b[ 0 ][ 0 ];
for ( int r = 1; r < 2; ++r )
for ( int c = 1; c < 3; ++c )
if ( b[ r ][ c ] < smallest )
smallest = b[ r ][ c ];
cout << smallest;

(c) Consider the following expressions. State whether each of the following
expressions is true or false and explain why?

1. !(p||q) is the same as ! p||!q


! (p||q) is not the same as !p||!q; for example, if p is true and q is false,
the first expression will be false but the second expression will be true. The
correct equivalent to the expression !(p||q) is the expression !p&&!q.
2. !!! p is the same as !p
!!!p is the same as ! p.
3. p&&q||r is the same as p&&(q||r)
p&&q||r is not the same as p&&(q||r) ; for example, if p is false and r is true,
the first expression will be true, but the second expression will be false:
p&&q||r is the same as (p&&q)||r.

Problem Three
Using relevant examples, explain briefly what you understand by the terms

Function overloading,
Function overloading involves having a function with the same name as another
function, but with different parameter types as shown in the function declarations
below.

int Add(int nX, int nY); // integer version


double Add(double dX, double dY); // floating point version

The function Add is used to carry out two addition of both integer and double type
variables.

Inline function
An inline function is used to request the compiler to replace each call to a function with
explicit code for the function. To the programmer, an inline function appears the same
as an ordinary function, except for the use of the inline specifier as shown below.

inline int cube(int n)


{
return n*n*n;
}

3
Operator precedence
Operator Precedence is the order in which a program performs the operations in a
formula. If one operator has precedence over another operator, it is evaluated first. For
example the post fix increment has higher precedence (++) than the post fix
decrement (--) and the multiply operator (*). Any other example from the student is
allowed

(d) Engineers often measure the ratio of two power measurements in decibels, or dB.
The model for decibels is

Where P2 is the power level being measured and P1 is some reference power level.
Considering the two power levels, write the definition of a function named decibel to
compute the decibel level using the above model. Use function log10 to calculate the
logarithm.

double decibel(double P1, double P2)


{
return 10.0*log10(P2/P1);
}

Assuming that the reference level P1 is 1 watt, write C++ statements, including a loop,
that uses the function decibel to calculate and display on the terminal screen the
decibel levels corresponding to power levels between 1 and 20 watts in 0.5 watt steps.
Note: You do not need to write a complete program but you should declare any
variables that you use.
double p1, p2, dB;
p1 = 1.0;
for(p2 = 1.0; p2 <= 20.0; p2+=0.5)
{
dB = decibel(p1, p2);
cout << dB << endl;
}

Problem Four
(a) What is the difference between the statements const int * ptr_One and int *
const ptr_Two?
The const int * ptrOne declares that ptrOne is a pointer to a constant integer. The integer
itself cannot be changed using this pointer. The int * const ptrTwo declares that ptrTwo is a
constant pointer to an integer. Once it is initialized, this pointer cannot be reassigned.

(b) Clearly study the following code and identify the possible error with it and
suggest a solution.

short a[32];
for (int i = 0; i< 32; i++)
*a++ = i*i;

4
The only problem is that the array name a is a constant pointer, so it cannot be
incremented. The following modified code would be okay:

short a[32];
short* p = a;
for (int i = 0; i < 32; i++)
*p++ = i*i;

(c) Explain why the following three conditions are true for an array a and an int i:

a[i] == *(a + i);


*(a + i) == i[a];
a[i] == i[a];

The value a [ i ] returned by the subscripting operator [ ] is the value stored at the
address computed from the expression a + i. In that expression, a is a pointer to its base
type T and i is an int, so the offset i*sizeof (T) is added to the address a. The same
evaluation would be made from the expression i + a which is what would be used for i [
a] .

(d) Discuss the possible shortcomings of the following code?

#include <iostream>
using namespace std;
int main()
{
int *pInt;
*pInt = 9;
cout << "The value at pInt: " << *pInt;
cin.get();
cin.ignore();
return 0;
}

pInt should have been initialized. More importantly, because it was not initialized and
was not assigned the address of any memory, it points to a random place in memory.
Assigning 9 to that random place is a dangerous bug.

Vous aimerez peut-être aussi