C++ Lab Manual
C++ Lab Manual
C++ Lab Manual
Information Technology
College of Computing Sciences and Engineering
Solutions
Department of Information Science
Lab Manual
Prepared by
Professor Mostafa Abd-El-Barr
Mariam Al-Otaibi
Aisha Al-Noori
Laboratory Policy.......................................................................................................................................... 5
Introduction ................................................................................................................................................... 7
In this lab the students will be using Microsoft Visual C++ 6.0.
Activity Weight
Total 15%
2. If you agree to the license, put the checkmark and click Next.
6. When asked for the setup type, click on the Standard button.
7. When asked to register the environment variables, you can leave the option to register the
environment variables unchecked, though it does no harm to check this option. Click OK.
8. Note that the Visual C++ setup has finished successfully and click OK.
1. Laboratory Objective
The objective of this laboratory is to introduce students to the basic concepts in C++ Programming
Language as well as the basic forms for the Input and the Output Statements in the Language.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
a. Type, edit, compile, debug, and run simple C++ Programs.
b. Write, edit, compile, debug, and run a program that includes simple Input and Output Statements
in the C++ Language.
\r carriage return
\t Tabulation
\v vertical tabulation
\b Backspace
\f page feed
\a alert (beep)
\? question (?)
\\ inverted slash
In this laboratory, you are required to type, edit, compile and execute the following C++ program:
#include<iostream.h>
void main()
Use Microsoft visual C++ to compile and execute the above program.
Write a C++ program that inputs three integers by the user and displays the three integers, their sum,
average and product.
Sample Output
The sum is 12
The product is 48
The average is 4
5. Laboratory Instructions
Output:
*********
* *
* *
* *
* *
* *
* *
* *
*********
1. Laboratory Objective
The objective of this laboratory is to introduce students to the basics of declaring Variables, and
Constants as well as the use of different Operators in the C++ Language.
2. Laboratory Learning Outcomes: After conducing this laboratory students will be able to:
a. Declare Variables and Constants and use them in Simple C++ Programs.
b. Use different types of Operators, such as Arithmetic, Relational, and Compound Operators in
Simple C+++ Programs.
double 8 double precision floating point number. 1.7e + / - 308 (15 digits)
long double 10 long double precision floating point number. 1.2e + / - 4932 (19 digits)
bool 1 Boolean value. It can take one of two values: true or false true or false
NOTE: this is a type recently added by the ANSI-C++
standard. Not all compilers support it. Consult section bool
type for compatibility information.
! Boolean NOT
+, - Addition, Subtraction
<, <=, >, >= Less than, Less than or equal to, Greater than, Greater than or Equal to
=, *=, /=, %=, Assignment, Multiply and assign, Divide and assign, mod and assign, Add
+=, -= and assign, Subtract and assign
1. Write a C++ program that declares and initializes an integer then displays the integer raised to the
power of 2.
2. Write a C++ program that declares and initializes three variables of type double. The program
should display the sum, average, and product of the numbers in the command window.
5. Laboratory Instructions
6. Laboratory #2 Exercises
Write a C++ program that declares and initializes the radius of a circle. The program should output the
area and circumference. Define π as a constant in your program.
π = 3.14159
Sample Output
1. Laboratory Objective
The objective of this laboratory is to train students on how and when to use different C+++
control structures.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
a. Identify and practice using the different C++ Control Structures.
b. Practice using the C++ Branching and Switch Statements in Simple C++ Program Applications.
Switch (expression) {
Case constant_1;
Block of Statements_1
Break;
Case constant_2;
Block of Statements_2
Break;
……
Default:
Default Block of Statements
}
Write a C++ program that prompts the user to input two integer variables. The program should
prompt the user asking which operation need to be performed as follows:
1. Addition
2. Subtraction
3. Multiplication
4. Division (the second number should not be equal to zero)
5. Compare the two numbers (determining if the first number is larger, smaller or equal to the
second number)
When the user inputs his/her choice a switch statement should b used to process the user’s input
and display the result.
1- Addition
2- Subtraction
3- Multiplication
4- Division
3 + 8 = 11
5. Laboratory Instructions
6. Laboratory #3 Exercises
Write a C++ program that prompts the user to input an integer variable. The program should then print
whether the number is odd or even.
Sample Output
Enter an integer: 9
9 is odd
ISC 115 Computing Foundations Page 17
Laboratory #4 – Control Structures II
1. Laboratory Objective
The objective of this laboratory is to train students on how and when to use different C+++
control structures.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
a. Identify and practice using the different C++ Control Structures.
b. Practice using the C++ Loop Structure in Simple C++ Program Applications.
for (variable initialization; condition; variable update) { Code to execute while the condition is true }
1. Write a C++ program that uses while loop to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum (firstNum must be less
than secondNum). If the user enters the first number that is greater than or equal to the second
number, an error message should be printed and the program should read the numbers again until
the user satisfies the previous condition.
b. Output all even numbers between firstNum and secondNum.
c. Output the sum of all odd numbers between firstNum and secondNum.
Sample Output
Enter two integers, such that the first number is less than the second
number
9 2
2 9
2. Write a C++ program that inputs a series of 10 integers and determines and prints the largest integer.
Your program should use at least the following three variables:
a. counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input
and to determine when all 10 numbers have been processed).
b. number: The integer most recently input by the user.
c. largest: The largest number found so far.
Sample Output
Enter 10 integers
Enter number: 45
Enter number: 2
Enter number: 5
Enter number: 11
Enter number: 2
Enter number: 100
Enter number: 2
Enter number: 99
Enter number: 100
Enter number: 33
The largest is 100
3. Write a C++ program that inputs the size of the triangle and the shape, then it outputs the triangle
using the shape entered. Use a nested loop to output the triangle.
Sample Output
Enter the size of the triangle: 7
Enter the shape: #
#
##
###
####
#####
######
4. Write a C++ program that inputs a series of 10 integers and calculates and prints the average number.
The sum and average should be of type double.
Sample Output
Enter 10 integers
Enter number: 20
Enter number: 19
Enter number: 12
Enter number: 9
Enter number: 17
5. Laboratory Instructions
6. Laboratory #4 Exercises
Write a C++ program that calculates and prints the product of the odd integers from 1 to 15.
Sample Output
1. Laboratory Objective
The objective of this laboratory is to train students on how to use Arrays data structure in the context
of the C++ Programming Language.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
An array consists of a series of elements (variables) of the same type that are placed
consecutively in the computer’s memory such that the elements can be individually
referenced by adding an index to the array name.
Example: A -12 36 -120 72 255
A [0] A[1] A[2] A[3] A[4]
Example
1. Write a C++ program that creates an arithmetic sequence. It should prompt the user to input the first
term in the sequence first and the difference between two successive terms diff. The program
then creates a one-dimensional array with 10 elements ordered in an arithmetic sequence. It also
outputs the arithmetic sequence.
Sample Output
2. Write a C++ program that inputs 10 integers, stores it in a one dimensional array and calculates and
outputs the average of the integers. It also counts and outputs the number of integers that are greater
than the average and the number of integers that are less than the average.
Sample Output
Enter number 1 : 58
Enter number 2 : 66
Enter number 3 : 75
Enter number 4 : 98
Enter number 5 : 100
Enter number 6 : 82
Enter number 7 : 94
Enter number 8 : 87
Enter number 9 : 71
Enter number 10 : 96
average = 82.7
5 numbers are greater than the average
5 numbers are less than the average
5. Laboratory Instructions
Please follow our hints for “Good Programming Practices” and avoid our hints for “Common
Programming Mistakes” that were mentioned in previous labs.
6. Laboratory #5 Exercises
Lab Exercise 7
Write a C++ program that inputs 10 integers, stores it in a one dimensional array and calculates and
outputs the average of the integers. It also counts the number of integers that are greater than the average
and the number of integers that are less than the average. It then displays the result using a bar charts.
1. Laboratory Objective
The objective of this laboratory is to train students on how to use Arrays data structure in the context
of the C++ Programming Language.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
1
2
1. Write a C++ program that declares a two-dimensional array that has 5 rows and 5 columns.
Initialize the array such that if the sum of indices is even, the element should equal to the sum of
Sample Output
0 0 2 0 4
0 2 2 4 4
2 2 4 6 6
0 4 6 6 12
4 4 6 12 8
2. Write a C++ program for performing operations on a 2 x 2 matrix. Your program should do the
following operations:
a. Input the elements of the matrix.
b. Output the elements of the matrix.
c. Find if the matrix is symmetric or not.
Sample Output
element (0 , 0): 2
element (0 , 1): 5
element (1 , 0): 5
element (1 , 1): 2
2 5
5 2
It is a symmetric matrix
5. Laboratory Instructions
Please follow our hints for “Good Programming Practices” and avoid our hints for “Common
Programming Mistakes” that were mentioned in previous labs.
6. Laboratory #6 Exercises
Write a C++ program that output the 1 to 10 times tables. It should declare a two-dimensional array that
has 11 rows and 11 columns. It should initialize the array such that each element of the array equals the
product of indices. Print the 1 to 10 times table as shown in sample output.
Sample Output
The 1 times table
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
The 2 times table
ISC 115 Computing Foundations Page 25
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
…
…
…
The 10 times table
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
The objective of this laboratory is to train students on how to use Functions as an important
C++ Programming tool in modular programming.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
You can imagine that the "black box" takes some inputs, performs some processing on the
input(s) inside the box, and produces some output(s). How a function will use the inputs to come
up with the outputs is the essence of the function.
Type: is the type of data returned by the function. Or void for a function with no value returned.
Function_name: is the name by which the function can be called.
Arguments: Each argument consists of a type of data followed by its identifier, like variable
declaration (exe: int x). Or void for a function without parameters.
Function body: It can be a single instruction or a block of instructions delimited by curly brackets
{}.
Recursive Function
write a function integerPower(base, exponent) that returns the value of baseexponent. For
example, integerPower(3, 4) = 34 = 3 * 3 * 3 * 3. Assume exponent is a positive, nonzero
integer and that base is an integer. The function integerPower should use for or while to
control the calculation. Use this function in a program that inputs the base and exponent then it
calls the function integerPower to display the result.
Sample Output
enter the base 3
enter the exponent 4
3 raised to the power of 4 is 81
5. Laboratory Instructions
6. Laboratory #7 Exercises
Write a program that inputs 5 integers and passes them one at a time to function even, which uses the
modulus operator to determine whether an integer is even. The function should take an integer argument
and print whether the number is even or not.
Sample Output
Enter an integer: 5
5 is odd
Enter an integer: 100
100 is even
Enter an integer: 93
93 is odd
Enter an integer: 28
28 is even
Enter an integer: 10
10 is even
1. Laboratory Objective
The objective of this laboratory is to train students on how to integrate the knowledge gained
in the C++ programming and apply it skillfully in programming solutions for real life
problems.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
a. Apply the programming skills gained throughout the C++ Programming in solving real life
problems.
b. Gain appropriate experience in developing modular C++ Programs.
Write a C++ program that inputs 10 scores, stores it in a one dimensional array and computes the
standard deviation of the 10 scores. The formula for computing the standard deviation is:
To compute deviation with this formula, you have to store the individual scores using an
array, so that they can be used after the mean is obtained. Use {1, 2, 3, 4, 5, 6, 7, 8,
9, 10} to test the program.
Sample Output
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
5. Laboratory Instructions
Please follow our hints for “Good Programming Practices” and avoid our hints for “Common
Programming Mistakes” that were mentioned in previous labs.
Write a function drawHistograms that accepts one integer parameter n and displays n adjacent
asterisks. Use this function in a program that inputs (reads) five integers, passes the integers to
function drawHistograms one at a time to display the adjacent asterisks.
Sample Output
Enter a number 1
*
Enter a number 2
* *
Enter a number 3
* * *
Enter a number 4
* * * *
Enter a number 5
* * * * *
1. Laboratory Objective
The objective of this laboratory is to train students on how to integrate the knowledge gained
in the C++ programming and apply it skillfully in programming solutions for real life
problems II.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
a. Apply the programming skills gained throughout the C++ Programming in solving real life
problems.
b. Gain appropriate experience in developing modular C++ Programs.
Write a C++ program that calculates and displays the value of the following series:
. You should use a for loop that counts up from 1 to n. The sum
should be of type double.
Sample Output
Enter the value of n: 3
Enter the value of x: 2
the value of the series is 0.875
5. Laboratory Instructions
Please follow our hints for “Good Programming Practices” and avoid our hints for “Common
Programming Mistakes” that were mentioned in previous labs.
6. Laboratory Exercises
Write a C++ program that calculates and displays the value of the following series:
. You should use a for loop that counts up from 1 to n. The sum
should be of type double.
Sample Output
Enter the value of n: 5
Enter the value of x: 2
the value of the series is 0.65625
1. Laboratory Objective
The objective of this laboratory is to train students on how to integrate the knowledge gained
in the C++ programming and apply it skillfully in programming solutions for real life
problems.
2. Laboratory Learning Outcomes: After conducting this laboratory students will be able to:
a. Apply the programming skills gained throughout the C++ Programming in solving real life
problems.
b. Gain appropriate experience in developing modular C++ Programs.
Write a C++ program that creates a two-dimensional array with 5 rows and 5 columns. Fill the two-
dimensional array with random numbers in the range 1 to 10, inclusive. Print the two-dimensional
array. Then, find and display the maximum number in each row.
Sample Output
2 8 5 1 10
5 9 9 3 5
6 6 2 8 2
2 6 3 8 7
2 5 3 4 3
row 0: max = 10
row 1: max = 9
row 2: max = 8
row 3: max = 8
row 4: max = 5
5. Laboratory Instructions
Please follow our hints for “Good Programming Practices” and avoid our hints for “Common
Programming Mistakes” that were mentioned in previous labs.
Write a C++ program that input the elements of two 2 x 2 matrices. Then, it will output the sum of the
two matrices.
Audio CDs or applications with audio output may only be used with headphones with minimum
volume such that it should not disturb others.
All cell phones are to be turned off or set to silent while in the lab. If you receive a phone call,
you should exit the lab before answering your cell phone.
Any file saved on the computer hard drive will be deleted without notice. Students should save
their work onto an external storage device such as USB drive or CD.
Changing hardware and software configurations in the computer labs is prohibited. This includes
modifications of the settings, modifications of system software, unplugging equipment, etc.
Open labs are reserved for academic use only. Use of lab computers for other purposes, such as
personal email, non-academic printing, instant messaging, playing games, and listening to music
is not permitted.
Please leave the computer bench ready for the next patron. Leave the monitor on the login screen,
and do not forget to take all your personal belongings before leaving the lab. While leaving
computer bench please push the chair inside the computer bench.
Users are responsible for their own personal belongings and equipment. Do not leave anything in
the Computer Lab unattended for any length of time. The Computer Labs staffs are not
responsible for lost or stolen items.
Users are not allowed to clear paper jams in the printer by themselves.
After using white-board the user must clean for other user.
4 Mariam Al-Otaibi