8.6 – Operations on Array part-3

by subbu on October 27, 2013

Specification: Accept “n” elements into an array, insert an element at a required position and print the resultant array.

Show Logic

Actual position (index) is one less than the position given from outside

pos--;

Inserting an element in an array
To insert an element at pos, all the elements which are at the right side to the pos must shift one step forward. Elements can be shifted one by one using a loop that starts from n-1 and stops at pos. Elements a[i] shift to their next positions a[i+1] as loop progresses.

for(i=n-1;i>=pos;i--)
   a[i+1]=a[i];         /* shifts to their next positions */

At the end of loop execution, we find that all the elements which are at the right side to the pos are shifted one step forward.
Now we can get a new number from the keyboard and sore at a[pos].

scanf("%d",&a[pos]);

inserting an element into an array
Now the size of array is incremented by 1

n++;

Program:

#include<stdio.h>
int main()
{
int n,i,pos,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
printf("Enter the position:");
scanf("%d",&pos);
if(pos<1 || pos>n)          /*checking invalid position*/
   printf("Invalid position");
else
{
   pos--;                   /* converting position into index*/
   for(i=n-1;i>=pos;i--)    /* Shifting the elements */
      a[i+1]=a[i];
   printf("Enter the new element:");
   scanf("%d",&a[pos]);         /*inserting new element */
   n++;                    /* number of elements is increased by 1 */
   printf("The resultant array:\n");
   for(i=0;i<n;i++)         /*Printing resultant array */
      printf("%5d",a[i]);
}
return 0;
}

Execution:
How many elements?7
Enter 7 elements:
12  67  54  34  98  27  15
Enter the position:3
Enter the new element:88
The resultant array:
12   67   88   54   34   98   27   15

Specification: Accept “n” elements into an array, delete an element at a required position and print the resultant array.

Show Logic

Actual position (index) is one less than the position given from outside

pos--;

To delete an element, we overwrite the element at pos by its next element, next element with its next element. We continue the process until the last element.

Elements can be overwritten one by one using a loop that starts from pos and stops at n-2. The next element a[i+1] is overwritten on current element a[i] as loop progresses.
deleting an element from an array

for(i=pos;i<n-1;i++)
   a[i]=a[i+1];

deleted an element from an array
At the end of loop, we find that element at the pos is deleted (overwritten) and the number of elements is reduced by 1

n--;
Program:

#include<stdio.h>
int main()
{
int n,i,a[50],pos;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
printf("Enter the position:");
   scanf("%d",&pos);
if(pos<1 || pos>n)
   printf("Invalid position");
else
{
   pos--;
   for(i=pos;i<n-1;i++)
       a[i]=a[i+1];
   n--;
   printf("The resultant array:\n");
   for(i=0;i<n;i++)
      printf("%5d",a[i]);
}
return 0;
}

Execution:
How many elements?7
Enter 7 elements:
12 67 54 34 98 27 15
Enter the position:3
The resultant array:
12   67   34   98   27   15

Specification: Accept “n” elements into an array and find whether an element is existed or not. (Searching)

Show Logic

We will assign 0 (false) to the flag variable x

x=0;

We will traverse the array, element by element from first to last in searching of the required element (f) using the loop.

for(i=0;i<n;i++)
        a[i]

If the required element is existed then 1 is assigned to the flag variable x.

for(i=0;i<n;i++)
   if(a[i]==f)         /* checking for required element */
   {
     x=1;            /* element is existed */
     break;
   }

At the end of loop, if the value of flag variable x is 1 then the required element is existed otherwise not existed.

if(x)
   printf("Existed");
else
   printf("Not existed");

Program:

#include<stdio.h>
int main()
{
int n,i,x,f,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
printf("Find what?");
scanf("%d",&f);
x=0;
for(i=0;i<n;i++)
  if(a[i]==f)
  {
    x=1;
    break;
  }
if(x)
  printf("Existed");
else
  printf("Not existed");
return 0;
}

Execution 1:
How many elements? 6
Enter 6 elements:
45    56   78   45   34   23
Find what? 78
Existed

