You are on page 1of 12

LABORATORY MANUAL

Subject Name: CS09204 Data Structures and Algorithms


Student Name Rahhim Adeem
Reg. No. 70129477
Date 17th –Feb-2023

MAPPING OF LAB TO CLOs & PLOs


CLO 3: Implement commonly used data structures and PLO 5: Modern Tool Usage
algorithms using programming software. Cognitive Domain: C3

Rubric for Modern Tool Usage


Criteria Attainment Score
Excellent Very good Good Fair Poor
(100-85%) (84-71%) (70-61%) (60-50%) (49-0%)
Understanding Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
of Engineering skillful ability to very good ability good ability to some ability to minimal or no
Tools describe and to describe and describe and/or describe ability to describe
explain the explain the explain the and/or explain and/or explain the
principles behind principles behind principles behind the principles principles behind
and applicability and applicability and applicability behind and and applicability
of engineering of engineering of engineering applicability of engineering
tools. tools. tools. of engineering tools.
tools.
Ability to Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
perform skillful ability to very good good ability to some ability to minimal or no
experiment identify and use ability to identify identify and use identify or use ability to identify
using the most relevant and use relevant tools for an tools for an or use tools for an
Engineering
tools for a range of tools for an engineering engineering engineering
Tool (Dev C++)
engineering engineering activity, but may activity. activity.
activities. activity. not identify the
most relevant
tool.
Generation and Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
Interpretation skillful ability to very good ability good ability to some ability to minimal or no
of results using generate results to generate generate results generate ability to generate
modern tools using modern results using using modern results using results using
(Dev C++)
tools. modern tools. tools. modern tools. modern tools.
Ability to Demonstrates Demonstrates a Demonstrates Demonstrates Demonstrates
relate skillful ability to very good ability good ability to some ability to minimal or no
experiment understand to understand understand understand ability to
with theory significance of significance of significance of significance of understand
and its experiment and its experiment and experiment and its experiment significance of
relation to the its relation to the relation to the and its relation experiment and its
significance
theory theory theory. to the theory relation to the
theory

Engr. Muhammad Arslan Rafique


Lab 01: Implementation of insertion, deletion, linear search and binary search
on an array type data structure.
Tool Used: Dev C++
Objectives: The basic objective of the lab is to learn how to perform different operations on array
type data structure like Insertion, deletion, linear search and binary search etc.
Theory:

Arrays

The array is a basic abstract data type that holds an ordered collection of items accessible by an


integer index. These items can be anything from primitive types such as integers to more
complex types like instances of classes. Since it's an ADT, it doesn't specify an implementation,
but is almost always implemented by an array (data structure) or dynamic array.
Arrays have one property: they store and retrieve items using an integer index. An item is stored
in a given index and can be retrieved at a later time by specifying the same index. The way these
indices work is specific to the implementation, but you can usually just think of them as the slot
number in the array that the value occupies. Take a look at the image below:

One Dimensional Array with Six Elements

An array is a series of elements of the same type placed in contiguous memory locations that
can be individually referenced by adding an index to a unique identifier.

That means that, for example, five values of type int can be declared as an array without
having to declare 5 different variables (each with its own identifier). Instead, using an array,
the five int values are stored in contiguous memory locations, and all five can be accessed using
the same identifier, with the proper index.
For example, an array containing 5 integer values of type int called A could be represented as

Engr. Muhammad Arslan Rafique


Declaring 1-D Arrays:

To declare an array in C++, the programmer specifies the type of the elements and the number
of elements required by an array as follows:

type arrayName [ arraySize ];

This is called a single-dimension array. The arraySize must be an integer constant greater
than zero and type can be any valid C++ data type. For example, to declare a 10-element array
called balance of type double, use this statement:

Double balance[10]

Initializing 1-D Arrays:

You can initialize C++ array elements either one by one or using a single statement as
follows:

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } cannot be larger than the number of elements that
we declare for the array between square brackets [ ]. Following is an example to assign a
single element of the array:

If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous

example. balance[4] = 50.0;

The above statement assigns element number 5th in the array a value of 50.0. Array with
4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first
element which is also called base index. Following is the pictorial representation of the same
array we discussed above:

Accessing Array Elements:

An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example:

double salary = balance[9];

Engr. Muhammad Arslan Rafique


Example 1:
#include<iostream>
#include<iomanip>
using namespace std;
int main ()
{
int

n[10];
for(int i =0; i <10;i+
+)
{
n[ i ]= i +100;
}
cout<<"Element"<<setw(13)<<"Value"<<endl;

for(int j =0; j <10; j++)


{
cout<<setw(7)<< j <<setw(13)<< n[ j ]<<endl;
}
System(“pause”)
;
return0;
}

