# Computer programming Final Exam – Addis Ababa University

Note:

Except the questions which require writing code, assume that all the required header files and namespaces are included

For question which require identifying and programming bugs, it is not expected to write the entire code, rather writing the number in which the error located, what type of error it is and the debugging option (correction) of that error is enough. Presenting your answer in a tabular form is highly encouraged

Part I. Explain the following in detail:

1. Explain in detail about the major components of a computer system

2. List and briefly explain the different types of data types

3. What us the difference between a break and continue statement?

Part II. The programs have one or more syntax, runtime or logical errors or even the combination of all the errors. So, identify all errors in the program, and propose your debugging mechanism

5.

1. int main () //program to calculate the body mass index of a person
2. {   float height, weight, bmi;
3.               bmi =  weight/(height*height);
5.      cin>>height;
7.               cin>weight;
8.               cout<<”your body mass index is”<<bml;
9. return 0;
10. }

6.

1. int main()
2. {
3.     /* program intended to convert
4.          the Celsius scale to the
5.           corresponding Fahrentine scale */
6.     double tF = 0;
7.     double tC == 0;
8.     const double conversionFactor;
9.     cout<<”Enter the conversion Factor : “;
10.     cin>>conversionFactor;
11.     cout<<”Enter the TC value: “;
12.     cin>>tC;
13.     conversionFactor *tC+32=tF;
14.     cout<<”The farhanite equivalent of “
15.              <<tC<<”is”<<tF<<endl;
16.     return 0;
17. }

7.

1. int main ()
2. {
3.       char name [10];
4.       String father_name;
5.       char full_name [10];
6.       name= “Abebe”;
7.       father_name = “kebede”
8.       full_name = name + father_name;
10.       return 0;
11. }

Part III. Determine the outputs of the following C++ Program

8.

1. int function(int a, int* b, int& c)
2. {
3.     a++;
4.     (*b)++;
5.     c++
6.     return a;
7. }
8. int main()
9. {
10.     int a = 10;
11.     int b = 10;
12.     int c = 10;
13.     int d = 10;
14. function(a, &b, c);
15.    cout<<a<<” “<<b<<” “<<c<<” “<<d<<endl;
16. function(c, &d, a);
17.    cout<<a<<” “<<b<<” “<<c<<” “<<d<<endl;
18. c = function(b, &a, d);
19.    cout<<a<<” “<<b<<” “<<c<<” “<<d<<endl;
20. a = function(a, &a, a);
21.    cout<<a<<” “<<b<<” “<<c<<” “<<d<<endl;
22. return 0;
23. }

9.

1. int main()
2.   {
3.       for(int i=0; i<7;i++)
4.          {
5.             for(int j=0;j<i;j++)
6.              {
7.                 if ( ( j%2 ) ==0) {
8.                       cout<<”- “<<j<<”-“;
9.                       continue;
10.                  }
11.                  else
12.                        cout<<i+j);
13.               }
14.                   cout<<endl;
15.           }
16. }

Part IV. Write a c++ code for question 10 – 12. The code shall be neat well formatted and shall include all the appropriate headers and namespaces

10. Write a C++ program that takes two numbers from the user and computes their sum, difference, product or division of the two numbers after asking the user what kind of computation they require to do.

11. In mathematics, greatest common divisor (gcd) of two numbers is the largest positive Integer that divides each of the integers. One way to implement it is the gcd of x and y, where x>y is the same as the greatest common divisor of x and (x remainder y) until the remainder becomes zero. The last remainder before zero (the last y) becomes the gcd. For example, gcd of 688 and 12 is gcd of 12 and 4 because 4 is the remainder of 688 and 12. Finally gcd of 12 and 4 is 4 because the remainder is now 0. Write a c++ Program that implements the gcd function recursively.

12. Write a C++ program that accepts array of character of size 50 from the user, iterate through the array until the null character is found and print out the reverse of the given string