0% found this document useful (0 votes)
210 views

Lab Manual DSA

Uploaded by

Deepanshu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
210 views

Lab Manual DSA

Uploaded by

Deepanshu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Structures

Lab Manual
Department of Computer Science and Engineering
The NorthCap University, Gurugram
EWA Lab Manual (CSL 273) | i
2019-20

Data Structures
Lab Manual
CSL 209

Pooja Sapra

Department of Computer Science and Engineering

NorthCap University, Gurugram- 122001, India

Session 2021-22

Published by:

School of Engineering and Technology

Department of Computer Science & Engineering

The NorthCap University Gurugram

• Laboratory Manual is for Internal Circulation only


© Copyright Reserved

No part of this Practical Record Book may be

reproduced, used, stored without prior permission of The NorthCap University

Copying or facilitating copying of lab work comes under cheating and is considered as use
of unfair means. Students indulging in copying or facilitating copying shall be awarded zero
marks for that particular experiment. Frequent cases of copying may lead to disciplinary
action. Attendance in lab classes is mandatory.

Labs are open up to 7 PM upon request. Students are encouraged to make full use of labs
beyond normal lab hours.

PREFACE
Data Structures Lab Manual is designed to meet the course and program requirements of
NCU curriculum for B.Tech II year students of CSE branch. The concept of the lab work is to
give brief practical experience for basic lab skills to students. It provides the space and
scope for self-study so that students can come up with new and creative ideas.

The Lab manual is written on the basis of “teach yourself pattern” and expected that
students who come with proper preparation should be able to perform the experiments
without any difficulty. Brief introduction to each experiment with information about self-
study material is provided. The laboratory exercises will include familiarization with linear
and non-linear data structures starting with the basics of Arrays, Linked Lists to advanced
data structures like Trees & Graphs along with their implementation in Java programming
language that covers all the basic operations. Students would attain the practical
understanding of different kinds of data structures along with their implementation and
operations performed. Finally, the students would require applying the practical concepts
of data structures learned in solving real time problems that involves their basic usage and
applicability. At the start of each experiment a question bank for preparation and practice
is suggested which may be used to test the basic understanding of the students about the
experiment. Students are expected to come thoroughly prepared for the lab. General
disciplines, safety guidelines and report writing are also discussed.
The lab manual is a part of curriculum for the TheNorthCap University, Gurugram.
Teacher’s copy of the experimental results and answer for the questions are available as
sample guidelines.

We hope that lab manual would be useful to students of CSE, IT, ECE and BSc branches and
author requests the readers to kindly forward their suggestions / constructive criticism for
further improvement of the work book.

Author expresses deep gratitude to Members, Governing Body-NCU for encouragement and
motivation.
Authors
The NorthCap University
Gurugram, India

CONTENTS
S.N. Details Page No.

Syllabus
1 Introduction

2 Lab Requirement

3 General Instructions

4 List of Experiments

5 List of Flip Assignment

6 List of Projects

7 Rubrics

8 Annexure 1 (Format of Lab Report)

9 Annexure 2 (Format of Project Report)

COURSE TEMPLATE

1. Department: Department of Computer Science and Engineering

2. Course Name: Data Structures 3. Course Code 4. L-T-P 5. Credits


CSL209 3-0-2 4

6. Type of Course
(Check one): Programme Core ü Programme Elective Open Elective

7. Pre-requisite(s), if any: FOCP-II


8. Frequency of offering (check one): Odd ü Even Either semester Every semester

9. Brief Syllabus:

Solving computational problems requires the knowledge of efficient data organization and the ability to make
effective choices among multiple solutions. In this course, we will explore several fundamental data structures in
computer science and learn to implement them in Java. The course aims to teach the fundamentals of data
structures, their design, implementation and effective use in problem solving approach. With the knowledge of data
structures and practical experience in implementing them, students can become much more effective designer and
developer. The course will start with the basic introduction of linear as well as non-linear data structures and further
proceeds with the programming intensive task of implementing them. This course will also cover file organization
and different hashing techniques in its last module.
Total lecture, Tutorial and Practical Hours for this course (Take 15 teaching weeks per semester): 75

Practice
Lectures: 45 hours
Tutorials : 0 hours Lab Work: 30 hours

10. Course Outcomes (COs)


Possible usefulness of this course after its completion i.e. how this course will be practically useful to him once
it is completed

