Uploaded by Ananda Murugan

04.04.20 - Class materials

advertisement
UNIT-IV
ARRAYS
Arrays
Definition:
An array is a collection of data items, all of the same type, accessed using a common name.
Types:
1. One-dimensional array
2. Two dimensional array
One Dimensional Array:
Declaring Arrays
type arrayname [ arraysize ];
This is called a single-dimensional array.
Eg: int mark[10];
Now mark is an array variable which is sufficient to hold upto 10 integer numbers. The memory
allocation for the mark array is
mark[0]
1000
mark[1]
1002
mark[2]
1004
mark[3]
mark[4]
1006
1008
mark[5]
1010
mark[6]
mark[7]
mark[8]
mark[9]
1012
1014
1016
1018
Where 1000,1002, 1004 etc are the memory addresses at which the array elements
mark[0],mark[1] etc are stored.
Initializing an array
We can initialize array in C either one by one or using a single statement as follows:
int mark[10]={3,2,4,67,12,34,1,9,32,98};
For the above array, the memory allocation would be as follows:
mark[0]
mark[1]
mark[2]
mark[3]
mark[4]
mark[5]
mark[6]
mark[7]
mark[8]
mark[9]
3
2
4
67
12
34
1
9
32
98
1000
1002
1004
1006
1008
1010
1012
1014
1016
1018
Example 1
Program to read an array of integers and print the array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n;
printf(“\n enter the number of elements in the array”);
scanf(“%d”, &n);
printf(“\n enter the values for the elements of the array”);
for(i=0; i<n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“\n the values for the elements of the array”);
for(i=0; i<n; i++)
{
printf(“\n %d”,a[i]);
}
getch();
}
When the above code is compiled and executed, it produces the following result:
enter the number of elements in the array
5
enter the values for the elements of the array
10
20
30
40
50
Example 2
Program to read an array of integers and print the sum of the elements of the array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,sum=0;
printf(“\n enter the number of elements in the array”);
scanf(“%d”,&n);
for(i=0;i<n; i++)
{
scanf(“%d”, &a[i]);
}
for(i=0; i<n; i++)
{
sum=sum+a[i]);
}
printf(“sum=%d “,sum);
getch();
}
When the above code is compiled and executed, it produces the following result:
enter the number of elements in the array
5
enter the values for the elements of the array
10
20
30
40
50
Sum=150
Example 3
Program to find sum of odd and even elements in an array of integers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i=0,oddsum=0,evensum=0;
printf("\n enter the size of array");
scanf("%d",&n);
printf("\n enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if (a[i]%2==0)
{
evensum= evensum+a[i];
}
Else
{
Oddsum=oddsum+a[i];
}
printf("\n\n the sum of even elements :%d ",evensum);
printf("\n\n the sum of odd elements :%d ",oddsum);
getch();
}
When the above code is compiled and executed, it produces the following result
enter the size of array
6
enter the array elements
4
3
78
98
23
56
the sum of even elements :236
the sum of odd elements :26
Example 4
Program to copy an array of elements into another array
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],b[10],i=0;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("\n enter the array elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
b[i]=a[i];
}
printf("\n the copied array\n");
for(i=0;i<n;i++)
{
printf("\t%d",b[i]);
}
getch();
}
Example 5
When the above code is compiled and executed, it produces the following result:
enter the size of the array
5
enter the array elements
4
3
6
7
1
the copied array
4
3
6
7
1
Example 5
Program to copy an array of elements into another array in reverse order
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],b[10],i;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("\n enter the array elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
b[i]=a[n-i-1];
}
printf("\n the array in reverse order\n");
for(i=0;i<n;i++)
{
printf("\t%d",b[i]);
}
getch();
}
When the above code is compiled and executed, it produces the following result:
enter the size of the array
5
enter the array elements
4
3
6
7
1
the array in reverse order
1
7
6
3
4
Example 6
Program to arrange the elements of an array in sorted order
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],i=0,j=0;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("\n enter the array elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n the array in sorted order\n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
getch();
}
When the above code is compiled and executed, it produces the following result:
enter the size of the array
5
enter the array elements
43671
the array in sorted order
1
3
4
6
7
Example 7
Program to find the largest and smallest elements in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int n,max,min,a[10],i,j;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("\n enter the array elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
else if (a[i]<min)
{
min=a[i];
}
}
printf(“ \n the largest element is : %d”,max);
printf(“ \n the smallest element is : %d”,min);
}
getch();
}
When the above code is compiled and executed, it produces the following result
enter the size of the array
5
enter the array elements
4
3
6
7
1
the largest element is : 7
the smallest element is : 1
Example 8
Program to search whether a given element is present in an array of integer elements
#include<stdio.h>
#include<conio.h>
void main()
{
int n,number,a[10],i=0;
clrscr();
printf("\n enter the size of the array");
scanf("%d",&n);
printf("\n enter the array elements ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf(“\n enter the element to be searched”);
scanf(“%d”,&number);
for(i=0;i<n;i++)
{
if(a[i]= =number)
{
printf(“the number is found ”);
exit (0);
}
printf(“ the number is not found”);
}
getch();
}
When the above code is compiled and executed, it produces the following result:
enter the size of the array
5
enter the array elements
4
3
6
3
1
enter the element to be searched
3
the number is found
Download