=========================preview======================
(COMP102)final002.pdf
Back to COMP102 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY
Department of Computer Science
COMP 102: Computer and Programming Fundamentals I
Spring 2000
Final Examination
Ren Lan Liao
Date: May 30 2000 Time: 4:30-6:30 pm Venue: Sports Hall
This exam contains 10 questions in 16 pages. Please count the pages.
You have 2 hours to complete this exam.
Problem Your points Max points Problem Your points Max points
1 4 6 14
2 10 7 5
3 10 8 10
4 10 9 6
5 20 10 11
Subtotal 54 Subtotal 46
Your total points 100
Please identify yourself: Lecture/Lab section Name
Student id
Signature
1. (4 points total)
Write an intfunction named outOfOrderthat takes as parameters an array of double and an integer size. This function will test this array for being out of order, meaning that the array violates the condition: a[0] <= a[1] <= a[2] <=
The function returns an integer, it returns C1if the elements are not out of order; otherwise it will return the index of the first element of the array that is out of order. For example, consider the declaration
double a[10]={1.2, 2.1, 3.3, 2.5, 4.5, 7.9, 5.4, 8.7, 9.9, 1.0};
In the array above, a[2]and a[3]are the first pair out of order, a[3]is the first element out of order, so the function returns 3. If the array were sorted, the function would return
-1. #include <iostream.h> int outOfOrder(double A[10], int size)
{ int i;
for ( i =
0; ; i++)
if ( )
break;
if ( )
else return -1; return ;
}
int main()
{
double a[10]={1.2, 2.1, 3.3, 2.5, 4.5, 7.9, 5.4, 8.7, 9.9, 1.0};
cout << outOfOrder(a, 10) << endl;
return 0;
}
2. (10 points) You are given the following binary search function:
#include <iostream.h>
int bsearch(int data[], // input: array
int size, // input: array size
int value // input: value to find
) // output: index if found
{ // otherwise return -1
int i, lower, middle, upper;
lower = 0;
upper = size - 1;
while (true) {
for ( i=lower; i <= upper; i++)
cout << data[i] ;
cout << endl;
middle = (lower + upper) / 2;
if (data[middle] == value)
return middle;
else if (lower >= upper)
return -1;
else if (value < data[middle])
upper = middle - 1;
else
lower = middle + 1;
}
}
if
int A[8] = { 1, 2, 3, 5, 7, 10, 14, 17 };
then what is the output when the following call is made: (In the following grids, each cell should hold one integer, not one digit! )
cout << bsearch(A, 8, 6) << endl;
cout << bsearch(A, 8, 14) << endl;
3. (10 points in total) a)(5 points) Write a function that will replace (or clip) any value above a specified value with the specified value. Assume that the function prototypes is
void clip(double b[], int size, double peak_value);
To illustrate, the following function reference will replace any value above 5.0 in the array x with the value 5.0
Clip(x, n, 5.0)
void clip(double b[], int size, double peak_value)