Professional Documents
Culture Documents
Exercise 1 - Array Manipulation
Exercise 1 - Array Manipulation
Exercise 1 - Array Manipulation
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
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);
• Output :
Enter the number of elements : 10
• Result :
The above program was executed and the output was verified for a sample set of input values,