Execution 2:
How many elements? 6
Enter 6 elements:
45    56   78   45   34   23
Find what? 17
Not existed

Specification: Accept “n” elements into an array, find an element and replace it with another element and print the resultant array.

Show Logic

We will traverse element by element in searching for the required element (x). If the required element is existed then we will overwrite it with new element.

for(i=0;i<n;i++)
  if(a[i]==x)
     a[i]=y;     /* overwriting with new value */

Program:

#include<stdio.h>
int main()
{
int n,i,x,y,a[50];
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
printf("Find what?");
scanf("%d",&x);
printf("Replace with what?");
  scanf("%d",&y);
for(i=0;i<n;i++)
{
   if(a[i]==x)
       a[i]=y;
}
printf("The resultant array:\n");
for(i=0;i<ni++)
   printf("%5d",a[i]);
return 0;
}

Execution:
How many elements? 6
Enter 6 elements:
21   56   34   23   56   32
Find what? 56
Replace with what? 66
The resultant array:
21   66   34   23   66   32

Specification: Accept n elements into one array, m elements into another array and append the elements of second array to first array and print the resultant array.

Show Logic

Appending an array to another array
Here array a has n elements and b has m elements. To append b to a, m elements of b are copied onto a from a[n]. The total number of elements in a would be n+m.

for(i=n,j=0;j<m;i++,j++)
    a[i]=b[j];

Program:

#include<stdio.h>
int main()
{
int i,j,n,m,a[50],b[50];
printf("How many elements into 1<sup>st</sup> vector:");
scanf("%d",&n);
printf("How many elements into 2<sup>nd</sup> vetor:");
scanf("%d",&m);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
printf("Enter %d elements:\n",m);
for(i=0;i<m;i++)
   scanf("%d",&b[i]);
for(i=n,j=0;j<m;i++,j++)
       a[i]=b[j];
printf("The resultant array:\n");
for(i=0;i<n+m-1;i++)
     printf("%5d",a[i]);
return 0;
}

Execution:
How many elements into 1st vector: 5
How many elements into 2nd vector: 5
Enter 5 elements:
5   9   6   4   7
Enter 5 elements:
8   3   2   1   5
The resultant array:
5   9   6   4   7   8   3   2   1   5

Specification: Accept two vectors of same size and print the sum, subtraction and product of them.

Show Logic

We can’t directly add, subtract or multiply any two vectors.

c=a+b;     /* can't be done  */
d=a-b;     /* can't be done  */
e=a*b;     /* can't be done  */

We need to perform arithmetic operations on individual elements of vectors.

for(i=0;i<n;i++)
{
   c[i]=a[i]+b[i];   /* addition of two vectors */
   d[i]=a[i]-b[i];    /* subtraction of two vectors  */
   e[i]=a[i]*b[i];    /* product of two vectors */
}

Program:

#include<stdio.h>
int main()
{
int i,n,a[50],b[50],c[50],d[50],e[50];
printf("Enter the size of vector:");
scanf("%d",&n);
printf("Enter %d elements into 1st vector:\n",n);
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
printf("Enter %d elements into 2nd vector:\n",n);
for(i=0;i<n;i++)
   scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
   c[i]=a[i]+b[i];
   d[i]=a[i]-b[i];
   e[i]=a[i]*b[i];
}
printf("Sum of two vectors:\n");
for(i=0;i<n;i++)
    printf("%5d",c[i]);
printf("\nSubtraction of two vectors:\n");
for(i=0;i<n;i++)
    printf("%5d",d[i]);
printf("\nProduct of two vectors:\n");
for(i=0;i<n;i++)
    printf("%5d",e[i]);
return 0;
}

Execution:
Enter the size of vector:5
Enter 5 elements into 1st vector:
12      34      11      6       5
Enter 5 elements into 2nd vector:
6       9       8       4       5
Sum of two vectors:
18   43   19   10   10
Subtraction of two vectors:
6   25    3    2    0
Product of two vectors:
72  306   88   24   25

Previous post:

Next post: