0% found this document useful (0 votes)
20 views15 pages

Data Structures and Algorithms Experiment 1

dsa lab manual

Uploaded by

janvijain996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

Data Structures and Algorithms Experiment 1

dsa lab manual

Uploaded by

janvijain996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SVKM’s NMIMS University

School of Technology Management & Engineering, Indore


COURSE: Data Structures and Algorithms

Experiment No. 01

PART A

A.1 Aim:
Implementation of various array operations like
Traversal
Insertion
Deletion

A.2 Prerequisite:
Basics of Array, C++

A.3 Outcome:
After successful completion of this experiment students will be able to:

1. Operations on arrays
2. Traversing
3. Insertion
4. Deletion

A.4 Theory:

Arrays:-

DATA STRUCTURE - ARRAY

An array is defined as an ordered set of similar data items.  All the data items of
an array are stored in consecutive memory locations in RAM.  The elements of an
array are of same data type and each item can be accessed using the same name. 
Array is a foundation of other data structures.

For example other data structures such as Linked List, Stack, and Queue etc. are
implemented using array.
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
BASIC OPERATIONS ON ARRAYS

Following are the basic operations supported by an array.

 Traverse − print all the array elements one by one.

 Insertion − Adds an element at the given index.

 Deletion − Deletes an element at the given index.

 Search − Searches an element using the given index or by the value.

 Update − Updates an element at the given index

Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should store:

B.1 Answers of Task to be written by student:

**********************

 Insertion − Adds an element at the given index.


#include<iostream>

#include<array>

using namespace std;

