Lab Report Template Ict
Lab Report Template Ict
LAB REPORT # 01
Submitted by
BE-34-12-166
Syndicate-B
Submitted to
20-02-13
Algorithm and Computing Lab Report # 01
Introduction
C++
C++ is a general purpose programming language and one of the most popular. It is
implemented on wide variety of hardware and operating systems. It can also be used for
hardware design. C++ is, in many ways, an enhancement to C adding things like classes and
templates etc.
Objectives
The purpose of this lab was to become familiar with the basics of a C++ program and to
learn how to write a basic program. The objectives of this lab are stated below:
Understanding functions
1
Algorithm and Computing Lab Report # 01
Adding comments
Tasks
The tasks performed in this lab revolved around the objectives mentioned above. Following
were the tasks performed:
Header Files:
Header files contain certain elements of source code which are to be used time and again in
a standard format. Once a header file has been compiled (after addition in a program) it
doesn‟t need to be compiled again, hence saving time. The header files usually contain
forward declarations of variables; a class etc. and has the extension .h. For example, the
header file iostream.h includes function about input and output. A header file is added in
the following way:
The # shows that the process is predefined. The addition of a header file is shown below:
2
Algorithm and Computing Lab Report # 01
Functions:
Functions are predefined operations that have been grouped together and are used
multiple times in a program. Due to tis they have become a kind of a standard. The
function used in almost all programs is int main ( ). The ( ) identifies a function. The body
of the function is placed inside { }. The function must have a return same as the data
type defined of the function. It is empty in case of main (and void) function however
arguments can also be put there. A function is included in the following way:
Namespaces:
Namespaces can be viewed as a group of variables. Multiple variables can be grouped in a
namespace and same identifiers can be used for variables in different namespaces. A
namespace is created in the following way:
namespace identifier {data type variable1;} (e.g. namespace Gotham { int tumbler=8;} )
3
Algorithm and Computing Lab Report # 01
4 Using namespaces
Variables:
Declaring variables creates a space/slot in memory which can be used to store just about
anything (e.g. integers, floating points, characters etc.). While declaring a variable its value
can be set to anything (usually 0) and the type of data it is going to store is also defined.
Variables can store input given by a user of an intermediate result of some operation. A
variable cannot have the same name as a keyword or begin with a numeric. A variable is
declared and set in the following way:
data type variable=0 (e.g. int My_Age=0). Declaration of a variable is shown below:
4
Algorithm and Computing Lab Report # 01
Input/output:
While in a program an input can be taken by the user and an output can be displayed. An
input can also trigger some sort of action while output can show the result of some operation
or can indicate the end of a program. In order take input a variable must be declared first
to store that value. In case of output, a variable storing the value of some operation can be
displayed or “printed” or any line can be displayed as desired. To take input the
following code is used:
cin>>variable identifier (e.g. cin>>My_Age). The output is shown below:
As mentioned above the output can be of two types, and it can be displayed by using the
following code:
5
Algorithm and Computing Lab Report # 01
Escape sequences:
Escape sequence can be really helpful in giving spaces in the output that is shown to the
user. They also allow to add things like quotes („‟ “ ”) and backslash (\) in the output. Escape
sequences can be added by the following code:
\’ (Single quote)
\” (Double quotes)
\\ (Backslash)
Comments:
Comments can be added in the program to make it more understandable for someone
seeing the source code. The comments don not appear in the output, they are just there to
make the code easier to read. Comments can be added in the following way:
6
Algorithm and Computing Lab Report # 01
9 A comment
Arithmetic operations:
Basic arithmetic operations like addition; multiplication etc. can be performed by using
appropriate code. The symbols used for these operations are: +, -, *, /, % (modulus). These
operations can be applied in the following way:
7
Algorithm and Computing Lab Report # 01
Variable identifier arithmetic operator equal to constant/variable (e.g. int a=5; a+=/-
=/*=/%=7)
Another way to increase or decrease the value of a variable is by using the increment or
decrement operators. They increase/decrease the value of the integer by 1. There are two
ways in which these operators can be applied:
8
Algorithm and Computing Lab Report # 01
The above implementation first executes/completes the operation in which this operator is
applied and then increases/decreases the value of variable by 1. The output is shown below:
12 Applying increment operator, with the value of variable increasing after function is complete
This implementation first increases or decreases the value of variable by one and then
completes the rest of operation. The output is shown below:
13 Applying decrement operator, with the value of variable decreasing before operation is complete
9
Algorithm and Computing Lab Report # 01
Observations
I observed in this lab that C++ can be used to write small functionality programs as this
language provides a plethora of feature. In addition to this, Visual Studio is a really great tool
for compiling due to it IntelliSense feature.
Conclusion
This lab was useful and it helped understand and learn the basic concepts of a C++ program.
C++ is a very competent language to write good programs. Furthermore, Visual Studio, with
its large arsenal of features, makes coding an enjoyable process.
10