You are on page 1of 20

“GHANA COMMUNICATION TECHNOLOGY UNIVERSITY

END OF SECOND SEMESTER EXAMINATIONS - 2020/2021 ACADEMIC


YEAR
FACULTY OF COMPUTING AND INFORMATION SYSTEMS

DEPARTMENT OF INFORMATION SYSTEMS AND TECHNOLOGY

TAKE HOME EXAMINATION

CSSD 102 – HIGH LEVEL PROGRAMMING LANGUAGE

DIT /BIT LEVEL 100

NAME: SETH ESOUN INDEX: 040118158

SESSION: MORNING COMPUS: TESANO

DIGITAL ADDRESS: C0-003-1323



Question 1

i)

a. Int
In programming, associate number may be a information kind accustomed represent
real integers that don't contain uncomplete values. Different sorts of number
information sorts are hold on in numerous ways in which on devices. In many common
programming languages, as an example, a little number includes a vary of thirty-
two,767 to -32,768. These restricted ranges demonstrate the link between a numerical
worth as seen by humans and the way these values are hold on in memory.

The use of integers as variables aids programming during a sort of ways in which. a
preferred technique, as an example, is to ascertain associate number variable and store
some worth in it which will have an effect on computations or calculations conducted
within the program. Primitive instances of code for counters, amortization schedules,
calendars, then on are examples.

b. Float
A float may be a information kind in engineering science that's fashioned of variety
that's not associate number since it includes a fraction written in mathematical notation.
one among the foremost rife professional definitions is that a float "contains numbers
on each side of the decimal." However, it's going to be a lot of simple to remark that
the float includes decimal fractions whereas the number doesn't. Some argue that the
float information kind is used in programming once a lot of accuracy than integers will
offer is needed.

c. Double
The double information kind is made into the compiler and is employed to construct
numeric variables that contain integers with decimal points. several pc languages, as
well as C, C++, C#, and others, acknowledge the double as a sort. A double kind could
represent each uncomplete and full values. It will have a complete of up to fifteen digits,
as well as those before and when the mathematical notation. once operating with
integers that require decimal points, several programmers believe the double kind to be
the default.
d. String
A string, like associate number or a floating-point unit, may be a information kind
employed in programming that's accustomed represent text instead of numbers. it's
created from a series of characters which will conjointly embrace areas and numerals.
String’s embrace, as an example, the word "hamburger" and therefore the sentence "I
Greek deity three hamburgers." If provided properly, even "12345" could also be
deemed a string. Strings should usually be encircled in quotation marks for the
information to be recognized as a string instead of variety or variable name.

e. Bool
Bool may be a basic kind within the C, C++, and C# programming languages. this sort
of variable will solely have 2 values: one and zero. These ar similar to true and false in
C++ and might be used interchangeably. In C#, bool variables could solely be true or
false; they can't be used with one or zero. To conserve memory house, Boolean
variables are often classified along. Understanding binary could also be a valuable
ability. employing a bool and testing for true/false will increase your program's
readability.

ii)
a. Int

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
// declaring variables:
int a, b;
int result;

// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;

// print out the result:


cout << result;
// terminate the program:

return 0;

///output 4

b. float

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
// declaring variables:
float a, b;
float result;

// process:
a = 5.0;
b = 2.0;
a = a + 1;
result = a - b;

// print out the result:


cout << result;

// terminate the program:

return 0;

c. double

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
// declaring variables:
double a, b;
double result;

// process:
a = 5.00;
b = 2.00;
a = a + 1;
result = a - b;

// print out the result:


cout << result;

// terminate the program:

return 0;

d. String

// my first string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystring1 = "This is a string";
string mystring2 = "This is a string";
string mystrin3 = "This is a string";

cout << mystring1;


cout << mystring2;
cout << mystring3;
return 0;
}

e. Bool

#include <iostream>
using namespace std;

int main() {
bool isCodingFun = true;
bool isFishTasty = false;
bool isMilkTasty = true;

cout << isCodingFun << "\n";


cout << isFishTasty;<< << "\n";

cout << isMilkTasty;


return 0;}
Question 2

a. Pointers

A pointer may be a variable in C++ that holds the address of another variable. Pointers, like
standard variables, have a knowledge sort. A pointer of sort number, as an example, will
contain the address of associate degree number variable. a personality sort pointer will store
the address of a personality sort variable. A pointer ought to be viewed as a symbol of a memory
address. Programs will emulate call-by-reference exploitation pointers. they're additionally
capable of making and manipulating dynamic information structures. A pointer variable in C++
may be a variable that points to a selected location in memory that's stated by another variable.
to grasp C++ pointers, you need to initial comprehend however computers store information.
after you outline a variable in your C++ program, it's given a location within the computer's
memory. This variable's worth is saved at the desired place. C++ offers the & (reference)
operator to see the position in memory wherever the information is hold on. The operator
returns the address of a variable. as an example, if x may be a variable, &x yields the variable's
address.

Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza"; // A string variable
string* ptr = &food; // A pointer variable that stores the address of food

// Output the value of food


cout << food << "\n";

// Output the memory address of food


cout << &food << "\n";

// Output the memory address of food with the pointer


cout << ptr << "\n";
return 0;}
b. Arrays
The array system in C++ stores a fixed-size successive assortment of things of a
similar sort. a group of information is kept in associate array, though it's typically a lot
of convenient to ideate associate array as a group of variables of a similar sort. rather
than shaping separate variables like number0, number1,..., and number99, you outline
one array variable like numbers and use numbers [0], numbers [1,..., numbers [99] to
represent individual variables. associate index is employed to access a particular part
in associate array. All arrays area unit created of contiguous memory regions. the
primary part corresponds to very cheap address, whereas the last part corresponds to
the best address. so as to declare associate array in C++, the engineer should offer the
kind of parts and therefore the variety of parts needed by the array, as seen below.
type arrayName [arraySize];. This is mentioned as a single-dimension array. The
arraySize constant should be larger than zero, and therefore the sort is any acceptable
C++ knowledge sort. as an example, to declare a 10-element array of sort double
named balance, use the subsequent statement: double equilibrium [10];

c. Structure
A structure could be a grouping of variables of assorted knowledge sorts that area unit
mentioned by one name. it's such as a category within the sense that each hold a group
{of knowledge of knowledge of information} of assorted data sorts. as an example,
suppose you want to avoid wasting bound info a few persons, like his or her name,
citizenship variety, and income. to avoid wasting this info separately, you will simply
construct numerous variables like name, citNo, and salary. However, within the
future, you will want to avoid wasting info for several folks. currently you'd have to
be compelled to construct distinct variables for every piece of knowledge for every
person: name1, citNo1, salary1, name2, citNo2, salary2, name3, citNo3, salary3,
salary4, salary5, salary6, salary7, salary8, salary7. you'll without delay imagine
however massive and untidy the code would be. it'll even be a troublesome endeavour
since there'll be no relationship between the variables (information). A desirable
approach would be to cluster all relevant info below the term Person and utilize it for
everybody. The code is currently heap clearer, perceivable, and economical. Person
could be a structure that contains all associated info below one name.
d. Classes
In C++, a category is that the basic building part that ends up in Object-Oriented
programming. it's a user-defined knowledge kind with its own set of information
members and member strategies which will be accessed and employed by making an
instance of that category. A C++ category to an object's blueprint.
As AN example: think about the automobile category. There is also varied cars with
numerous names and types, however they'll all have bound basic characteristics, like four
wheels, a regulation, a mileage varies, and so on. So, during this case, the category is
automobile, and therefore the attributes area unit wheels, speed limitations, and miles.
Example
category MyClass { // the category
public: // Access intellect
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
• A category may be a knowledge kind that contains knowledge members and member
functions that's such as by the user.
• Data members area unit knowledge variables, and member functions area unit
functions wont to management these variables. These knowledge members and member
functions verify the attributes and behavior of objects in a very category.
• In the preceding example of sophistication automobile, the info members are going to
be regulation, miles, and so on, and therefore the member functions are going to be
applied brakes, increase speed, and so on.

e. Function

A perform may be a block of code that's wanting to do a particular activity. for instance,
if you're developing an enormous C++ program and you would like to execute a selected
task varied times, like showing values from one to ten, you want to write a couple of lines
of code and repeat these lines every time you show knowledge. an alternative choice is to
place these lines at intervals a perform and decision it whenever you would like to point
out values. this can build your code a lot of straightforward, readable, and reusable.
you will partition your code into functions. it's up to your ways you split your code into
distinct functions, however conceptually, the division ought to be specified every perform
performs a particular duty. A perform declaration informs the compiler concerning the
name, come kind, and parameters of a perform. the particular body of the perform is
provided by a perform definition.
The C++ commonplace library includes a excessiveness of intrinsical functions
that your application will use. for instance, strcat() is employed to concatenate 2 strings,
memcpy() is employed to repeat one memory address to a different, and there area unit
more procedures. A perform is usually proverbial by alternative names, like a technique, a
software system, or a method. C++ has bound pre-defined functions, like main (), that
area unit wont to run code. However, you will additionally write your own functions to
handle bound tasks.
To create (also known as declare) a function, type the function name followed by
parenthesis ().

Syntax
void myFunction() {
// code to be executed
}

Question 3

#include <iostream>

using namespace std;

int main()
{
string name1[5] = {"E","s","o","u","n"};
string value;

cout << "Enter a value to be searched for: ";


cin >> value;

int foundValue = -1;

for (int i = 0; i < 5; i++) {


if (value == name1[i]) {
foundValue = i;
break;
}
}
if (foundValue >= 0) {
cout << value << " was found at the index of: " << foundValue;
}
else {
cout << value << " was not found!";
}

return 0;
}

Question 4
#include <iostream>
using namespace std;

class Car {
public:
string name;
int speed;
string favColor;
string make;
int YearModel;

double addTwoNumbers (double a, double b) {


cout << a + b;
}
};

int main()
{
Car objone;
objone.name = "Toyota";
objone.speed = 23;
objone.favColor = "Black";
objone.make = "makeBy";
objone. YearModel = 2021;

cout << "Name: " << objone.name << endl;


cout << "speed: " << objone.age << endl;
cout << "Favorite Color: " << objone.favColor << endl;
cout << "make: " << objone.make << endl;
cout << "YearModel: " << objone.YearModel << endl;
objone.addTwoNumbers(42.5, 60);
return 0;
} ”

Question 5
Object-oriented programming could be a programming technique or pattern during which
programs are organized around objects instead of functions and logic. It partitions the info into
2 memory locations, specifically information and functions, and aids in creating the code
versatile and standard. Object-oriented programming is primarily involved with objects that
has to be manipulated. information in OOPs is also delineated as objects with properties and
functionalities. Object-oriented programming's structure, or building blocks, embrace the
following:
• Classes are information sorts that are established by the user and function the blueprint
for individual objects, properties, and actions.
• Objects are instances of a category that are shaped with such information. things may
be real-world objects or abstract entities. once a category is initial shaped, the outline is that
the sole object such.
• Methods are functions such inside a category that specify associate degree object's
actions. each methodology category definition begins with a respect to an instance object.
additionally, the subroutines enclosed inside associate degree object are said as instance
strategies. strategies are employed by programmers to make sure reusability or to stay
practicality confined inside one object at a time.
• Attributes are outlined within the category model associate degreed describe an object's
state. information is going to be saved within the attributes field of objects. category
characteristics are the property of the category itself.
There are bound basic notions that function the inspiration for OOPs some includes.

• Encapsulation- Encapsulation is usually characterised because the packaging of


knowledge and knowledge into one entity. Encapsulation is represented in Object-
Oriented Programming because the coupling of knowledge and therefore the functions
that alter it. contemplate a real-world example of encapsulation: in an exceedingly firm,
there are a unit several divisions like the accounting section, the finance section, the
sales section, and so on. The finance sector manages all money transactions and
maintains track of all money knowledge. Similarly, the business department is
accountable of all sales-related operations and maintains track of all sales. Now, for no
matter reason, a political candidate from the finance sector might want all of the sales
knowledge for a particular month. during this instance, he's not permissible to directly
read the sales section's knowledge. He should 1st contact another officer within the
business department and acquire the precise data from him. Encapsulation is what it's.
Here, the sales department's knowledge and therefore the people that might modify it
area unit bundled below the umbrella term "sales section."

• Abstraction - information abstraction is one among the foremost crucial aspects of


object-oriented programming in C++. Abstraction entails revealing simply the foremost vital
info whereas concealing the subtleties. information abstraction is that the method of exposing
solely the required info a couple of information set to the skin world whereas concealing the
background details or implementation. take into account the subsequent scenario: a person is
driving a automobile. the person solely is aware of that pressing the accelerators can increase
the speed of the automobile or applying the brakes can stop the automobile, however he has no
plan however pressing the accelerators really will increase the speed of the automobile, nor
will he perceive the inner mechanism of the automobile or however the accelerator, brakes,
and alternative controls ar enforced within the automobile. this can be the definition of
abstraction.
Abstraction victimisation Classes: we tend to might use categories to implement Abstraction
in C++. victimisation the accessible access specifiers, the category assists US in grouping
information members and member functions. a category has management over that information
members ar visible to the skin world and that don't seem to be.
Header file abstraction: Header files ar another kind of abstraction in C++. take into account
the pow() operate enclosed within the maths.h header file. after we ought to reason the ability
of variety, we tend to merely decision the operate pow() within the maths.h header file and
provide the numbers as arguments, not knowing the underlying formula by that the operate
calculates the ability of numbers.

• Inheritance- categories will employ code from alternative categories. Relationships and
subclasses between objects may be assigned , sanctioning developers to employ common logic
whereas still maintaining a novel hierarchy. This property of OOP forces a a lot of thorough
information analysis, reduces development time and ensures the next level of accuracy.
Inheritance refers to a category's capability to derive options and traits from another class. one
among the foremost vital aspects of Object-Oriented Programming is inheritance. Sub category
or Derived category: {a category|a category} that inherits attributes from another class is named
as a Sub category or Derived Class. Base category or Super category: the category whose
attributes ar hereditary by subclasses is named because the Base category or Super Class.
Inheritance supports the concept of "reusability," which suggests that if we would like to
construct a brand new category and there's already a category that has a part of the code that
we tend to need, we tend to might derive our new category from the previous category. we tend
to ar utilizing the previous class's fields and functions by doing therefore. Dog, Cat, and Cow
ar samples of derived categories from the Animal Base category.

•Polymorphism- The term polymorphism refers to the presence of the many forms.
Polymorphism is outlined because the ability of a message to be sent in additional than one
kind. someone will have several characteristics at a similar time. a man could be a parent, a
spouse, associated an worker all at a similar time. As a result, a similar individual exhibits
various behaviour in several settings. this can be called polymorphism. Distinct occurrences of
associate operation might have totally different behaviours. The behaviour is set by the
information sorts used within the operation. C++ permits for operator and performance
overloading.
Operator Overloading: Operator overloading is that the method of inflicting associate operator
to show various behaviours in several things.
Function Overloading: operate overloading is that the use of one operate name to accomplish
many functions. Polymorphism is commonly used within the implementation of inheritance.
Assume we'd like to construct a operate to feature bound integers, wherever generally there ar
2 integers and alternative times there ar 3 integers. we are able to write the Addition technique
with a similar name however totally different parameters; the relevant technique are referred
to as supported the parameters.

Question 6
I)
a) Category hierarchies
In object-oriented programming, a group of categories that ar connected by inheritance. every
category could be a "subclass" of another category, that is its "superclass."
The taxon has all of the characteristics of its taxon, however it should add new options or
redefine existing ones. A category's attributes (or "properties") ar the gathering of attributes (or
"properties") that associate object of that class contains, similarly because the ways that will
be dead thereon.

Single inheritance happens once every category has only 1 taxon. The inverse is multiple
inheritance, that permits a category to possess several super categories. Single inheritance
creates a tree structure within the category hierarchy, whereas multiple inheritance creates a
directed graph.
Typically, the "object" category, the foremost general category that's associate antecedent
of all others and has no taxon, sits at the highest of the hierarchy.
Trees grow downward in computers, even as they are doing in kindred, that is why subclasses
ar regarded "below" their super categories. once a technique is invoked on associate object, it's
1st searched within the object's category, then within the taxon of that category, so on up the
hierarchy till it's found.
As a result, a category solely should declare the ways that ar distinctive thereto, and every one
alternative ways are hereditary from all of its super categories. A taxon object will do
everything a taxon object will, and perhaps rather more. The taxon is named because the "base
class" in C++, whereveras the taxon is named because the "derived class" (not to be confused
with a derived type).
b) Algorithms
is a careful stepwise generic resolution guide that was created with the goal of providing the
foremost economical and least long resolution to any drawback bestowed. There ar several
tools accessible to check the potency of algorithms, like huge American state notation, Omega
notation, and Gama notation, which may be wont to verify the effectiveness of algorithms.
A finite assortment of consecutive structured stages that is a roadmap to resolution any
drawback. This c++ formula term is most ordinarily employed in computing to explain the
method of resolution sophisticated problems. The design of the answer may alter betting on the
formula. In compared to alternative recursive solutions, the foremost economical formula is
one that provides the solution in less time and consumes less memory. The functions meant to
figure on range ranges ar contained within the C++ header algorithm>. These functions perform
operations on numbers however don't alter the information. It solely functions whereas iterating
or inform to numbers, and it ne'er touches its information.