class insert_into_arr{

public:

int arr[10],i,j,insert_num,insert_index;

void create_array(){

for(i=0;i<7;i++){

cin>>arr[i];
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
}

void display_array(){

cout<<"\nArray before insertion:";

for(i=0;i<7;i++){

cout<<arr[i]<<" ";

void insert_new_number(){

cout<<"\nEnter the index at which you want to insert the number:";

cin>>insert_index;

cout<<"\nEnter the number at you want to insert:";

cin>>insert_num;

for(i=8;i>=insert_index;i--){

arr[i+1]=arr[i];

arr[insert_index]=insert_num;

int s=sizeof(arr)/sizeof(arr[0]);

cout<<"\nSize of the array is:"<<s;

cout<<"\nArray After insertion:";

for(i=0;i<8;i++){

cout<<arr[i]<<" ";

}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms

};

int main(){

insert_into_arr a1;

a1.create_array();

a1.display_array();

a1.insert_new_number();

return 0;

}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
 Deletion − Deletes an element at the given index
#include<iostream>
#include<array>
using namespace std;
class array_deletion{
public:
int i,j,arr[5],delete_ele,delete_ind,temp;
void create_arr(){
for(i=0;i<5;i++){
cin>>arr[i];
}
}
void display_arr(){
cout<<"Array before deletion:";
for(i=0;i<5;i++){
cout<<arr[i]<<" ";
}
}
void delete_element(){
cout<<"\nEnter the element you want to delete:";
cin>>delete_ele;
for(i=0;i<5;i++){
if(arr[i]==delete_ele){
temp=i;
for(i=temp;i<5;i++){
arr[i]=arr[i+1];
}
}
}
cout<<"Array after deletion:";
for(i=0;i<4;i++){
cout<<arr[i]<<" ";
}
}
};
int main(){

array_deletion d1;
d1.create_arr();
d1.display_arr();
d1.delete_element();

return 0;
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
}

#include<iostream>

#include<array>

using namespace std;

class array_deletion{

public:

int i,j,arr[5],del_ele;

void create_array(){

for(i=0;i<5;i++){

cin>>arr[i];

void display_array(){

cout<<"Array before deletion:";

for(i=0;i<5;i++){
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
cout<<arr[i]<<" ";

void delete_array_element_by_index(){

cout<<"\nEnter the index you want to delete:";

cin>>del_ele;

for(i=0;i<5;i++){

if(i==del_ele){

for(i=del_ele;i<5;i++){

arr[i]=arr[i+1];

cout<<"Array after deletion:";

for(i=0;i<4;i++){

cout<<arr[i]<<" ";

};

int main(){

array_deletion d1;

d1.create_array();

d1.display_array();

d1.delete_array_element_by_index();
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
return 0;

 Traverse − print all the array elements one by one


#include<iostream>

#include<array>

using namespace std;

class array_traverse{

public:

int i,j,arr[10];

void create_array(){

for(i=0;i<5;i++){

cin>>arr[i];

void display_array(){

cout<<"The array is:";

for(i=0;i<5;i++){

cout<<arr[i]<<" ";
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
}

};

int main(){

array_traverse a1;

a1.create_array();

a1.display_array();

return 0;

 Update − Updates an element at the given index


#include<iostream>
#include<array>
using namespace std;
class array_updation{
public:
int i,j,arr[5],index,num;
void create_array(){
for(i=0;i<5;i++){
cin>>arr[i];
}
}
void display_array(){
cout<<"The original array is:";
for(i=0;i<5;i++){
cout<<arr[i]<<" ";
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
}
}
void arr_updation(){
cout<<"\nEnter the index at which you want to update:";
cin>>index;
cout<<"\nEnter the number you want to add:";
cin>>num;
arr[index]=num;
cout<<"The new array is:";
for(i=0;i<5;i++){
cout<<arr[i]<<" ";
}
}
};
int main(){

array_updation u1;
u1.create_array();
u1.display_array();
u1.arr_updation();
return 0;
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
 Search − Searches an element using the given index or by the
value
#include<iostream>
#include<array>
using namespace std;
class array_searching{
public:
int i,j,arr[10],search_ele,search_element_index;
void create_array(){
for(i=0;i<10;i++){
cin>>arr[i];
}
}
void display_array(){
cout<<"Original array is:";
for(i=0;i<10;i++){
cout<<arr[i]<<" ";
}
}
void search_element(){
cout<<"\nEnter the element you want to search:";
cin>>search_ele;
for(i=0;i<10;i++){
if(arr[i]==search_ele){
search_element_index=i;
break;
}
}
cout<<"\nThe entered element is present at "<<search_element_index<<" index
";
}
};
int main(){
array_searching s1;
s1.create_array();
s1.display_array();
s1.search_element();
return 0;
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms

Real life application of arrays:


#include<iostream>
#include<string>
using namespace std;
class array_application{
public:
int i,index,temp,p,update_index;
string student_names[10],new_name,del_name,search_nam,update_name;
void create_array(){
cout<<"Enter the names of the students:"<<endl;
for(i=0;i<5;i++){
getline(cin,student_names[i]);
}
}
void display_array(){
for(i=0;i<5;i++){
cout<<student_names[i]<<" ";
}
}
void insert_name(){
cout<<"\nEnter the index at which you want to insert the name:";
cin>>index;
cin.ignore();
cout<<"\nEnter the name you want to insert:";
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
getline(cin,new_name);
for(i=5;i>=index;i--){
student_names[i+1]=student_names[i];
}
student_names[index]=new_name;
for(i=0;i<6;i++){
cout<<student_names[i]<<" ";
}
}
void delete_name(){
cout<<"\nEnter the name you want to delete:";
getline(cin,del_name);
for(i=0;i<6;i++){
if(student_names[i]==del_name){
temp=i;
for(i=temp;i<5;i++){
student_names[i]=student_names[i+1];
}
}
}
cout<<"\nArray after deletion:";
for(i=0;i<5;i++){
cout<<student_names[i]<<" ";
}
}
void search_name(){
cout<<"\nEnter the element you want to search:";
getline(cin,search_nam);
for(i=0;i<5;i++){
if(student_names[i]==search_nam){
p=i;
}
}
cout<<search_nam<<" is present at "<<p<<"th index"<<endl;
}
void update_nam(){
cout<<"\nEnter the index at which you want to update:";
cin>>update_index;
cin.ignore();
cout<<"\nEnter the name you want to update:";
getline(cin,update_name);
for(i=0;i<5;i++){
if(i==update_index){
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms
student_names[i]=update_name;
}
}
cout<<"\nAfter updation of the array:";
for(i=0;i<5;i++){
cout<<student_names[i]<<" ";
}
}

};
int main(){
array_application a1;
a1.create_array();
a1.display_array();
a1.insert_name();
a1.delete_name();
a1.search_name();
a1.update_nam();

return 0;
}
SVKM’s NMIMS University
School of Technology Management & Engineering, Indore
COURSE: Data Structures and Algorithms

PART B

Roll No. : A011 Name: Janvi Jain

Class : Btech (AI & DS) 3 SEM Batch :

Date of Experiment: 19/07/24 Date of Submission: 25/07/24

You might also like