To understand and implement various data structures like Arrays, Linked Lists, Stack, Queues,
CO 1
Tree, Graphs and File Organization

CO 2 To be able to illustrate the basic operations of data structures Arrays and Linked List using Java

CO 3 To analyze Stacks and Queues operations and calculate their complexities

To apply mathematical functions, Algorithmic principles and theoretical concepts to the modelling
CO 4
and designing of solutions for Non-linear Data Structures Trees and Graphs

To use sound development principles to implement various linear and non-linear data structures of
CO 5
varying complexities for Sorting the elements and organization of files

11. UNIT WISE DETAILS No. of Units: 5

Unit Number: 1 Title: Object Oriented Programming and Data Structure No. of hours: 8
Content Summary:
Java Software Development Tools, Wrapper Classes, The ArrayList, Interfaces, Data & its meaning, Types of data
structures, Linear and Non-linear data structures, Introduction to Algorithms and analyzing time complexities
Unit Number: 2 Title: Arrays and Linked Lists No. of hours: 8
Content Summary:
Introduction to Arrays, Representation in Row major and Column major order, Array traversal, Linear and Binary
search, Linked list introduction, Insertion and Deletion, Doubly Linked list, Circular Linked list and their
implementation
Unit Number: 3 Title: Stacks & Queues No. of hours: 10
Content Summary:
Stack operations Push, pop, peek, Implementation using arrays and linked list, Stack applications, Polish Notations,
Introduction to Queues, Implementation using Arrays and Linked Lists, Circular Queues, Doubly ended queues
(Dqueues), Priority Queues applications and implementation
Unit Number: 4 Title: Trees & Graphs No. of hours: 12
Content Summary:
Introduction to Binary Trees, Expression trees, Tree traversal: Inorder, Preorder & Postorder, Tree implementation
using Array and Linked List, Introduction to Binary Search Tree, Insertion and Deletion in BST, AVL Trees, Insertion
and Deletion, B Trees: Insertion and traversal
Introduction to Graphs, Representation, Graph traversal: Breadth First Search & Depth First Search, Applications
Unit Number: 5 Title: Sorting Algorithms & File Organization No. of hours: 7
Content Summary:
Sorting introduction, Bubble Sort, Quick Sort, Merge Sort and Heap Sort Implementation and analysis, File
Organization: File representation on hard disks, Indexing and Hashing techniques, Linear probing & Quadratic
probing
12. Brief Description of Self-learning components by students (through books/resource material etc.):
Object-Oriented Design with UML
GUI Programming

Books Recommended :
Text Books:
1) Robert Lafore, “Data Structures and Algorithms in Java”, SAMS publications, 2 nd Edition, 2002
2) Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser, “Data Structures and Algorithms in Java”,
Wiley Publications, 6th Edition, 2014

Reference Books:
3) Seymour Lipschutz, “Data Structures”, Schaum’s Outlines, McGraw Hill Education, 1 st Edition, 2014
4) Aaron M. Tannenbaum, Yedidyah Langsam, Moshe J.Augenstein, “Data Structure using C”, Pearson India,
1st Edition, 2018

Electronic Textbooks available through the ASU Library:


JAVA For Everyone: Late Objects, 2nd Edition, Cay S. Horstmann.
(https://bcs.wiley.com/he-bcs/Books?action=index&bcsId=6907&itemId=1118063317)

Reference Websites: (nptel, swayam, coursera, edx, udemy, lms, official documentation weblink)
https://canvas.asu.edu/courses/58080
https://lms.ncuindia.edu/course/view.php?id=323
https://www.coursera.org/learn/data-structures?specialization=data-structures-algorithms
https://swayam.gov.in/nd2_cec19_cs04/preview
https://www.edx.org/course/data-structures-an-active-learning-approach

Practice (Tutorial/Case Studies/ Industry Visit/Field Work) Content

Sr. No. Topic Unit covered


1. Different data structures concepts 1
2. Time complexities of algorithms 1
3. Conversion of infix to postfix and prefix, postfix to prefix 1
and infix, prefix to postfix and infix
4. Binary tree operations, AVL tree operations 4
5. Breadth-first search and depth-first search 4
6. Shortest path using djikstra algorithm 5

Reference Practical Content

Sr. No. Title of the Experiment Software/Hard Unit Time


ware based covered Required
1. (i) Write a java program to make an array of Software 1 4
specified length asking from user.

(ii) Write a java program to write various


operations on arrays such as finding element in
array, find duplicate values in array, sort an array
etc.

(iii) Write a java program to write linear and binary


search using arrays.
2. (i) Write a java program to create a singly linked list Software 2 8
of specified length asking from user.

(ii) Write a java program to write various


operations on linked list such as add an element in
linked list, remove or sort a linked list etc.

(iii) Write a java program to create a circular linked


list and perform various operations on it.

(iv) Write a java program to create a doubly linked


list and perform various operations on it.

(v) Write a java program to create a circular doubly


linked list and perform various operations on it.

3. (i) Write a java program to represent stack using Software 3 4


arrays and perform operations such as pop, push,
peek etc.

(ii) Write a java program to represent stack using


linked list perform operations such as pop, push,
peek etc.
4. (i) Write a java program to represent queue using Software 3 4
arrays and perform operations such as add, remove,
search in queue using front, rear pointers etc.

(ii) Write a java program to represent queue using


linked list perform operations such as add, remove,
search in queue using front and rear pointers etc.

(iii) Write a java program to implement stack using


queue.
(iv) Write a java program to implement queue using
stack.
5. (i) Write a java program to create a binary search Software 4 4
tree using arrays and perform add, remove, search
operations in tree.

(ii) Write a java program to create a binary search


tree using linked lists and perform add, remove,
search operations in tree.

(iii) Write a java program to create binary tree for


inorder traversal using arrays/linked list.

(iv) Write a java program to create binary tree for


postorder traversal using arrays/linked list.

(v) Write a java program to create binary tree for


preorder traversal using arrays/linked list.
6. (i) Write a java program to implement breadth-first Software 4 2
search using arrays/linked list.

(ii) Write a java program to implement depth-first


search using arrays/linked list.
7. Write a java program to implement bubble sort. Software 5 1
8. Write a java program to implement merge sort. Software 5 1
9. Write a java program to implement quick sort. Software 5 1
10. Write a java program to implement heap sort. Software 5 1

Value Added Experiments

1. NA

Project (To be done as individual/in group): No

Evaluation Scheme (Choose one related to the course)

TYPE OF ALLOT
S. No. PARTICULAR PASS CRITERIA
COURSE TED
1 Theory+ Minor Test RANG
15% Must Secure 30% Marks Out of
Practical
Major Test 35% Combined Marks of Major Test
(L-T-P/L-0-P)
Continuous Evaluation 10% Plus Minor Test with Overall 40%
Through Class Marks in Total.
Tests/Practice/Assign
ments/Presentation/Q
uiz
Online Quiz 5%
Lab Work 35%
Mapping of PO’s and CO’s

PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 2 2 2 1 1 1 1 1 2 2 3 3 2 2 1
CO2 1 2 1 1 1 1 1 1 2 3 2 2 2 1 2
CO3 3 3 2 2 2 1 1 1 2 3 3 2 3 2 2
CO4 3 3 3 3 3 2 2 2 2 3 3 3 3 3 3
CO5 2 2 2 2 2 1 2 1 2 2 2 2 3 2 2

1. INTRODUCTION

That ‘learning is a continuous process’ cannot be over emphasized. The theoretical


knowledge gained during lecture sessions need to be strengthened through practical
experimentation. Thus, practical makes an integral part of a learning process.

The purpose of conducting experiments can be stated as follows:

 To understand and implement various data structures like Arrays, Linked Lists,
Stack, Queues, Tree and Graphs
 Reporting and analysing their complexities.
 To illustrate the basic functionality and operations performed
 To use sound development principles to implement various non-linear data
structures of varying complexities.
 Hands on experience on the experimental setup and software tools


2. LAB REQUIREMENTS

Requirements Details

Software Requirements Any programming editor (Sublime text/Notepad++)

Operating System Any Operating System

Hardware Windows and Linux:

Requirements 8 GB RAM (Recommended)

80 GB hard disk space

Required Bandwidth NA
3. GENERAL INSTRUCTIONS

3.1 General discipline in the lab

 Students must turn up in time and contact concerned faculty for the experiment
they are supposed to perform.
 Students will not be allowed to enter late in the lab.
 Students will not leave the class till the period is over.
 Students should come prepared for their experiment.
 Experimental results should be entered in the lab report format and
certified/signed by concerned faculty/ lab Instructor.
 Students must get the connection of the hardware setup verified before
switching on the power supply.
 Students should maintain silence while performing the experiments. If any
necessity arises for discussion amongst them, they should discuss with a very
low pitch without disturbing the adjacent groups.
 Violating the above code of conduct may attract disciplinary action.
 Damaging lab equipment or removing any component from the lab may invite
penalties and strict disciplinary action.

3.2 Attendance
 Attendance in the lab class is compulsory.
 Students should not attend a different lab group/section other than the one
assigned at the beginning of the session.
 On account of illness or some family problems, if a student misses his/her lab
classes, he/she may be assigned a different group to make up the losses in
consultation with the concerned faculty / lab instructor. Or he/she may work
in the lab during spare/extra hours to complete the experiment. No attendance
will be granted for such case.

3.3 Preparation and Performance


 Students should come to the lab thoroughly prepared on the experiments they
are assigned to perform on that day. Brief introduction to each experiment
with information about self-study reference is provided on LMS.
 Students must bring the lab report during each practical class with written
records of the last experiments performed complete in all respect.
 Each student is required to write a complete report of the experiment he has
performed and bring to lab class for evaluation in the next working lab.
Sufficient space in work book is provided for independent writing of theory,
observation, calculation and conclusion.
 Students should follow the Zero tolerance policy for copying / plagiarism. Zero
marks will be awarded if found copied. If caught further, it will lead to
disciplinary action.
 Refer Annexure 1 for Lab Report Format.
4. LIST OF EXPERIMENTS

Sr. Title of the Experiment Software used Unit CO Time


No. covered Covered Required
1 Create an array of integer with Sublime 2 CO1
size n. Return the difference
Text/Notepad++
between the largest and the
smallest value inside that array.
21. Write a program that initializes an Sublime 2 CO2
array with ten random integers
Text/Notepad++
and then prints four lines of
output, containing:
 Every element at an
even index
 Every odd element
 All elements in
reverse order
 Only the first and last
element
32 Write a program to read numbers Sublime 2 CO1
in an integer array of size 5 and
Text/Notepad++
display the following:
 Sum of all the elements
 Sum of alternate
elements in the array
 Second highest element
in the array

4 Write a program to create a singly Sublime 1 CO1


linked list of n nodes and
Text/Notepad++
perform:
• Insertion
o At the beginning
o At the end
o At a specific location
• Deletion
o At the beginning
o At the end
o At a specific location
51 Write a program to perform the Sublime 1 CO1
following set of operations on a
Text/Notepad++
singly linked list (SLL):
 Swapping the first and
last node of a singly
linked list
 Pairwise swap
elements of a given
linked list
 Get the location of
first and last
occurrence of an
element in a single
LinkedList
 Remove duplicates
from an unsorted
linked list
 Delete alternate nodes
of a Linked List
6 Write a program to check weather Sublime 1 CO1
the given linked lists are identical Text/Notepad++
or not. It should contain a static
function findIdentical() which
returns TRUE if the lists are
identical and FALSE otherwise.
7 Write a program to reverse the Sublime 1 CO1
linked list in pairs.
Text/Notepad++
If you have a linked list that holds
1 --> 2 --> 3 --> 4 --> 5, then
after the function has been called
the linked list would hold 2 --> 1
--> 4 --> 3 --> 5
8 Write a function that, given an Sublime 2 CO2
integer n, converts n to binary and
8 Text/Notepad++
stores each bit in one node of a
linked list with the least
significant bit at the head of the
list and the most significant bit at
the tail.
For example, given `13`, the bits
are stored in the order 1 0 1 1,
from head to tail.
9 Write a program to rotate a Sublime 2 CO2
doubly linked list (DLL) by a N
Text/Notepad++
nodes in a counter-clockwise
direction.
Example: consider the given
linked list
AàBàCàDàEàFàGàH

Rotate by 4th Node

EàFàGàHàAàBàCàD
10 Write a program to implement Sublime 2 CO2
stack using arrays and linked Text/Notepad++
lists.
11 Write a program to reverse a Sublime 2 CO2
sentence using stack. Text/Notepad++
12 Write a program to check for Sublime 2 CO2
balanced parenthesis in a given Text/Notepad++
expression.
13 Write a program to convert infix Sublime 2 CO2

expression to prefix and postfix Text/Notepad++


expression.
14 Sublime 3 CO3

Write a program to implement Text/Notepad++


Tower of Hanoi using stacks

15 Write a program to implement Sublime 3 CO3


Queue using Array and Linked Text/Notepad++
Lists.
16 Write a program to reverse the Sublime 3 CO3
first m elements from the queue, Text/Notepad++
where m < n, n is the total
number of elements in queue.
17 Write a program to find the Sublime 3 CO3
frequency of each element in a Text/Notepad++
circular queue.
18 Sublime 4 CO4
Write a Java program to insert
some elements into a binary tree Text/Notepad++
and perform in-order traversal of
the tree.
19 Write a Java program to Sublime 4 CO4
demonstrate insertion & deletion Text/Notepad++
operation in a binary search tree.
20 Given a binary tree and a number Sublime 4 CO4
N, write a Java program to find Text/Notepad++
the N-th node in the Post Order
traversal of the given binary tree.
21 Write a program to implement Sublime 4 CO4
Breadth First Search using Text/Notepad++
Adjacency Matrix/List.
22 Write a program to implement Sublime 5 CO5
Breadth First Search using Text/Notepad++
Adjacency Matrix/List.
23 Write a program to implement Sublime 5 CO5
Single Source Shortest Path Text/Notepad++
Algorithm.
24 Write a program to implement Sublime 6 CO6
Bubble Sort, Selection Sort and Text/Notepad++
Insertion Sort algorithm.
25 Write a program to implement Sublime 6 CO6
Quick Sort algorithm Text/Notepad++
26 Write a program to implement Sublime 6 CO6
Merge Sort algorithm Text/Notepad++

5. LIST OF FLIP EXPERIMENTS

1. Write a Java program to swap the first and last elements of an array (length must be at
least 5) and create a new array.
2. Write a Java program to segregate all 0s on left side and all 1s on right side of a given
array of 0s and 1s.
3. Write a program to rearrange a given list such that it consists of alternating minimum
maximum elements
4. Write a program for calculating the frequency of different values in a linked list of
integers with multiple entries.
5. Write a program in java to Check if stack elements are pairwise consecutive or not.
6. Write a program to reverse the first m elements from the queue, where m < n, n is the
total number of elements in queue.
7. Write a Java program to construct a binary tree using the given In-Order and Pre-Order
traversal
a. Example:
b. Input :
i. in[] = {4, 8, 2, 5, 1, 6, 3, 7}
ii. pre[] = {1, 2, 4, 8, 5, 3, 6, 7}

8. Write a program to implement Dijkstra's to calculate shortest path in graphs.

6. RUBRICS

Marks Distribution
Continuous Evaluation(50 Marks) End Semester Exam (20 Marks)
Each experiment shall be evaluated for 10 End semester practical evaluation
marks and at the end of the semester including Mini project (if any) carries 20
proportional marks shall be awarded out of marks.
50.
Following is the breakup of 10 marks for
each
4 Marks: Observation & conduct of
experiment. Teacher may ask questions
about experiment.
3 Marks: For report writing
3 Marks: For the 15 minutes quiz to be
conducted in every lab.
Annexure 1
(Student Lab Report)

Data Structures
(CSL 209)

Lab Practical Report

Faculty name: Dr. Pooja Sapra Student name: Deepanshu Kaushik

Roll No.: 20CSU351

Semester: 3rd Sem

Group: B2(AI and ML)

Department of Computer Science and Engineering


NorthCap University, Gurugram- 122001, India
Session 2021-22
INDEX
S.No Experiment Page Date of Date of Marks CO Sign
No. Experiment Submission Covered
Data Structure Lab Manual (CSL 209) | 1
2021-22
EXPERIMENT NO. 1

Student Name and Roll Number:Deepanshu Kaushik 20CSU351


Semester /Section: 3rd Sem / B2

Link to Code:

Date: 12/08/2021
Faculty Signature:

Marks:

Objective(s):

To familiarize the students with linear data structure array and its basic operations

Outcome:

The students will be able to implement and use arrays for solving various problems

Problem Statement:

Create an array of integer with size n. Return the difference between the largest and the smallest value inside
that array.

Background Study:

An Array is a data structure consisting of a collection of elements (values or variables), each


identified by at least one array index or key. An array is stored such that the position of each
element can be computed from its index tuple by a mathematical formula. The simplest type of data
structure is a linear array, also called one-dimensional array.
Data Structure Lab Manual (CSL 209) | 2
2021-22

Algorithm (Student Work Area):

1.Input the array elements(Taking Input from the user)

2.Initialize max_val = min = arr[0]

3.Repeat from i = 1 to i<n using for loop

4.if(arr[i] > max_val)

5.max_val = arr[i]

6.else(arr[i] < min)

7.min= arr[i]

8.Print difference(max_val-min).

Code (Student Work Area):

import java.util.*;
public class Difference {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter the size of array : ");
int i;
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter " + n + " elements of the array : ");
for(i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
Data Structure Lab Manual (CSL 209) | 3
2021-22
int max_val = arr[0];
int min = arr[0];
for(i = 1; i < n; i++)
{
if(arr[i] > max_val)
max_val = arr[i];
else if(arr[i] < min)
min = arr[i];
}
System.out.println("Difference between the largest and smallest values of the said array: "+
(max_val-min));
}
}

Question Bank:

1. What is Data Structure?

Data structure is a fundamental concept of any programming language, n essential for


algorithmic design. ... DS is how data and the relationship amongst different data is represented,
that aids in how efficiently various functions or operations or algorithms can be applied.

2. Why Array is called as Linear Data Structure?

It is called linear ds because the memory allocation of the data in array is contiguous unlike
list and trees.the memory is allocated initially and the elements are fed into it in a linear order till
the limit of array is encountered. Insertion in between is not possible.

3. What type of Indexing is used in Java?

 Presence Index
 Equality Index
 Substring Index
 Browsing Index
 Approximate Index
 International Index
Data Structure Lab Manual (CSL 209) | 4
2021-22

EXPERIMENT NO. 2

Student Name and Roll Number:

Semester /Section:

Link to Code:

Date:

Faculty Signature:

Marks:

Objective(s):

To familiarize the students with linear data structure array and its basic operations

Outcome:

The students will be able to implement and use arrays for solving various problems

Problem Statement:

1. Write a program that initializes an array with ten random integers and then prints four lines of
output, containing:
 Every element at an even index
 Every odd element
 All elements in reverse order
 Only the first and last element
Data Structure Lab Manual (CSL 209) | 5
2021-22
Background Study:

An Array is a data structure consisting of a collection of elements (values or variables), each


identified by at least one array index or key. An array is stored such that the position of each
element can be computed from its index tuple by a mathematical formula. The simplest type of data
structure is a linear array, also called one-dimensional array.

Algorithm (Student Work area):

Code (Student Work Area):

Question Bank:

1. How we can segregate all 0s on left side and all 1s on right side of a given array of 0s and
1s.
2. How to reverse the array elements.
3. How to find the index of an array element
4. How to remove a specific element from an array. 
5. How to insert an element (specific position) into an array.

 
Data Structure Lab Manual (CSL 209) | 6
2021-22

EXPERIMENT NO. 3

Student Name and Roll Number:

Semester /Section:

Link to Code:

Date:

Faculty Signature:

Marks:

Objective(s):

To familiarize the students with linear data structure array and its basic operations

Outcome:

The students will be able to implement and use arrays for solving various problems

Problem Statement:

3 Write a program to read numbers in an integer array of size 5 and display the following:
 Sum of all the elements
 Sum of alternate elements in the array
 Second highest element in the array

Background Study:

An Array is a data structure consisting of a collection of elements (values or variables), each


Data Structure Lab Manual (CSL 209) | 7
2021-22
identified by at least one array index or key. An array is stored such that the position of each
element can be computed from its index tuple by a mathematical formula. The simplest type of data
structure is a linear array, also called one-dimensional array.

Algorithm (Student Work Area):

Code (Student Work Area):

Question Bank:

1. How we can count occurrence of a given number in the array and its frequency.

2. How we can print the following in 2-D integer array with each element of maximum 2 digits

a) Elements of the entered array.


b) Elements of the array after each element is multiplied by 2 if it is an odd number.

3. Given an array of integers, return the number of times that two 6's are next to each other in
the array. Also count instances where the second element is 7

4. Write a method called swapPairs() that accepts an array of integers and swaps the elements at
adjacent indexes. That is, elements 0 and 1 are swapped, elements 2 and 3 are swapped, and so
Data Structure Lab Manual (CSL 209) | 8
2021-22
on. If the array has an odd length, the final element should be left unmodified. For example, the
list {10, 20, 30, 40, 50} should become {20, 10, 40, 30, 50} after a call to your method.

5. Write a method called median() that accepts an array of integers as its argument and returns
the median of the numbers in the array. The median is the number that will appear in the middle
if you arrange the elements in order.

You might also like