Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Department of Information Technology

DEPARTMENT OF INFORMATION
TECHNOLOGY

Practical File

B. Tech (IT) 2nd Year – 3rdSemester

Subject: Data Structure & Algorithms Lab


BTIT303-18

CHANDIGARH ENGINEERING COLLEGE

(LANDRAN)

Faculty: - Submitted by: -

Mr. Amitabh Sharma

Roll No: Page 1


Department of Information Technology

INDEX

Sr. No. Name of the Experiment Page No


1 Write a program to insert a new element at end as well as at a 3-5
given position in an array.
2

10

11

12
13
14 i.

15 i.

16

17

18

19
20

Roll No: Page 2


Department of Information Technology

Experiment No: 1
Title: Write a program to insert a new element at end as well as at a given
position in an array.

Array: An array is defined as finite ordered collection of homogenous data,


stored in contiguous memory locations.

 finite means data range must be defined.


 ordered means data must be stored in continuous memory addresses.
 homogenous means data must be of similar data type.

Syntax: data_type array_name[array_size];

Program Code:

#include <iostream>

using namespace std;

int main () {

int A [50], pos, c, n, value, ch, j;

cout<<"Enter number of elements in the array"<<endl;

cin>>n;

cout<<"Enter "<<n<<" elements"<<endl;

for (c = 1; c <= n; c++)

cin>>A[c];

cout<<"Press 1 to insert at end and 2 to insert at specified


position"<<endl;

cin>>ch;

if (ch == 1) {

pos=n+1;

cout<<"Please enter the value"<<endl;

cin>>value;
Roll No: Page 3
Department of Information Technology

for (c = 1; c <= pos; c++)

A[pos] = value;

cout<<"Resultant array is"<<endl;

for (c = 1; c <= pos; c++)

cout<<A[c]<<" ";

else {

cout<<"enter position where u wish to insert element"<<endl;

cin>>pos;

for (int j=n; j>=pos; j--) {

A[j+1] =A[j];

cout<<"enter the value u want to insert"<<endl;

cin>>A[pos];

cout<<"resultant array is "<<endl;

for (j=1; j<=n+1; j++) {

cout<<A[j]<<"\t";

Output of Code:

Roll No: Page 4


Department of Information Technology

Roll No: Page 5

You might also like