Here are some C++ algorithm examples with step-by-step explanations:


Example #1
Create a C++ algorithm that will allow you to construct a software that will add two numbers.
Algorithm
Steps are given below:
1. Start
2. Accept num1, num 2
3. Sum= num1+ num2
4. Display sum
5. Stop

C) Flowchart
A flow chart may be a graphical illustration of a method or action. It consists of many
phases that the method "flows" through from setting out to finish. Flowchart’s area unit
normally used for generating company ways, specifying troubleshooting stages, and building
mathematical algorithms. Some flowcharts may have some stages; however, others could also
be quite sophisticated, with many different outcomes.
Standard symbols area unit sometimes utilized in flowcharts to denote distinct phases or
operations inside the chart. as an example, every step is painted by a parallelogram, however
every selection is painted by a diamond. Arrow’s area unit accessorial between the varied
symbols to point the flow of the procedure. whereas flowcharts could also be created employing
a pen and paper, there are a unit numerous software system packages out there that build flow
chart style exceptionally easy.

Flow chart symbols all have totally different meanings. the subsequent area unit the foremost
common flow chart symbols:

• Terminator: Associate in Nursing elongated circle that represents the start or finish of
a method.
• Process: an oblong flow chart form that represents a typical method flow step
(instructions or actions).
• A diamond flow chart form denotes a branch within the method flow. decisions that
have got to be created
• An instrumentation may be a little, labelled circular flow chart type that represents a
possibility within the method flow. (As shown by the circle with the letter "A" below.)
• Data: A quadrilateral that represents a process's knowledge input or output (I/O).
• Document: A document or report is denoted by this term (see image in sample flow
chart below).
d) Control Structure
A control structure could be a part of a codebase that supports selections supported variable
analysis. These useful elements square measure extraordinarily valuable in computing and
programming. Providing tangible examples is one in all the foremost effective strategies to
demonstrate what a bearing structure is. A do-while loop, for instance, is Associate in Nursing
example of a bearing structure within which the engineer orders the loop to restate a method
for a particular range of times. The variable is initialized at a particular purpose and is designed
to rise with every iteration till it reaches a planned threshold, at which period the loop ends.

