0% found this document useful (0 votes)
2 views

Answers of Midterm - COM253 - Fall 2023

The document contains model answers for the midterm exam of the Engineering Computer Programming module (COM253) for Fall 2023. It includes programming questions related to C++ such as calculating the volume and surface area of an ellipsoid, developing functions for specific outputs, and identifying errors in code snippets. The exam format allows for a scientific calculator, a disconnected PC, and a VC++ compiler, with specific instructions on answer submission.

Uploaded by

mou.msd.50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Answers of Midterm - COM253 - Fall 2023

The document contains model answers for the midterm exam of the Engineering Computer Programming module (COM253) for Fall 2023. It includes programming questions related to C++ such as calculating the volume and surface area of an ellipsoid, developing functions for specific outputs, and identifying errors in code snippets. The exam format allows for a scientific calculator, a disconnected PC, and a VC++ compiler, with specific instructions on answer submission.

Uploaded by

mou.msd.50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Faculty of Engineering

Model Answers of Midterm Exam

Faculty Engineering
Department Mechatronics Systems Engineering
Module Code COM253
Module Title Engineering Computer Programming
Semester Fall 2023
Time Allowed 1.5 Hours
Total Mark 20
No. of Pages 4+ cover
Material provided ------------
Equipment permitted - Scientific calculator

- PC; disconnected from internet

- VC++ Compiler

- Printer

Additional Instructions - All Answers must be in English otherwise it will be ignored.

- Any developed code on the PC should be either re-written by


hand in the answer sheet or printed and attached to it at the
last page.

- Attach page _4_ to your answer sheet

No books, paper or electronic devices are permitted to be brought into the examination room
other than those specified above.
Faculty of Engineering
Model Answers of Midterm Exam

Module Title : Engineering Computer Programming


Module Code : COM253
Semester : Fall 2023

Answer all the following questions:


Question 1 : ........................................................................................................................................ (5 Marks)
An ellipsoid is the body of evolution of an ellipse. Figure 1 shows an ellipsoid with three main dimensions
a ,b, c. Write a C++ program, using math library, to calculate its volume (V) and surface area (S).

, 𝑺=

𝝅 = 𝟒. 𝐭𝐚𝐧−𝟏 (𝟏)

Figure 1
Answer:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
const float Pi = 4 * atan(1);
float a, b, c;
cout << "a,b,c:\a";
cin >> a >> b >> c;
float V = (4.0 / 3) * Pi * a * a * b * c;
float e = sqrt(1 - (a * a) / (c * c));
float S = 2 * Pi * a * a + Pi * c * c / e * log((1 + e) / (1 - e));
cout << "V=" << V << ",\tS=" << S;
}

Page 2 of 5
Question 2 : ........................................................................................................................................ (5 marks)
Develop a C++ program using functions to provide the following screen output. (Figure-2)
It should define and use the following functions to produce the three later columns.

• Twice(): it receives an integer (n)and returns twice its value (2.n)


• Power2(): it receives an integer (n) and returns (𝟐𝒏 )
• Factorial(): it receives an integer (n) and returns its factorial (n!)

Figure 2

(Note: Do NOT use math library functions in Question 2)


Answer:

#include <iostream>
using namespace std;

long Factorial(int n)
{
long M = 1;
for (int i = 1; i <= n; i++)
M *= i;
return M;
}

int Power2(int n)
{
return (1 << n);
}

int Twice (int n)


{
return (2 * n);
}

int main()
{
cout << "i\t\t2*n\t\t2^n\t\tn!\n"
<< "==\t\t===\t\t===\t\t===\n";
for (int i = 1; i < 11; i++)
cout << i << "\t\t" << Twice(i) << "\t\t" << Power2(i) << "\t\t" << Factorial(i)<<endl;
return 0;
}

Page 3 of 5
Question 3 : ....................................................................................................................................... (5 Marks)
Fill in the blanks with the appropriate word or symbol:
1. The modulus operator (%) in C++ gives the ________ of two integer numbers.
2. In C++, the ( ++i ) is an example of the use of ________-increment operator.
3. The logical AND operator in C++ is represented by ________.
4. The ternary operator in C++ is denoted by _______.
5. To compare two values for equality in C++, you use the ________ operator.
6. The scope resolution operator in C++ is denoted by ________.
7. The switch statement in C++ is used for _______ selection.
8. The _______ loop is guaranteed to be executed at least once.
9. The statement ________ is used to create an infinite loop.
10. The ________ keyword is used to force the termination of the current iteration of a loop and jump to the next
iteration.
Answer:
1) Reminder of division
2) Pre
3) &&
4) ?:
5) ==
6) ::
7) Multiway
8) do-while
9) for(;;) / while(1) / while(true)
10) continue

Page 4 of 5
Question 4 : ....................................................................................................................................... (5 Marks)
For the following code fragments,

• Find logical or syntax error(s) (if any), mark them (by circling around) then correct the
code in your answer sheet.
• Trace the code providing the output screen and memory map.
1- Screen Memory
//add two numbers //add two numbers 8 x: 5
int x = 5; int x = 5; y:3
int y = 3; int y = 3; result:
int result == x + y; int result = x + y; 8
cout >> result _ cout << result ;

2-
// Display the sign of x // Display the sign of x 5 x: 5
int x; int x;
cin >> x;
Positive
cin >> x; or
{ if (x > 0) {
if (x > 0) -5 x: -5
cout << "Positive";
cout << "Positive"; }
Negative
}
else {
{
cout << "Negative";
else }
cout << "Negative";
}

3-
//display from 1 to 5 //display from 1 to 5 12345 i:
for (int i = 0 ; 1 < 5; i -- ) for (int i = 1 ; i <= 5; i ++ ) 1234
cout << i; cout << i; 5

4-
// display if x is zero // display if x is zero num:5
int num;
5
int num; Number is NOT zero
cin << num; cin >> num;
if (num = 0) if (num == 0) or num:0
cout << "Number is zero"; cout << "Number is zero"; 0
else (num!=0) else _ Number is zero
cout << "Number is NOT zero"; cout << "Number is NOT zero";

5-
// assign b a value greater by // assign b a value 5 a:
1 than a //greater by 1 than a 6 56
int a, b; int a, b;
cin << a; cin >> a; b:6
b = a++ ; b = ++a ;
cout << b<< end ; cout << b<< endl ;

Page 5 of 5

*Best Wishes*

You might also like