Computer Programming TH - Final-2015

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Time Allowed: 90 Min Final Paper - Computer Programming (MCT-141) 2015-MC-_______

Problem 1 What is the output of the following program segment? [CLO 3][5 POINTS]
Write Answer here:
int day, i;
for(day = 1; day <= 3; day = day + 1){
cout<<"On day "<<day<<" of school, I learnt\n";
for(i = day; i > 0; i = i - 1){
if(i == 1){
if(day == 1){cout<<"A ";}
else{cout<<"And a ";}
cout<<"simple addition question.\n"; }
else if(i == 2){
cout<<"How to say ABCs,\n";}
else if(i == 3){
cout<<"How to draw with crayons,\n";}
}
cout<<"\n";
}
Problem 2 Write down a single C++ statement for each of followings: [CLO 2][6 POINTS]

𝑦 = sin √|𝑥|
Assign to the int variable choice a random value from the set
{10,15,20,25,30}
Declare a type int array named list with 100 elements, such that the first 5
array elements are initialized to contain the values 1, 2, 3, 4, 5, respectively, and
the remaining elements are initialized to 0.
Problem 3 Consider the following function having an array as input parameter. Describe in a single sentence that what the function is calculating. (e.g. It is
finding the minimum value of the array) [CLO 3][3 POINTS]
int final(int first, int last, int a[]){ Write Answer here:
if(first == last)
return a[first];
else
return (a[first] + final(first+1,last,a));
}
Time Allowed: 90 Min Final Paper - Computer Programming (MCT-141) 2015-MC-_______
Problem 4 Assume that a structure named date is defined as: Write Answer here:
[CLO 2][4 POINTS]

struct date{
int month;
int day;
int year;
};
Write a C++ function named compareDates having two date instances
d1 and d2 as input and returns -1 if d1 is an earlier date than d2, 1 if d1 is
a later date than d2, and 0 if d1 and d2 are the same. Assume d1 and d2
contain valid dates.

Problem 5 Consider following two functions: [CLO 4][4 POINTS] Write Answer here:
int mystery1 ( int a[], int size, int x ){
int n=-1;
for (int i = size - 1; i >= 0; i--){
if ( a[i] == x ){n=i; break;}
}
return n;
}
int mystery2 ( int a[], int size, int x ){
int n=-1;
for (int i = 0; i < size; i++ ){
if ( a[i] == x ){n=i;break;}
}
return n;
}
Do both of functions perform the same task? Comment whether their outputs will always be same for the same input parameters.

Problem 6 An array named values of size n is symmetric if values[0] is equal to values[n-1], values[1] is equal to values[n-2], and so
on. For example, the elements in the following array are symmetric: int list1[] = {1, 4, 5, 4, 1};
Time Allowed: 90 Min Final Paper - Computer Programming (MCT-141) 2015-MC-_______
The elements in the following array are not symmetric: int list2[] = {4, 6, 8, 9, 9, 8, 5, 4};
Write a complete C++ function named isSymmetric that has an array of integers and the size as input and returns the Boolean value true if the elements
of array are symmetrical and returns false if they are not symmetrical. [CLO 3][6 POINTS]

Start writing in this box: Continue Here:

Problem 7 Write a C++ function named cocktailSort, the prototype of which is given below, that has two parameters. The parameter array is an integer
array and the parameter size gives the number of elements in it. The function sorts the given array in ascending order, following a slight variation of the
bubble sort algorithm, called the Cocktail sort. Instead of repeatedly passing through the list from left to right as in each iteration of bubble sort, Cocktail sort
passes alternately from left to right and then from right to left. Similar to bubble sort, the sorting algorithm stops as soon as it is detected that the array is
sorted. [CLO 4][6 POINTS]

void bubble_sort(int array[], int size){ void cocktail_sort(int array[], int size){
int temp;
int swap;

do
{
swap = 0;
for (int count = 0; count < (size - 1); count++){
if (array[count] > array[count + 1]){
temp = array[count];
Time Allowed: 90 Min Final Paper - Computer Programming (MCT-141) 2015-MC-_______
array[count] = array[count + 1];
array[count + 1] = temp;
swap = 1;
}
}
}
while (swap==1);
}

Problem 8 To make telephone numbers easier to remember, some companies use letters to show their telephone number. For
example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number
meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight
letters. Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding
telephone number in digits. If the user enters more than seven letters, then process only the first seven letters. Also output the –
(hyphen) after the third digit. Assume that user will enter only UPPER case letters and he can enter space bar as well.
[CLO 3][6 POINTS]

You might also like