8.5 – Operations on array Part-2

by subbu on October 25, 2013

Specification: Accept n elements into an array, reverse the elements of array and print the resultant array

Show Logic

How to reverse the array elementsIn order to reverse the elements of array, a[0] and a[n-1] must be inter changed, a[1] and a[n-2] must be interchanged and must continue up to the middle of array.
Here, elements from first are selected using the index variable i, elements from last are selected using the index variable j.

i=0;
j=n-1;

i is incremented by 1 and j is decremented by 1 for every iteration, a[i] and a[j] are interchanged. It must continue as long as i<j.

while(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
  i++;
  j--;
}

The same loop can be written using for loop as

for(i=0,j=n-1;i<j;i++,j--)
{
  temp=a[i];
  a[i]=a[j];
  a[j]=temp;
}

Program:

#include<stdio.h>
int main()
{
int i,j,temp,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]);
for(i=0,j=n-1;i<j;i++,j--)
{
   temp=a[i];
   a[i]=a[j];
   a[j]=temp;
}
printf("Elements in reverse order:\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
Elements in reverse order:
15   27   98   34   54   67   12

Specification: Accept n elements into an array and print whether the array is a palindrome array or not.

Show Logic

If the reversed array is equl to the original array then the array is called palindrome array.
To check whether the array is a palindrome or not, we have to check a[0] and a[n-1], a[1] and a[n-2] and so on up to the middle of array. If all the pairs are equal then the array is called palindrome array. If we find any inequality then the array is not called palindrome array.
How to find a palindrome array or notFirst we store 1 (true) in a flag variable x

x=1;

Elements from first using “i” and last using “j” are compared for inequality until middle of array (i<j). If there is any inequality then flag variable “x” is stored with 0.

for(i=0,j=n-1;i<j;i++,j--)
 if(a[i]!=a[j])
 {
    x=0;
    break;
 }

At the end of loop, if “x” is 1 that is unchanged (no inequality) then it is a palindrome array otherwise it is not a palindrome array.

if(x)
   printf("Palindrome array");          /* executed if x is 1 */
else
   printf("Not a palindrome array");

Program:

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

Execution 1:
How many elements?  7
Enter 7 elements:
12   27   54   34   54   27   12
Palindrome array

Execution 2:
How many elements?  7
Enter 7 elements:
12   27   54   34   54   18   12
Not a palindrome array

Specification: Accept “n” elements into an array, append a new element to the array and print the resultant array.

Show Logic
  • Appending is adding a new element at the end.
  • Because a[n-1] is the last element, new element must be added at a[n]
  • Now the number of elements would be n+1

How to append an element to an array

Program:

#include<stdio.h>
int main()
{
int a[50],i,n;
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 new element:");
  scanf("%d",&a[n]);
n++;                /* number of elements are incremented by 1 */
printf("The resultant array is :\n");
for(i=0;i<n;i++)
  printf("%5d",a[i]);
return 0;
}

Execution:
How many elements? 7
Enter 5 elements:
12   67   54   34   98   27   15
Enter the new element: 68
The resultant array is
12   67   54   34   98   27   15   68

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

Show Logic
  • From the user point of view valid position is from 1 to n
  • From the programmer point of view index of element is from 0 to n-1.
  • Hence index is 1 less than the position. To print the element at a required position
pos--;                      /* making position into index */
printf("%d",a[pos]);  /* printing the element at index */

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);
/* according to the user, position must be between 1 and n */
if(pos<1 || pos>n)                   /*checking for valid position*/
   printf("Invalid position");
else
{
   pos--;           /* index is 1 less than position */
   printf("The element is %d",a[pos]);
}
return 0;
}

Execution 1:
How many elements? 6
34   45   67   22   65   23
Enter the position: 2
The element is 45

Execution explained:
Here 2 is a valid position
Because index is 1 less than position printing a[1] (45) as output.

Execution 2:
How many elements? 6
34   45   67   22   65   23
Enter the position: 0
Invalid position

Execution 3:
How many elements? 6
34   45   67   22   65   23
Enter the position: 7
Invalid position

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

Show Logic

Modifying an element in an array
Assigned a new value to an element results overwriting old value with the new value.

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--;
   printf("Enter the new element:");
   scanf("%d",&a[pos]);
   printf("The resultant array :\n");
   for(i=0;i<n;i++)
      printf("%5d",a[i]);
}
return 0;
}

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

Specification: Accept two arrays of n elements and print whether they are equal or not

Show Logic

We can’t compare two arrays directly by their names

if(a==b)  /* cant be done */

Two arrays must be compared element by element from first to last. Arrays are not equal if any two elements are unequal.

Comparing two arrays

flag=1;
for(i=0;i<n;i++)
 if(a[i]!=b[i])
 {
   flag=0;
   break;
 }

Here we check elements using a loop for i=0, 1, 2 … ,n-1. We assign false (0) if any inequality found. If the flag is unchanged (1) then arrays are equal otherwise not equal

Program:

#include<stdio.h>
int main()
{
int a[50],b[50],n,i,flag;
/* accepting elements into arrays*/
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements into 1st array:\n",n);
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
printf("Enter %d elements into 2nd array:\n",n);
for(i=0;i<n;i++)
  scanf("%d",&b[i]);
flag=1;
for(i=0;i<n;i++)
  if(a[i]!=b[i])
  {
     flag=0;
      break;
  }
if(flag)
   printf("Equals");
else
   printf("Not equal");
return 0;
}

Execution:
How many elements?4
Enter 4 elements into 1st array:
12 34 45 55
Enter 4 elements into 2nd array:
12 34 45 55
Equals

Specification: Accept “n” elements into an array and print how many positive numbers, negative numbers, zeros, even numbers and odd numbers are existed.

Show Logic

Common loop used to access elements one by one is

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

We check every element and count appropriate counter by one from first element to last element

pc=0,nc=0,zc=0,ec=0,oc=0;
for(i=0;i<n;i++)
{
  if(a[i]>0)  /* checking for positive */
     pc++;
  if(a[i]<0) /* checking for negative */
     nc++;
  if(a[i]==0) /* checking for zeros */
     zc++;
  if(a[i]%2==0) /* checking for even */
     ec++;
  if(a[i]%2==1) /* checking for odd */
     oc++;
}

Program:

#include<stdio.h>
int main()
{
int a[50],n,i,pc,nc,zc,ec,oc;
/* accepting elements into arrays*/
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
pc=0,nc=0,zc=0,ec=0,oc=0;
for(i=0;i<n;i++)
{
   if(a[i]>0)  /* checking for positive */
     pc++;
   if(a[i]<0) /* checking for negative */
     nc++;
   if(a[i]==0) /* checking for zeros */
     zc++;
   if(a[i]%2==0) /* checking for even */
     ec++;
   if(a[i]%2==1) /* checking for odd */
     oc++;
}
if(pc) /* It is true only when pc is a non-zero */
   printf("\nPositive numbers %d",pc);
if(nc)
   printf("\nNegative numbers %d",nc);
if(zc)
   printf("\nZeros %d",zc);
if(ec)
   printf("\nEven numbers %d",ec);
if(oc)
   printf("\nOdd numbers %d",oc);
return 0;
}

Execution:
How many elements?5
Enter 5 elements:
12  -23  0  25  62
Positive numbers 3
Negative numbers 1
Zeros 1
Even numbers 3
Odd numbers 1

Previous post:

Next post: