Arrays-unit3 (1)
Arrays-unit3 (1)
Q) What is Array?
Array is a kind of data structure that can store a fixed-size sequential collection of
elements of the same type
(OR)
An array is a collection of data that holds fixed number of values of same type and share same
name.
(OR)
Array is a collection of homogenous data stored under unique name. The values in an
array are called as 'elements of an array.' These elements are accessed by numbers called as
'subscripts or index numbers.' Arrays may be of any variable type.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
1. Single or One Dimensional array is used to represent and store data in a linear form.
2. Array having only one subscript variable is called One-Dimensional array
3. It is also called as Single Dimensional Array or Linear Array
1. Declaration:
Syntax:
<data-type><array_name> [size];
Syntax
Significance
Parameter
Initializing array means assigning values to the array. Arrays can be initialized in two ways:
Syntax:
Data type arr_name[size]={value1,value2,value3….};
(OR)
Data type arr_name[ ]={value1,value2,value3….};
Examples:
Int a[4]={1,2,3,4};
Here
a[0]=1; a[1]=2;a[2]=3;a[3]=4;
2
printf("\n\t Enter three numbers : ");
for(i=0; i<3; i++)
{
scanf("%d", &a[i]); // read array
}
printf("\n\n\t Numbers are : ");
for(i=0; i<3; i++)
{
printf("\t %d", a[i]); // print array
}
getch();
}
Write a program to accept values into array and display them at run time.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n;
clrscr();
printf(“enter the size of array\n”);
scanf(“%d”,&n);
printf(“enter the elements into array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“the array elements are\n”);
for(i=0;i<n;i++)
printf(“%d is %d\n”,i,a[i]);
getch();
}
Output:
Enter the size of array 3
Enter the elements into array 1
2
3
The array elements are 1 2 3
3
1. Traversing:
Traversing means accessing each and every element of the array for a specific purpose. An array is
homogeneous data elements, then traversing of the data elements can include printing every
element, counting the total number of elements, or performing any process on these elements.
2) Searching:
Searching means weather a particular element value is present in the array or not.
If the value is present searching returns the position of the value otherwise it returns
appropriate message.
There are two popular methods for searching
Linear search:-It is sequential search by comparing every element of the array one by one in
Sequence.
Binary search:-It is a searching algorithm that works on sorted list.
We have linear array A as below:
A={15,50,35,20,25};
Suppose item to be searched is 20.
We will start from beginning and will compare 20 with each element. This process will continue until
element is found or array is finished. Here:
1)Compare20with15
20 # 15, go to next element.
2)Compare20with50
20#50,gotonextelement.
3)Compare20with35
20#35,gotonextelement.
4)Compare20with20
20 = 20, so 20 is found and its location is 4.
Example:
A={10,20,50,30,15};
New element to be inserted is 100 and location for insertion is 3. So shift the elements from 5th
location to 3rd location downwards by 1 place. And then insert 100 at 3rd location. It is shown below:
4. Deletion: Deleting an element from the array means removing a data element from an already
existing array.
Examlpe:
A={10,20,50,40,25};
The element to be deleted is 50 which is at 3rd location. So shift the elements from 4th to 6th location
upwards by 1 place. It is shown below:
4
After deletion the array will be:
1 2 3 4 5 6
1 6
0 20 40 25 0
5. Sorting: It is used to arrange the data items in some order i.e. in ascending or descending order
in case of numerical data and in dictionary order in case of alphanumeric data.
E.g.
We have linear array A as below:
1 2 3 4 5
10 50 40 20 30
After arranging the elements in increasing order by using a sorting technique, the array will be:
1 2 3 4 5
10 20 30 40 50
6.Merging: It is used to combine the data items of two arrays into single array.
Example:
a={1,4,2,5};
b={5,8,3}
After merging merged array C is as below:
Merged array c={1,4,2,5,5,8,3};
1. Array having more than one subscript variable is called Multi-Dimensional array.
2. Multi Dimensional Array is also called as Matrix.
i) Two dimensional array
1. Two Dimensional Array requires Two Subscript Variables
2. Two Dimensional Array stores the values in the form of matrix.
3. One Subscript Variable denotes the “Row” of a matrix.
4. Another Subscript Variable denotes the “Column” of a matrix.
5
i) Declaring Two-Dimensional Arrays
Syntax:
<data-type><array_nm> [row_subscript][column-subscript];
Example:
int a[3][3];
In above example, a is an array of type integer which has storage size of 3 * 3 matrix.
The total elements are 3*3 =9.
Initializing array means assigning values to the array. Arrays can be initialized in two ways:
Example:
Int a[2][2];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf(“%d”,a[i][j]);
Program :
/* Program to demonstrate two dimensional array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], i, j;
clrscr();
printf("\n\t Enter matrix of 3*3 : ");
for(i=0; i<3; i++)
{
6
for(j=0; j<3; j++)
{
scanf("%d",&a[i][j]); //read 3*3 array
}
}
printf("\n\t Matrix is : \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\t %d",a[i][j]); //print 3*3 array
}
printf("\n");
}
getch();
}
7
Q) Arrays and Functions (OR) How to pass array to functions:
In C programming, a single array element or an entire array can be passed to a function. Also, both
one-dimensional and multi-dimensional array can be passed to function as argument.
Q) Sparse matrices:
a sparse matrix is a matrix in which most of the elements are zero.
Basically ,there are two types of sparse matrices.
In the first type of sparse matrix all elements above or below the diagonal have zeros.
Lab Programs:
1. Write a program to search a element in the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int n,i,x,flag=0;
clrscr();
printf("enter the array size\n");
scanf("%d",&n);
printf("enter array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements are\n");
9
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n enter the element to search element\n");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
printf("\nelement %d found at %d position",x,(i+1));
flag=1;
break;
}
}
if(flag==0)
printf("\nelement %d not found",x);
getch();
}
#include<conio.h>
void main()
{
int a[10],i,j,n,temp;
clrscr();
printf("enter the array size\n");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n array elements before deletion are \n");
for(i=0;i<n;i++)
printf("%d",a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
If(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(“\n array elements after sortinf are\n");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
10
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
clrscr();
printf("Enter row and col size os matrix A\n");
scanf("%d%d",&m1,&n1);
printf("Enter row and col size of matrix B\n");
scanf("%d%d",&m2,&n2);
if(m1==m2 && n1==n2)
{
printf("enter A matrix elements\n");
read_arr(a,m1,n1);
11
printf("enter B matrix elements\n");
read_arr(b,m2,n2);
add_arr(a,b,c,m1,n1);
printf("the addition of A and B matrix is\n");
print_arr(c,m1,n1);
}
else
printf("matrix addition not possible");
getch();
}
#include<stdio.h>
#include<conio.h>
for (i=0;i<m1;i++)
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(k=0;k<n1;k++)
c[i][j]+= a[i][k]*b[k][j];
}
void main()
{
int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
clrscr();
printf("Enter row and col size os matrix A\n");
scanf("%d%d",&m1,&n1);
printf("Enter row and col size of matrix B\n");
scanf("%d%d",&m2,&n2);
if(n1==m2)
{
printf("enter A matrix elements\n");
read_arr(a,m1,n1);
printf("enter B matrix elements\n");
read_arr(b,m2,n2);
add_arr(a,b,c,m1,n1,n2);
printf("the multiplication of A and B matrix is\n");
print_arr(c,m1,n2);
}
else
printf("matrix multiplication not possible");
getch();
}
5. Write a c program to find both the largest and smallest number in a list of integers.
#include<stdio.h>
void main()
{
int a[50],size,i,big,small;
clrscr();
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter elements in to the array\n");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
big=a[i];
}
printf("\nLargest element: %d",big);
small=a[0];
for(i=1;i<size;i++)
13
{
if(small>a[i])
small=a[i];
}
printf("\nSmallest element: %d",small);
getch();
}
14