Exercise 1 - Array Manipulation

You might also like

You are on page 1of 4

20CA2013 Data Structures Lab

Exercise number : 1
Date: 15th July, 2021

Array Manipulation
• Problem 1 : To manipulate an array by modifying it’s elements.

• Aim : To enter, delete, insert and print the elements of an array using C++.

• Algorithm :
Step 1 : Start the program
Step 2 : Include iostream
Step 3 : Define a function read - Parameters : int array[10], int n; Return value : void
Step 4 : Define a function print - Parameters : int array[10], int n; Return value : void
Step 5 : Define a function insert - Parameters : int array[10], int n, int t, int position;
Return value : void
Step 6 : Define a function del - Parameters : int array[10], int n, int t; Return value : void
Step 7 : Inside function main traverse step 8 to step 8
Step 8 : Declare int array[10], n, t, position
Step 9 : Input n as number of array elements
Step 10 : Call function read - Parameters : array, n
Step 11 : Call function print - Parameters : array, n
Step 12 : Input t as the element to be deleted
Step 13 : Call function del - Parameters : array, n, t
Step 14 : Set n = n – 1
Step 15 : Call function print - Parameters : array, n
Step 16 : Input t as the element to be inserted
Step 17 : Input position as the position where the element is to be inserted
Step 18 : Call function insert - Parameters : array, n, t, position
Step 19 : Call function print - Parameters : array, n + 1
Step 20 : Return 0
Step 21 : End the program

Register number : URK20DA1009


Name : Judah Felix
20CA2013 Data Structures Lab
Function read(int array[10], int n) :

Step 1 : Declare int i


Step 2 : For i = 0; i < n; i++ repeat step 3
Step 3 : Input array[i] as the elements to be entered into the array
Step 4 : End the function

Function print(int array[10], int n) :

Step 1 : Declare int i


Step 2 : For i = 0; i < n; i++ repeat step 3
Step 3 : Display array[i]
Step 4 : End the function

Function insert(int array[10], int n, int t, int position) :

Step 1 : Declare int i


Step 2 : For i = n; i > position - 1 ; i-- repeat step 3
Step 3 : Set array[i] = array[i - 1]
Step 4 : Set array[position - 1] = t
Step 5 : End the function

Function del(int array[10], int n, int t) :

Step 1 : Declare int i


Step 2 : For i = 0; i < n; i++ repeat step 3 to step 4
Step 3 : If array[i] == t execute step 4
Step 4 : Break
Step 5 : For i; i < n + 1; i++ repeat step 6
Step 6 : Set array[i] = array[i + 1]
Step 7 : End the function

Register number : URK20DA1009


Name : Judah Felix
20CA2013 Data Structures Lab
• Source code :
#include<iostream>

using namespace std;

void read(int array[10], int n)
{
    int i;
    cout << "\n\nEnter the " << n <<" elements one by one : ";
    for(i = 0; i < n; i++)
    {
        cin >> array[i];
    }
}

void print(int array[10], int n)
{
    int i;
    cout << "\nThe array elements are : ";
    for(i = 0; i < n; i++)
        cout << array[i] << '\t';
}

void insert(int array[10], int n, int t, int position)
{
    int i;

    for(i = n; i > position - 1; i--)
        array[i] = array[i - 1];

    array[position - 1] = t;
}

void del(int array[10], int n, int t)
{
    int i;

    for(i = 0; i < n; i++)
    {
        if(array[i] == t)
            break;
    }

    for(i; i < n + 1; i++)
        array[i] = array[i + 1];
}

int main()
{
    int array[10], n, t, position;
    
    cout << "\nEnter the number of elements : ";
    cin >> n;
    
    read(array, n);
    print(array, n);

    cout << "\n\n\nEnter the element to be deleted : ";
    cin >> t;
    del(array, n, t);
    n--;

    print(array, n);

    cout << "\n\n\nEnter the element to be inserted : ";
    cin >> t;
    cout << "\nEnter the position where " << t << " is to be inserted : ";
    cin >> position;
    insert(array, n, t, position);

    print(array, n + 1);

Register number : URK20DA1009


Name : Judah Felix
20CA2013 Data Structures Lab
    return 0;
}

• Output :
Enter the number of elements : 10

Enter the 10 elements one by one : 10


20
30
40
50
60
70
80
900
100

The array elements are : 10 20 30 40 50 60 70 80 900 100

Enter the element to be deleted : 900

The array elements are : 10 20 30 40 50 60 70 80 100

Enter the element to be inserted : 90

Enter the position where 90 is to be inserted : 9

The array elements are : 10 20 30 40 50 60 70 80 90 100

• Result :

The above program was executed and the output was verified for a sample set of input values,

Register number : URK20DA1009


Name : Judah Felix

You might also like