C++ Lab Manual
C++ Lab Manual
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:
Introduction
software above (Editing and compiling) integrated in the same environment. it facilitates the
programmer task
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
First start a new Project by clicking on Create a new project OR To create a project, click on
Step 1. Create a project from the File menu or the Start Here tab:
Step 3: Click Next on the “Welcome to the new console application wizard!” screen.
Click Next.
Step 6: Choose the compiler. For this tutorial, choose GNU GCC as the compiler. Click finish.
main.cpp. Click the icon in the toolbar or press F9 to compile and run the program.
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
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
Step 2: In the Configure your new project dialog, enter HelloWorld in the Project name edit
Step 3: Visual Studio creates a new project. It's ready for you to add and edit your source
code.
Step 5: To run the code, on the menu bar, choose Local Windows Debugger
Theory:
Practical_1 :
Type the following code to display “This is a simple program in C++” in C++. Without
#include <iostream>
int main ()
return 0;
After finishing writing the code, check if there is any error. You can compile the code to check
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
int main ()
return 3 ;
After finishing writing the code, check if there is any error. You can compile the code to check
Result
If successful (no error) then run your program. Figure below should appear in your screen
4- To learn, recognize and correct the two types of Program errors (syntax and logic errors)
Theory:
- 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
/***************************************************************
* 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;
After finishing writing the code, check if there is any error. You can compile the code to check
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
int main ()
return 0;
Result
1- If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen
Theory:
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;
}
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>
int main ()
// Multiplication
cout << "Testing multiplication operator" << endl;
cout << "Value of 2.4 * 4.1 = " << 2.4 * 4.1 << endl;
// Remainder
cout << "Testing remainder operator" << endl;
return 0;
After finishing writing the code, check if there is any error. You can compile the code to check
Result
1- If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen
/***************************************************************
* 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;
}
Result
1- If successful (no error) then run your program. Figure below should appear in your screen
2- Discuss the result obtained by screen
Theory:
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
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;
// Decision
if (score >= 60)
} // End if
else
} // End else
return 0;
After finishing writing the code, check if there is any error. You can compile the code to check
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
/***************************************************************
* 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';
}
After finishing writing the code, check if there is any error. You can compile the code to check
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
- Logical Expressions
- Switch Statement
Practical_1:
/***************************************************************
* 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.";
After finishing writing the code, check if there is any error. You can compile the code to check
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 ()
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
After finishing writing the code, check if there is any error. You can compile the code to check
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
Theory:
- Basic C++ program structure
- while Loop
Practical_1:
/***************************************************************
* 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;
}
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): ";
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
Theory:
- do-while loop
- for loop
Practical_1:
/***************************************************************
* 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
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>
int main ( )
After finishing writing the code, check if there is any error. You can compile the code to check
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