Multi-Dimensional Array:

A multi-dimensional array is an array of more than one array.

An array of two one-dimensional arrays is called two dimensional array. It is also known as
table or matrix. Each elements of the two-dimensional array is referenced by its index values or
st nd
subscripts. For 2-D array there are 2 subscripts, 1 for row and 2 for column.

The C++ compiler supports a maximum of 12-subscripted arrays.

Declaration of Two-Dimensional Arrays:

Engr. Muhammad Arslan Rafique


type array-name [r][c];
To declare an integer type table “abc” having 12 rows and 3 columns, the declaration statement
is written as:

int abc [12][3];

The total number of elements in the above array is 12 * 3 = 36.

Initializing Tables:

To assign values to a table abc[2][3] that has two rows and three columns, values are assigned
row-wise.
int abc[2][3] = {{16,33,2}, {10,12,16}};

char abc[3][3] = {{“a”, “b”, “c”},{“d”, “e”, “f”},{“g”, “9”,”8”}};

Accessing Data in Two-Dimensional Arrays:

Data is entered into individual elements of a two-dimensional array. To enter data, the element is
referenced by its index or subscript value. Similarly, data is retrieved from an array from
individual elements of the array.
Usually nested loops are used to access elements of the 2-D array.

Linear Search:

Linear search or sequential search is a method for finding a particular value in a list that consists
of checking every one of its elements, one at a time and in sequence, until the desired one is
found. It is the most basic of search algorithm you can have.

The worst case performance scenario for a linear search is that it needs to loop through the entire
collection; either because the item is the last one, or because the item isn't found. In other words,
if you have N items in your collection, the worst case scenario to find an item is N iterations.
This is known as O(N) using the Big O Notation. The speed of search grows linearly with the
number of items within your collection.

Binary Search:

A binary search or half-interval search algorithm locates the position of an item in a sorted array.
Binary search works by comparing an input value to the middle element of the array. The
comparison determines whether the element equals the input, less than the input or greater. When
the element being compared to equals the input the search stops and typically returns the position
of the element. If the element is not equal to the input then a comparison is made to determine
whether the input is less than or greater than the element. A binary search is a divide and conquer
search algorithm.

Engr. Muhammad Arslan Rafique


Following link show the step wise evaluation of algorithm.

For binary search the array should be sorted. For this purpose, a sorting algorithm has to be
performed on the array. Let’s perform the bubble sort algorithm. It works on the following
principle.

Engr. Muhammad Arslan Rafique


Lab Tasks:
1. Write a program to input float type data into a table having 5 rows and 4 columns. Find out
the maximum number entered in the table and print it on the screen.

INPUT:

OUTPUT:

Engr. Muhammad Arslan Rafique


2. Write a program in which user input an element to delete, the element is then searched
in the array, if it is found it is deleted and new array is displayed. If the element is not
present in the array, then a not found message is displayed.

INPUT:

OUTPUT:

Engr. Muhammad Arslan Rafique


3. Write a program in which user insert an element in an array if an index is available for
element. If the no index is available for the new element, then no space message is displayed.

INPUT:

OUTPUT:

Engr. Muhammad Arslan Rafique


4. Implement the Linear Search Algorithm on an array. Calculate the total time required to
search an element?

INPUT:

OUTPUT:

As for the time complexity of this algorithm, it is O(n), where n is the size of the array. This is
because in the worst case scenario, the algorithm has to iterate through the entire array to find the
element. In the best case scenario, the element is found at the first index, so the algorithm only
needs to do one comparison. However, the average case time complexity is also O(n) because the
element could be anywhere in the array

Engr. Muhammad Arslan Rafique


5. Implement the Binary Search Algorithm on an array. Calculate the total time required to
search an element?
INPUT:

OUTPUT:

As for the time complexity of this algorithm, it is O(log n), where n is the size of the array. This
is because the algorithm repeatedly divides the array in half until it finds the element or
determines that it is not in the array.(average case)

Engr. Muhammad Arslan Rafique


Lab Assessment
Understanding Ability to Generation and Ability to
of Engineering perform Interpretation of relate
Tools experiment using results using experiment Total
(Criteria 1) Engineering Tool modern tools with theory and 15
2 (Dev C++) (Dev C++) its significance Mark
(Criteria 2) (Criteria 3) (Criteria 4) s
6 4 3
Task 1

Task 2

Task 3

Task 4

Task 5

Averag
e Marks

Lab Engineer Name: M. Arslan Rafique Signature: ___________________

Engr. Muhammad Arslan Rafique

You might also like