KEMBAR78
C++ Lab Manual | PDF | C++ | Namespace
0% found this document useful (0 votes)
12 views37 pages

C++ Lab Manual

The document is a laboratory manual for C++ programming, authored by Dr. Ibrahim Arfeen, detailing various labs focused on fundamental programming concepts. It includes objectives, theoretical background, and practical exercises for each lab, covering topics such as Integrated Development Environments, data types, mathematical expressions, and decision-making statements. Each lab provides step-by-step instructions for writing and executing C++ programs, along with error-checking and discussion prompts.
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)
12 views37 pages

C++ Lab Manual

The document is a laboratory manual for C++ programming, authored by Dr. Ibrahim Arfeen, detailing various labs focused on fundamental programming concepts. It includes objectives, theoretical background, and practical exercises for each lab, covering topics such as Integrated Development Environments, data types, mathematical expressions, and decision-making statements. Each lab provides step-by-step instructions for writing and executing C++ programs, along with error-checking and discussion prompts.
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/ 37

Laboratory Manual

Fundamental of C++ Programming

Dr. Ibrahim Arfeen

Department of Electrical Engineering


Table of Contents

Lab 1 : Getting Started with Integrated Development Environments (IDE)……1

Lab 2: Simple input output C++ program ……………………………………………8

Lab 3: Data Types and Variables in C++……………………………………………..11

Lab 4: Mathematical Expressions…………………………………………………….15

Lab 5 : Simple Decision (Selection)…………………………………………………..20

Lab 6 : Complex Decisions (Selection)………………………………………………25

Lab 7 : while Loop………………………………………………………………………..30

Lab 8 : for and do-while loops…………………………………………………………33


Computer Eng : C ++ Programming Basic Introduction to C++ Lab 1
Title Lab 1 : Getting Started with Integrated Development Environments (IDE)
Objectives:

1-To become familiar with the login process and the C++ environment used in the lab (IDE)

2-To learn the basics of an editor and compiler and be able to compile and run programs

Theory:

Basic C++ program structure

Introduction

An Integrated Development Environment is a software providing the functionalities of the two

software above (Editing and compiling) integrated in the same environment. it facilitates the

programmer task

Creating C++ Program


The following steps are necessary for creating and execution of a program in C++.

- Write the code for a program with IDE and save it

- Compile the program with a compiler

- Link the program with library functions

- Run the program

1- Using Code::Blocks to compile and execute a C++ Program

Code::Blocks creates what is called a Workspace to keep track of the project you are working

on. It is possible for you to be working on multiple projects within your workspace. A project is

a collection of one or more source (as well as header) files. Source files are the files that

contain the source code for your program. If you are developing a C++ program, you are writing

C++ source code (.cpp files)

First start a new Project by clicking on Create a new project OR To create a project, click on

the File pull-down menu, open New and then Project.

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


1
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 1

Step 1. Create a project from the File menu or the Start Here tab:

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


2
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 1
Step 2. Choose the Console category and then the Console application and click Go.

Step 3: Click Next on the “Welcome to the new console application wizard!” screen.

Step 4: Choose C++!

…then click Next.

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


3
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 1
Step 5. Enter a project title. If you like you can change the default folder to hold the project.

Click Next.

Step 6: Choose the compiler. For this tutorial, choose GNU GCC as the compiler. Click finish.

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


4
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 1
Step 8: Your project is now created! Click on Sources in the left column, then double-click

main.cpp. Click the icon in the toolbar or press F9 to compile and run the program.

2- Using Microsoft Visual Studio

Visual Studio uses projects to organize the code for an app, and solutions to organize your

projects. A project contains all the options, configurations, and rules used to build your apps.

It manages the relationship between all the project's files and any external files. To create your

app, first, create a new project and solution.

Step 1: In Visual Studio, open the File menu and choose New > Project to open the Create

a new Project dialog. Select the Console App template that has C++, Windows, and

Console tags, and then choose Next

Step 2: In the Configure your new project dialog, enter HelloWorld in the Project name edit

box. Choose Create to create the project.

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


5
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 1

Step 3: Visual Studio creates a new project. It's ready for you to add and edit your source
code.

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


6
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 1
Step 4: The Console App template provides source code for a "Hello World" app, like this:

Step 5: To run the code, on the menu bar, choose Local Windows Debugger

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


7
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 2
Title Lab 2 : Simple input output C++ program
Objective:

1- Learn how to make a simple C++ program using IDE

2- To be familiar with basic syntax of C++ programming

Theory:

- Basic C++ program structure

- Procedure to write meaningful programs

Practical_1 :
Type the following code to display “This is a simple program in C++” in C++. Without

Statement using namespace std;

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


8
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 2

#include <iostream>

int main ()

Std :: cout << "This is a simple program in C++ ";

return 0;

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

If successful (no error) then run your program. Figure below should appear in your screen

Practical_2 :
Rewrite the following code to display “This is a simple program in C++” in C++. With Statement

using namespace std

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


9
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 2
#include <iostream>

using namespace std;

int main ()

cout << "This is a simple program in C++ ";

return 3 ;

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

If successful (no error) then run your program. Figure below should appear in your screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


10
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 3
Title Lab 3: Data Types and Variables in C++
Objective:

1- To introduce variables and named constants

2- To introduce various data types: Integer, Character and Floating point

3- To learn Variables Declaration and initialization local or global

4- To learn, recognize and correct the two types of Program errors (syntax and logic errors)

Theory:

- Basic C++ program structure

- Procedure to write meaningful programs

- Everything above the int main () is considered the header or global section.

Practical_1:
Type the following code to show why we need to be careful when we use integer literals to

initialize variables (local variable).

/***************************************************************
* Using some literal values as variable initializers *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration and initialization (local variable)
int x = -1245;
unsigned int y = 1245;
unsigned int z = -2367;
unsigned int t = 14.56;
// Outputting initialized values
cout << x << endl;

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


11
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 3
cout << y << endl;

cout << z << endl;


cout << t;
return 0;
}

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen and find logic errors.
3- Rewrite the above code to with global variables.

Practical_2 :
We can use an escape sequence (backslash followed by a symbol) to define some special

characters as shown in Table


C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.
12
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 3

Type the following code


/***************************************************************
* Using some special characters in strings *
***************************************************************/
#include <iostream>

using namespace std;

int main ()

cout << "Hello\n";

cout << "Hi\t friends." << endl;

cout << "Using some \bchar." << endl;

cout << "Hello\rBonjour mes amis." << endl;

cout << "This is a single quote\'." << endl;

cout << "This is a double quote\"." <<endl;

cout << "This is how to print a backslash \\.";

return 0;

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


13
Computer Eng : C ++ Programming Basic Introduction to C++ Lab 3
After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1- If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


14
Computer Eng : C ++ Programming Mathematical Expressions Lab 4
Title Lab 4: Mathematical Expressions
Objective:

1- To work with constants and mathematical functions

2- To demonstrate the use of arithmetic operators

Theory:

- Basic C++ program structure

- Arithmetic operators (+, - , *, /, %)

- Assignment operators (+=, -=, *=, /=)

Practical_1:
Type the following code to shows how it makes a big difference when we use parentheses

/***************************************************************
* The program shows the use of parenthetical expressions. *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Variable declaration
int x = 4;
// Printing the first expression with and without parentheses
cout << "Value with parentheses: " << (x + 3) * 5 << endl;
cout << "Value without parentheses: " << x + 3 * 5 << endl << endl;
// Printing the second expression with and without parentheses
cout << "Value with parentheses: " << 12 / ( x + 2) << endl;
cout << "Value without parentheses: " << 12 / x + 2;
return 0;
}

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


15
Computer Eng : C ++ Programming Mathematical Expressions Lab 4
After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen

Practical_2 :
Type the following code to mathematics Arithmetic operators

/***************************************************************
* Shows effects of multiplicative expressions *
***************************************************************/
#include <iostream>

using namespace std;

int main ()

// Multiplication
cout << "Testing multiplication operator" << endl;

cout << "Value of 3 * 4 = " << 3 * 4 << endl;

cout << "Value of 2.4 * 4.1 = " << 2.4 * 4.1 << endl;

cout << "Value of −3 * 4 = " << −3 * 4 << endl;

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


16
Computer Eng : C ++ Programming Mathematical Expressions Lab 4
// Division
cout << "Testing division operator" << endl;

cout << "Value of 30 / 5 = " << 30 / 5 << endl;

cout << "Value of 4 / 7 = " << 4 / 7 << endl;

// Remainder
cout << "Testing remainder operator" << endl;

cout << "Value of 30 % 5 = " << 30 % 5 << endl;

cout << "Value of 30 % 4 = " << 30 % 4 << endl;

cout << "Value of 3 % 7 = " << 3 % 7 << endl;

return 0;

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1- If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


17
Computer Eng : C ++ Programming Mathematical Expressions Lab 4
Practical_3 :
Type the following code to Assignment operators (+=, -=, *=, /=)

/***************************************************************
* Testing some compound-assignment expressions *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration of five variables
int x = 20;
int y = 30;
int z = 40;
int t = 50;
int u = 60;
// Use compound assignments
x += 5;
y −= 3;
z *= 10;
t /= 8;
u %= 7;
// Output results
cout << "Value of x: " << x << endl;
cout << "Value of y: " << y << endl;
cout << "Value of z: " << z << endl;
cout << "Value of t: " << t << endl;
cout << "Value of u: " << u;
return 0;
}

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


18
Computer Eng : C ++ Programming Mathematical Expressions Lab 4
After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1- If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


19
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 5
Title Lab 5: Simple Decision (Selection)
Objective:

1. To work with relational operators

2. To work with conditional statements

3. To learn and use nested if statements

Theory:

- Basic C++ program structure

- One way Decision: if (Also known as simple if)

-Two-way Decision: if…else

- Multi way Decision: if…else if…else if…else

Practical_1:
Type the following code to One way Decision

/***************************************************************
* Using if-statement to print the absolute value of a number *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration
int number;
// Getting input
cout << "Enter an integer: ";
cin >> number;
// Finding the absolute value
if (number < 0)
{
number = abs(number);
C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.
20
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 5
}
// Printing the absolute value
cout << "Absolute value of the number you entered is: " << number;
return 0;
}

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive and negative) value of numbers
3- Discuss the result obtained by screen

Practical_2 :
Type the following code to Two-way Decision: if…else

/***************************************************************
* Use of an if-else statement to find a pass/no-pass grade *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Local Declaration
int score;

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


21
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 5
// Input
cout << "Enter a score between 0 and 100: ";

cin >> score;

// Decision
if (score >= 60)

cout << "Grade is pass" << endl;

} // End if

else

cout << "Grade is no pass" << endl;

} // End else

return 0;

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1- If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive and negative) value of grades
3- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


22
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 5
Practical_3 :
Type the following code to nested if statements

/***************************************************************
* Find a grade given a score using the multi-way selection *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration
int score;
char grade;
// Get Input
cout << "Enter a score between 0 and 100: ";
cin >> score;
// Multi-way decision using if-else
if (score >= 90)
{
grade = 'A';
}
else if (score >= 80)
{
grade = 'B';
}
else if (score >= 70)
{
grade = 'C';
}
else if (score >= 60)
{
grade = 'D';
}
else
{
grade = 'F';
}

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


23
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 5
// Output
cout << "The grade is: " << grade;
return 0;
} // End main

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1- If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive) value of scores
3- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


24
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 6
Title Lab 6 : Complex Decisions (Selection)
Objective:

1- To learn and use logical operators

2- To learn and use the switch statement


Theory:
- Basic C++ program structure

- Logical Expressions

- Switch Statement

Practical_1:

Type the following code for complex decisions (Logical Expressions)

/***************************************************************
* Find age eligibility to rent a car *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration of variables
int age;
bool eligible;
// Getting input
cout << "Enter your age: ";
cin >> age;
// Setting the condition
eligible = (age >=25) && (age <= 100);
// Testing the condition and output
if (eligible)
{
cout << "You are eligible to rent a car.";

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


25
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 6
}
else
{
cout << "Sorry! You are not eligible to rent a car.";
}
return 0;
}

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive 1 to 100) value of age
3- Discuss the result obtained by screen

Practical_2 :
Type the following code for complex decisions

/***************************************************************
* Turn the air conditioning system on if the temperature is * below or above a certain
temperature. *
***************************************************************/
#include <iostream>
using namespace std;
int main ()

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


26
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 6
{
// Variable declaration
int temperature;
bool hot;
bool cold;
// Input the temperature
cout << "Enter the temperature: ";
cin >> temperature;
// Set two conditions
hot = temperature >= 75;
cold = temperature <= 65;
// Make the decision
if (hot || cold)
{
cout << "The air condition system is turned on!" << endl;
if (hot)
{
cout << "The cooler is working!" << endl;
}
else
{
cout << "The heater is working!" << endl;
}
}
else
{
cout << "The air condition system is turned off!" << endl;
}
return 0;
}

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


27
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 6
After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (above 75 and below 60) value of temperature (Fahrenheit)
3- Discuss the result obtained by screen

Practical_3 :
Type the following code to Switch Statement

/***************************************************************
* Use a swith statement to print a grade from a given score. *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration
int score;
char grade;
// Input
cout << "Enter a score between 0 and 100: ";
cin >> score;
// Decision making using switch statement

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


28
Computer Eng : C ++ Programming Decision Making or Conditional Statement Lab 6
switch (score / 10)
{
case 10: grade = 'A';
break;
case 9 : grade = 'A';
break;
case 8 : grade = 'B';
break;
case 7 : grade = 'C';
break;
case 6 : grade = 'D';
break;
default: grade = 'F';
} // End switch
// Output
cout << "Score: " << score << endl;
cout << "Grade: " << grade << endl;
return 0;
} // End main

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1- If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive 0 to 100) value of scores
3- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


29
Computer Eng : C ++ Programming Repetition (Loops ) Lab 7
Title Lab 7 : while Loop
Objective:

1. To introduce counter and event-controlled loops

2. To work with the while loop

3- Increment and decrement operators (++, --)

Theory:
- Basic C++ program structure

- while Loop

Practical_1:

Type the following code for counter- controlled while loop

/***************************************************************
* Use of a while statement to print a message 10 times *
***************************************************************/
#include <iostream>
using namespace std;
int main ( )
{
// Declaration and initialization of counter
int counter = 0;
// While statement
while (counter < 10) // The number of repetition is fixed to 10
{
Cout << "Hello world!" << endl;
counter++;
}
return 0;
}

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


30
Computer Eng : C ++ Programming Repetition (Loops ) Lab 7
After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen

Practical_2 :
Type the following code to event-controlled while loop

/***************************************************************
* Use of the sentinel-controlled while loop to find the sum *
* of some numbers *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration
int sum = 0;
int num;
// Loop including the first input
cout << "Enter an integer (1 to stop): ";

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


31
Computer Eng : C ++ Programming Repetition (Loops ) Lab 7
cin >> num;
while (num != 1)
{
sum = sum + num;
cout << "Enter an integer (1 to stop): ";
cin >> num ; // Sentinel update
}
// Outputting result
cout << "The sum is: " << sum;
return 0;
}
After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive or negative) value of num
3-Insert 1 to stop
4- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


32
Computer Eng : C ++ Programming Repetition (Loops ) Lab 8
Title Lab 8 : for and do-while loops
Objective:

1- To introduce the do-while loop

2-To work with the for loop

3- Increment and decrement operators (++, --)

Theory:

- Basic C++ program structure

- do-while loop

- for loop

Practical_1:

Type the following code to for loop

/***************************************************************
* Use of a for loop to print n integers *
***************************************************************/
#include <iostream>
using namespace std;
int main ()
{
// Declaration
int n;
// Get the value of n
cout << "Enter the number of integers to print: ";
cin >> n;
// Loop
for (int counter = 0; counter < n; counter++)
{
cout << counter << " ";
}
C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.
33
Computer Eng : C ++ Programming Repetition (Loops ) Lab 8
return 0
}

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive) value of n
3- Discuss the result obtained by screen

Practical_2 :
Type the following code to do-while loop

***************************************************************
* Demonstrate the use of the do-while loop to extract the *
* left-most digit of an integer. *
***************************************************************/
#include <iostream>

using namespace std;

int main ( )

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


34
Computer Eng : C ++ Programming Repetition (Loops ) Lab 8
// Declaration
int num;
short leftDigit;
// Input
cout << "Enter a non-negative integer: ";
cin >> num;
// Loop
do
{
leftDigit = num % 10;
num = num / 10;
} while (num > 0);
// Output
cout << "The leftmost digit is: " << leftDigit << endl;
return 0;
}

After finishing writing the code, check if there is any error. You can compile the code to check

the error (click build, then click compile).

Result

1-If successful (no error) then run your program. Figure below should appear in your screen
2-Insert different (positive three bits or four bits ) value of num
4- Discuss the result obtained by screen

C++ Programming: Dr.Ibrahim Arfeen Electrical Department Eng.


35

You might also like