# 8.4 – Operations on array part-1

by on October 25, 2017

Note:
Here on wards, in every program we are going to declare an array of 50 elements and will be using only n elements.
Now you may have a question that, why can’t we declare an array of n elements rather using n out of 50.
Because compiler has to know the size and allocate the memory while compiling the program that is before executing the program. Where as n is only accepted after the compilation that is while executing. Specification: Accept n elements into an array and print the elements in the accepted order.

Program:

```#include<stdio.h>
int main()
{
int n,i,a;
printf("How many elements:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)                        /* storing the data */
scanf("%d",&a[i]);
printf("The given elements are:\n");
for(i=0;i<n;i++)                       /* printing data     */
printf("%5d",a[i]);
return 0;
}
```

Execution 1:
How many elements: 4
Enter 4 elements:
12
34
56
78
The given elements are:
12      34      56      78 Execution 2:
How many elements: 6
Enter 6 elements:
12   56   87   67   65   45
The given elements are:
12   56   87   67   65   45 Example explained:
a allocates the memory allocation of 50 elements but we use only “n” elements that is 0 to n-1.
“n” is accessed from the user.
We can give elements to array one by one or multiple elements at a time using a tab separation.

Specification: Accept n elements into an array and print them in reverse order.

Show Logic

To print the elements in reverse, the index variable i must be started from n-1, reduced by 1 for every iteration and must continue until i becomes zero. ```for(i=n-1;i>=0;i--)
printf("%5d",a[i]);

```

Program:

```#include<stdio.h>
int main()
{
int n,i,a;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Elements in reverse order:\n");
for(i=n-1;i>=0;i--)
printf("%5d",a[i]);
return 0;
}
```

Execution:
How many elements? 5
Enter 5 elements:
12      67      54      34      98
Elements in reverse order:
98      34      54      67      12

Specification: Accept n elements into an array and print the sum of all the elements.

Show Logic We get individual elements from 0 to n-1 using a loop

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

We add the value of each element to a result variable sum as the loop progress. At the end of loop the variable sum will have sum of all the elements.

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

Program:

```#include<stdio.h>
int main()
{
int n,i,a,sum;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(sum=0,i=0;i<n;i++)
sum=sum+a[i];
printf("Sum of all the elements %d",sum);
return 0;
}
```

Execution:
How many elements? 6
Enter 5 elements:
12      67      54      34      98      10
Sum of all the elements 275

Specification: Accept “n” elements into an array and print the sum of even and odd elements.

Show Logic

We get individual elements using the loop

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

if the selected element a[i] is an even number then it is added to esum (Even sum) other wise added to osum (Odd sum). At the end of loop esum will have even elements sum and osum will have odd elements sum.

```for(esum=,osum=0,i=0;i<n;i++)
{
if(a[i]%2==0)
esum=esum+a[i];
else
osum=osum+a[i];
}
```

Program:

```#include<stdio.h>
int main()
{
int n,i,a,esum,osum;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(esum=0,osum=0,i=0;i<n;i++)
{
if(a[i]%2==0)
esum=esum+a[i];
else
osum=osum+a[i];
}
printf("Sum of even elements %d",esum);
printf("\nSum of odd elements %d",osum);
return 0;
}
```

Execution:
How many elements? 5
Enter 5 elements:
12      67      54      35      98
Sum of even elements 164
Sum of odd elements 102

Specification: Accept n elements into an array and print the maximum and minimum elements.

Show Logic The first element a is considered as both “min” and “max”.

```min=max=a;
```

We will select element by element from the second element to n-1 (a[i]) using loop

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

If a[i] is less than “min” then it is stored in “min”, If a[i] is greater than “max” then it is store in “max”

```for(min=max=a,i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
```

Once the loop is completed then “min” has the minimum element and “max” has the maximum element.

Program:

```#include<stdio.h>
int main()
{
int n,i,min,max,a;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(min=max=a,i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
printf("Minimum element %d",min);
printf("\nMaximum element %d",max);
return 0;
}
```

Execution:
How many elements? 5
Enter 5 elements:
12   67   6   35   98
Minimum element 6
Maximum element 98

Specification: Accept n elements into an array, copy the elements onto another array and print the resultant array.

Show Logic

An array can’t be copied like a normal variable just by using an assigning operator =

```b=a;    /* can't be done, gives error */
```

Need to copy element by element from first element to last element like

```b=a;
b=a;
------
b=a;
```

The same thing can be done using a loop

```for(i=0;i<n;i++)
b[i]=a[i];
``` Program:

```#include<stdio.h>
int main()
{
int n,i,a,b;
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;i<n;i++)
b[i]=a[i];
printf("Resultant array elements are:\n");
for(i=0;i<n;i++)
printf("%5d",b[i]);
return 0;
}
```

Execution:
How many elements? 5
Enter 5 elements:
12   67   6   35   98
Resultant array elements are:
12   67   6   35   98

Previous post:

Next post: