0% found this document useful (0 votes)
16 views7 pages

Answers of Final - COM253 - Fall 2023

The document contains model answers for the final exam in Engineering Computer Programming (COM253) for Fall 2023, detailing questions and answers related to C++ programming. It covers topics such as functions, structures, object-oriented programming principles, and series calculations. The exam consists of multiple questions, each with specific programming tasks and requirements.

Uploaded by

mou.msd.50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Answers of Final - COM253 - Fall 2023

The document contains model answers for the final exam in Engineering Computer Programming (COM253) for Fall 2023, detailing questions and answers related to C++ programming. It covers topics such as functions, structures, object-oriented programming principles, and series calculations. The exam consists of multiple questions, each with specific programming tasks and requirements.

Uploaded by

mou.msd.50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Faculty of Engineering

Model Answers of Final Exam

Faculty Engineering
Department Mechatronics Systems Engineering
Module Code COM253
Module Title Engineering Computer Programming
Semester Fall 2023
Time Allowed 3 Hours
Total Mark 40
No. of Pages 6+ cover
Material provided ------------
Equipment permitted - Scientific calculator
- PC; disconnected from internet.
- VC++ Compiler (Visual Studio)
- Printer
Additional Instructions - All Answers must be in English otherwise it will not be considered.
- Any code developed on the PC should be either re-written in the
answer sheet or printed and delivered as a hardcopy attached to it.
- 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 Final Exam

Module Title : Engineering Computer Programming


Module Code : COM253
Semester : Fall 2023

Question 1 : ........................................................................................................................................... (10 Marks)


1.1 ....................................................................................................................................................... (5 Marks)
Write a C++ program using functions that use nested loops to draw the following pattern with arbitrary size (n) using output
only either the 'x' character (by default) or the space character ' ':
a. Triangle(n)
X
X X
X X X
X X X X
b. InvertedTriangle(n)
X X X X
X X X
X X
X

Then write down main() function that uses the above mentioned functions to produce the following pattern:
X
X X
X X X
X X X X
X X X
X X
X
Answer:

#include <iostream>
using namespace std;
void Triangle(int n,char c='X')
{
for (int i = 0; i < n; i++)
{
cout << ' ';
for (int j = 1; j < 2*n; j++)
{
if (j < (n - i))
cout << ' ';
else if (j <= (n + i))
cout << c;
else
break;
}
cout << "\n";
}
}
void Inverted_Triangle(int n, char c = 'X')
{
for (int i = 0; i < n; i++)
{
for (int j = 1; j < 2 * n; j++)
{
//cout << "j=" << j;
if (j <= i)
cout << ' ';
else if (j < (2*n - i))
cout << c;
else
break; Run
}
cout << "\n";
}
}
int main()
{
Triangle(3);
Inverted_Triangle(4);
return 0;
}

Page 2 of 7
1.2 Write a C++ program using functions that: ......................................................................................................................... (5 Marks)
1- Define a structure named Student that has the following data fields:
- Name
- ID
- GPA
- Grades (of 5 courses)
2- For number of three students, find the average grade for each student and display it as a formatted table.
3- Find and print the student’s data which has the maximum average grade.
Answer:
#include <iostream>
using namespace std;

struct Student
{
long ID;
string Name;
float GPA;
float Grades[5];
};

void Read_Student(Student& S1)


{
cout << "Enter ID, Name and GPA: ";
cin >> S1.ID >> S1.Name >> S1.GPA;
cout << "Enter 5 Grades:";
for (int i = 0; i < 5; i++)
cin >> S1.Grades[i];
}

void Print_Student(Student S1)


{
cout << "\nID: " << S1.ID << "\t,Name: "
<< S1.Name << "\t,GPA: " << S1.GPA << "\nGrades: ";
for(int i=0;i<5;i++)
cout <<S1.Grades[i]<<", ";
}
float calc_Average(Student S1)
{
float Sum = 0.0;
for (int i = 0; i < 5; i++)
{
Sum += S1.Grades[i];
}
return Sum / 5.0;
}

int find_max(float G[], int N)


{
float M = G[0];
int i_max = 0;
for (int i = 0; i < N; i++)
if (G[i] > M)
{
i_max = i;
M = G[i];
}
return i_max;
}
int main()
{
Student S[3];
for (int i = 0; i < 3; i++)
{
cout << "\nStudent\t" << i+1<<endl;
Read_Student(S[i]);
}
float Average_Grades[3];
for (int i = 0; i < 3; i++)
{
Average_Grades[i] = calc_Average(S[i]);
cout << i + 1 << '\t'<< Average_Grades[i] <<endl;
}
int i_max = find_max(Average_Grades, 3);
cout << "The best student is: ";
Print_Student(S[i_max]);
return 0;
}

Page 3 of 7
Question 2 : .................................................................................................................(10 Marks)
Write a C++ program to calculate the following series e given number of terms (N) and the
factor x using functions as the following (Remark: DO NOT use math library):
𝒙𝟎 𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝑵
𝒆(𝒙, 𝑵) = + + + + …+
𝟎! 𝟏! 𝟐! 𝟑! 𝑵!

where: x^0=1,and 0!=1


a. define a recursive function Fact(x) function to calculate the factorial of number x
b. define an iterative function Power(x, n) function to calculate ( xn ) where x is a floating-
point number and n is positive integer.
c. Define a function e( ) that uses loop to calculate the summation of first N terms of the above-
mentioned series.
d. Write main( ) function to call and print the function e( ) output for value of x=3.5 and values
of N from 1 to 5, in a formatted table; trace it to show its run output.
Answer:

#include <iostream>
using namespace std;

long Fact(int x)
{
if (x <= 0)
return 1;
else
return x * Fact(x - 1);
}

float power(float x, int n)


{
float M = 1.0;
for (int i = 0; i < n; i++)
M *= x;
return M;
}

float e(float x, int N)


{
float Sum = 0;
for (int i = 0; i < N; i++)
Sum += power(x, i) / Fact(i);
return Sum;
}
int main()
{
float x = 3.5;
cout << "i\t\te(x,i)\n";
cout << "===\t\t=====\n";
for (int i = 1; i <= 5; i++)
cout << i << "\t\t"<< e(x, i) << '\n';
return 0;
}

Page 4 of 7
Question 3 : ...............................................................................................................(10 Marks)
For the following code (Page 4):
3.1 In the shadow box, write down your code that permits the whole code to run properly as per
OOP principles.................................................................................................................. (2 Marks)
3.2 After completing the code mark the following (by circling around it/them) then write its
description in your answer sheet :................................................................................... (6 Marks)
1) Abstract class
2) Constructor(s)
3) Inheritance statement(s) and its tree
4) Polymorphism
5) Virtual function(s)
6) Encapsulation
3.3 Trace the code to get its output using memory map ...................................................... (2 Marks)

Answer:
3.1
Shape(int i = 0) { id = i; }

virtual double getArea() = 0;


3.2
1)
class Shape : has on pure virtual function
2)
Shape(int)
Rectangle(double,double)
Circle(double)
3)
class Rectangle : public Shape
class Circle : public Shape

Shape

Rectangle Circle

4) array of pointer to base class points to derived class objects (Shape* shapes[])
5) getArea()
6) Data + functions : private and public members of classes Shape, Rectangle and Circle
3.3
Run:

Page 5 of 7
Attach to your answer sheet.
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


const double Pi = 4 * atan(1);

class Shape
{ 1) Abstract Class
int id;
public:
int getID()
{ return id;}
string getType()
{ return (id == 1) ? "Rectangle" : "Circle"; }

Shape(int i = 0) { id = i; }// 2)Constructor 3.1 Completed Code


virtual double getArea() = 0;//5)Pure Virtual function

};
class Rectangle : public Shape //3- Inheritance
{
private: //6-Encapsulation

double length;
double width;
public:
Rectangle(double l, double w) : Shape(1)
//2-Constructor
{ length = l; width = w; }
virtual double getArea()
{return length * width;}
};
class Circle : public Shape // 3-Inheritance
{
private:
double radius;
public:
Circle(double r) : Shape(2)
//2-Constructor
{radius=r;}
virtual double getArea()
{return Pi * radius * radius;}
};
int main()
{
Shape* shapes[2];
//4-Polymorphism
shapes[0] = new Rectangle(4, 5);
shapes[1] = new Circle(4);
for (int i = 0; i < 2; ++i)
{
cout <<"Shape:\tID:" << setw(3) << shapes[i]->getID()
<<",\tType:" << setw(10) << shapes[i]->getType()
<<",\tArea:" << setw(5) << shapes[i]->getArea()
<< endl;
}
delete shapes[0];
delete shapes[1];
return 0;
}

Page 6 of 7
Question 4 : .................................................................................................................(10 Marks)
Fill in the blanks with the appropriate word, term, or symbol:
1. _________ is a named block of code that performs a specific task and can be reused.

2. _________ is a special type of function that is used to initialize an object of a class.

3. _________ is a mechanism that allows a subclass to get the features of a superclass.

4. _________ function can be redefined in a subclass to provide a different implementation.

5. _________ is a class that cannot be instantiated but can be a base for other classes.

6. _________ is a process of combining data and functions into a single unit called an object.

7. _________ is a repetition control structure that executes a block of code as a certain condition is still true.

8. _________ is an operator that returns true if two operands are equal and false otherwise.

9. _________ is an operator that returns the reminder of the division of two integer operands.

10. _________ is an operator that increments the value of its operand by one.

11. _________ is an operator that assigns the value of its right operand to its left operand.

12. _________ is an operator that accesses the members of an object, of a class or structure, using a pointer to that object.

13. _________ is an operator that releases the memory allocated dynamically by the new operator.

14. __________ allows functions having the same name, but with different parameters, to be defined in the same scope.

15. _________ permits a pointer-of-superclass pointer that points to objects of different subclasses to invokes their functions
dynamically depending on their own definition.

16. ____________.is the process of creating an instance of a class in OOP.

17. ____________ involves a function calling itself multiple times with different arguments.

18. Functions that don't return a value are declared with the return type ____________.

19. ____________ loop is used when a set of statements needs to be executed certain number of times, repeatedly.

20. ____________ statement is used for decision-making and allows the program to execute different code blocks based on
the value of an integer variable.

Answer:

1- function 11- =
2- constructor 12- ->
3- inheritance 13- delete
4- virtual 14- overloading
5- abstract 15- Polymorphism
6- Encapsulation 16- Instantiation
7- while/conditional 17- Recursion
8- == 18- Void
9- % 19- for/counted
10- ++ 20- Switch-case

Page 7 of 7

You might also like