Array Manipulation
Shown next are C programs for carrying out manipulations such as finding the sum of elements of an array, adding two arrays, and reversing an array.
Program
ADDITION OF THE ELEMENTS OF THE LIST
#include<stdio.h>
#include<conio.h>
void main()
{
void read(int *,int);
void dis(int *,int);
int a[5],i,sum=0;
clrscr();
printf(“Enter the elements of list \n”);
read(a,5); /*read the list*/
printf(“The list elements are \n”);
dis(a,5);
for(i=0;i<5;i++)
{
sum+=a[i];
}
printf(“The sum of the elements of the list is %d\n”,sum);
getch();
}
void read(int c[],int i)
{
int j;
for(j=0;j<i;j++)
scanf(“%d”,&c[j]);
fflush(stdin);
}
void dis(int d[],int i)
{
int j;
for(j=0;j<i;j++)
printf(“%d “,d[j]);
printf(“\n”);
}
Example
Input
Enter the elements of the first array
15
30
45
60
75
Output
The elements of the first array are
15 30 45 60 75
The sum of the elements of an array is 225.
Addition of the two lists
Suppose the first list is
1
2
3
4
5
and the second list is
5
6
8
9
10
The first element of first list is added to the first element of the second list, and the result of the addition is the first element of the third list.
In this example, 5 is added to 1, and the first element of third list is 6.
This step is repeated for all the elements of the lists and the resultant list after the addition is
6
8
11
13
15
#include<stdio.h>
#include<conio.h>
void main()
{
void read(int *,int);
void dis(int *,int);
void add(int *,int *,int * ,int);
int a[5],b[5],c[5],i;
clrscr();
printf(“Enter the elements of first list \n”);
read(a,5); /*read the first list*/
printf(“The elements of first list are \n”);
dis(a,5); /*Display the first list*/
printf(“Enter the elements of second list \n”);
read(b,5); /*read the second list*/
printf(“The elements of second list are \n”);
dis(b,5); /*Display the second list*/
add(a,b,c,i);
printf(“The resultant list is \n”);
dis(c,5);
getch();
}
void add(int a[],int b[],int c[],int i)
{
for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
}
}
void read(int c[],int i)
{
int j;
for(j=0;j<i;j++)
scanf(“%d”,&c[j]);
fflush(stdin);
}
void dis(int d[],int i)
{
int j;
for(j=0;j<i;j++)
printf(“%d “,d[j]);
printf(“\n”);
}
Elucidation
Repeat step (2) for i=0,1,2,… (n−1), where n is the maximum number of elements in a list.
c[i] = a[i]+b[i], where a is the first list, b is the second list, and c is the resultant list; a[i] denotes the ith element of list a.
Example
Input
Enter the elements of the first list
1
2
3
4
5
Output
The elements of the first list are
2 3 4 5
Input
Enter the elements of the second list
6
7
8
9
10
Output
The elements of the second list are
6 7 8 9 10
The resultant list is
7 9 11 13 15
Inverse of the list
The following program makes a reverse version of the list.
#include<stdio.h>
#include<conio.h>
void main()
{
void read(int *,int);
void dis(int *,int);
void inverse(int *,int);
int a[5],i;
clrscr();
read(a,5);
dis(a,5);
inverse(a,5);
dis(a,5);
getch();
}
void read(int c[],int i)
{
int j;
printf(“Enter the list \n”);
for(j=0;j<i;j++)
scanf(“%d”,&c[j]);
fflush(stdin);
}
void dis(int d[],int i)
{
int j;
printf(“The list is \n”);
for(j=0;j<i;j++)
printf(“%d “,d[j]);
printf(“\n”);
}
void inverse(int inver_a[],int j)
{
int i,temp;
j-;
for(i=0;i<(j/2);i++)
{
temp=inver_a[i];
inver_a[i]=inver_a[j];
inver_a[j]=temp;
j-;
}
}
Example
Input
Enter the list
10
20
30
40
50
Output
The list is
10 20 30 40 50
The inverse of the list is
50 40 30 20 10
This is another version of an inverse program, in which another list is used to hold the reversed list.
#include<stdio.h>
#include<conio.h>
void main()
{
void read(int *,int);
void dis(int *,int);
void inverse(int *,int *,int);
int a[5],b[5];
clrscr();
read(a,5);
dis(a,5);
inverse(a,b,5);
dis(b,5);
getch();
}
void read(int c[],int i)
{
int j;
printf(“Enter the list \n”);
for(j=0;j<i;j++)
scanf(“%d”,&c[j]);
fflush(stdin);
}
void dis(int d[],int i)
{
int j;
printf(“The list is \n”);
for(j=0;j<i;j++)
printf(“%d “,d[j]);
printf(“\n”);
}
void inverse(int a[],int inverse_b[],int j)
{
int i,k;
k=j-1;
for(i=0;i<j;i++)
{
inverse_b[i]=a[k];
k-;
}
}
Example
Input
Enter the list
10
20
30
40
50
Output
The list is
10 20 30 40 50
The inverse of the list is
50 40 30 20 10