SULIT PEPERIKSAAN AKHIR SEMESTER 1 SESI 2006/2007 KNL 1353 PEMPROGRAMAN BERSTRUKTUR NOVEMBER 2006 ____________________________________________________________________________________________________________

Part I [20 marks] Answer True or False. Each question carries 1 mark. 1. 2 3 4. In general, C++ statements are case sensitive. A C++ statement must be terminated with period. The statement cout << “/n/n/n.”; will create 3 blank lines. The statement cout <<setioflags (ios:left) << setw(8) << setfill(%) << X displays 123%%%%%. The operand of the % operator must have integer type. If a=5, then a is equal to 6 after a++; is executed, but (with a=5) a is still equal to 5 after ++a; is executed. You must create a link between an external disk file and an ifstream object before you can read your input data. The statement cin>> “a” >> “b”; will store the first and second keyboard input data to variables a and b. Assuming a is 100, determine whether each logical expression that follows is true of false a= =100 | | a >100 && !a The case constants with a switch statement must be arranged in sequence, such as 101,102, 103 and so forth. Some programmers do not break out of for loops. In C++, day* = 5 may be used as a third expression in a for loop. An object of a class (with the class defined at the beginning of a program outside the body of any function) can be declared only in member functions. A constructor function is called automatically when object is declared. One reason for defining a function is to avoid writing the same group of C++ statements over and over again. A correctly written int type function will return an int type value to the calling function. In function call, if one of the argument is an array name not followed by brackets, then this passes the entire array to the function by copying each element into the memory region for the function.

5 6.

7.

8.

9.

10.

11. 12. 13

14. 15.

16.

17.

________________________________________________________________________
ROHANA BINTI SAPAWI 1/5 SULIT

19. int b) { int remainder. b=remainder.cc). A partially filled multidimensional array has all its filled memory positions together at the beginning of the reserved block of memory. } [8 marks] ________________________________________________________________________ ROHANA BINTI SAPAWI 2/5 SULIT .bb). Array d[2] [3] contains six elements: d[0]. 20. while(1) { remainder = a % b. } } cout<<"The greatest common diviser of "<< input_a <<" and " << input_b <<" is "<<b<<endl. divisor.SULIT PEPERIKSAAN AKHIR SEMESTER 1 SESI 2006/2007 KNL 1353 PEMPROGRAMAN BERSTRUKTUR NOVEMBER 2006 ____________________________________________________________________________________________________________ 18. greatest_divisor(divisor. d[3]. int b). All elements of a given array have the same data type. int main( ) { int aa=56. bb=168. d[4] and d[5]. input_b=b. d[1]. divisor=greatest_divisor(aa. if (remainder = =0) break. d[2]. cc=16. else { a=b. a. input_a=a. Part II Answer ALL questions Question 1 Write the expected output for the following c++ statement below. #include <math> #include <iostream> int greatest_divisor(int a. } int greatest_divisor(int a.

#include <iostream> using namespace std. j= "<<j<<". j++) { k = i+j. int main ( ) { int i. int main( ) { int friends_birthdays=0. j<=4. cout<<"i= "<<i<<". for (i=1. } m=k+i. void april( ). cout<<”After call to april( )”<<endl. cout<<”days_in_month = “<<days_in_month<<endl <<”friends_birthdays =“<<friends_birthdays<<endl<<endl.SULIT PEPERIKSAAN AKHIR SEMESTER 1 SESI 2006/2007 KNL 1353 PEMPROGRAMAN BERSTRUKTUR NOVEMBER 2006 ____________________________________________________________________________________________________________ b. days_in_month=30. k. m. } void april( ) { int friends_birthdays=2. } } [8 marks] ________________________________________________________________________ ROHANA BINTI SAPAWI 3/5 SULIT . april( ). cout<<”days_in_month = “<<days_in_month<<endl <<”friends_birthdays =“<<friends_birthdays<<endl<<endl. k= <<k<<endl. j. i+=2) { for (j=1. } [7 marks] c. int days_in_month = 31. cout<<”Before call to april( )”<<endl. i<=5. #include <iostream> using namespace std.

This program has a large number of errors. you will get better at debugging. As the program more and more. you will \\ get better at debugging. \\********************** Name: Application 2 Purpose: Learning debugging skills \\********************** include <iostrem> using namespace standard int main ( ) } cout >> DEBUGGING EXAMPLE:/n/n.SULIT PEPERIKSAAN AKHIR SEMESTER 1 SESI 2006/2007 KNL 1353 PEMPROGRAMAN BERSTRUKTUR NOVEMBER 2006 ____________________________________________________________________________________________________________ Question 2 Debug the program until it produced the following output. /*********************************************** ** Put a banner on your programs ************************************************ #include (iostream) using namespace std void <main>void [ cout<<" This program has a \n large number of errors.".) ] [6 marks] b.\n’ . Use the method we described to fix it. cot>> find the error of this program>>endl [6 marks] ________________________________________________________________________ ROHANA BINTI SAPAWI 4/5 SULIT . cot (As you program more and more. Debug the program and rewrite the program until no errors. cout<<"\nUse the method we \ described to fix it. cout >> “this is an example of debugging. a.

Use the pattern in writing your program.75 7. Three of the triangles are shown in Figure 1. b and c are given coefficients and x is unknown.0 [25 marks] . where a. The solution(s) to the quadratic equation is given by the quadratic formula: x= − b ± b 2 − 4ac 2a Note: This equation is not apply when a is zero and formula also fail to work if expression is under the square root is negative. 7.End of Question - ________________________________________________________________________ ROHANA BINTI SAPAWI 5/5 SULIT .0 3.0 . The coefficients are real number inputs. 6. State clearly outlines all the steps needed before you do the program.5 5.SULIT PEPERIKSAAN AKHIR SEMESTER 1 SESI 2006/2007 KNL 1353 PEMPROGRAMAN BERSTRUKTUR NOVEMBER 2006 ____________________________________________________________________________________________________________ Question 3 Write a program that solved quadratic equations.0 1. You should deduce the dimensions of the fourth triangle from the pattern exhibited by the first three. [20 marks] Question 4 Write a program that computes the areas of four right triangles. A quadratic equation is an equation of the form ax2 + bx + c = 0.

Sign up to vote on this title
UsefulNot useful