Another kind of management structure is that the if/then statement. The if/then statement
guides the computer's corresponding behaviour once applied to a variable in a very specific
state. Control structures, in general, mirror useful elements of a codebase and demonstrate the
importance of those decision-making processes in code.

II)

V= u + at

#include <iostream>
#include <cmath>

using namespace std;


int main()
{
cout << "Please enter u,t,a:\n";
cout << "u=";
double u, t, a;
cin >> u;
cout << "t=";
cin >> t;
cout << "a=";
cin >> a;
double V= u+ a*t;
cout << "V=" << V << endl;
return 0;
}

S=ut+1/2a𝒕𝟐

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Please enter u,t,a:\n";
cout << "u=";
double u, t, a;
cin >> v;
cout << "t=";
cin >> t;
cout << "a=";
cin >> a;
double s = u * t + 0.5 * pow(a * t,2);
cout << "S=" << S << endl;
return 0;
}

S=1/2t(v+u)

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Please enter u,t,a:\n";
cout << "u=";
double v, t, a;
cin >> v;
cout << "t=";
cin >> t;
cout << "a=";
cin >> a;
double S = v * t + (a * t * t)/2.0;
cout << "S=" << S << endl;
return 0;
}

𝒗𝟐 =𝒖𝟐+2as

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Please enter u,t,a:\n";
cout << "u=";
double u, s, a;
cin >> v;
cout << "s=";
cin >> s;
cout << "a=";
cin >> a;
double v = pow(u, 2) + 2 * a * s;
double total = pow(v, 2);

cout << " total =" << total << endl;


return 0;
}

Reference
Bolton, D., 2021. Definition of Double in C, C++ and C#. [Online]
Available at: https://www.thoughtco.com/definition-of-double-958065
[Accessed 24 12 2021].
Christensson, P., 2006. String Definition. [Online]
Available at: https://techterms.com
[Accessed 24 12 2021].
programiz, n.d. C++ Structure and Function. [Online]
Available at: https://www.programiz.com/cpp-programming/structure-function
[Accessed 24 12 2021].
tutorialspoint, n.d. cpp_functions. [Online]
Available at: https://www.tutorialspoint.com/cplusplus/cpp_functions.htm
Gillis, A. S., n.d. object-oriented programming (OOP). [Online]
Available at: https://searchapparchitecture.techtarget.com/definition/object-oriented-
programming-OOP
[Accessed 29 12 2021].
Christensson, P., 2008. Flowchart Definition.. [Online]
Available at: https://techterms.com
[Accessed 30 12 20211].

You might also like