Answers of Midterm - COM253 - Fall 2023
Answers of Midterm - COM253 - Fall 2023
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
- VC++ Compiler
- Printer
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
, 𝑺=
𝝅 = 𝟒. 𝐭𝐚𝐧−𝟏 (𝟏)
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.
Figure 2
#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 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*