Itp 4
Itp 4
Need of Arrays
Example of Arrays
Suppose we wish to arrange the percentage marks
obtained by 100 students in ascending order. In such a
case we have two options to store these marks in
memory:
Construct 100 variables to store percentage marks obtained
by 100 different students, i.e. each variable containing one
students marks.
Construct one variable (called array or subscripted variable)
capable of storing or holding all the hundred values.
Definition
1-Dimensional Arrays
typename variablename[size];
typename is any Data type
variablename is any legal variable name
size is a number indicating the size of the array
For example
int a[10];
2002
2012
2020
A[0]
A[5]
A[9]
Initializing Arrays
You can also let the compiler figure out the array size for you:
int array[] = { 100, 200, 300, 400};
Arrays
An array is a collection of similar elements.
The first element in the array is numbered 0, so
the last element is 1 less than the size of the
array.
An array is also called as a subscripted variable.
Before using an array, its type and dimension
must be declared.
Elements of an array are stored in contiguous
memory locations.
Multidimensional Arrays
Arrays in C can have virtually as many
dimensions as you want.
Definition is accomplished by adding additional
subscripts when it is defined.
For example:
int a [4] [3] ; //2- dimensional Array
defines a two dimensional array
a is an array with 4 elements, each of int [3];
In memory:
a[0][0] a[0][1] A[0][2] a[1][0] a[1][1] a[1][2] A[2][0] A[2][1]a[2][2] A[3][0] A[3][1]
a[3][2]
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
...
a[3][2] = 12;
Multidimensional arrays
How to interpret a declaration like:
int d[2][4];
(int)
(int)
(int)
(int)
(int)
d[0][0]
d[0][1]
d[0][2]
d[0][3]
d[1][0]
d[1][1]
d[0]
(int)
d[1][2]
(int)
d[1][3]
d[1]
{
int a[10][10], i,j;
printf(enter the element of
the matrix a of order 3*4\n);
for(i=0;i < 3;i++)
{ for(j=0;j < 4;j++)
scanf(%d,&a[i][j]);
printf( matrix a \n);
}
Output:
matrix a
/* Example program to add two matrices & store the results in the 3rd matrix */
main()
{
int a[10][10],b[10][10],c[10]
[10],i,j,m,n,p,q;
printf(enter the order of the
matrixn);
scanf(%d%d,&p,&q); //Input
row n column
if(m==p && n==q)
{
printf(matrix can be
added\n);
printf(enter the elements
of the
matrix a);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf(%d,&a[i][j]);
/*enter
elements of matrix*/
printf(enter the elements of
the
matrix b);
An Example -- Sorting
for(x=0; x<n; x++)
{
for(y=0; y<n-x; y++)
{
if(array[y]>array[y+1])
{
temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
}
}
}
printf("\n\n Finally sorted array is
:"); for( i=0;i<=n-1;i++)
printf( %d ",a[i]);
main()
{
int a[100],n,I,x,y,temp;
printf("\n\n Enter integer valu
e for total no.s of elements to
be sorted:
scanf("%d",&n);
for( i=0;i<=n-1;i++)
{ printf("\n\n Enter integer value fo
r element no.%d : ", i+1);
scanf("%d",&a[i]);
}
}
int found,i,pos;
printf("enter size" );
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter array element ");
scanf("%d",&a[i]);
}
printf("enter the target element ");
scanf("%d",&x);
found=0;
found*/
/*1-found
0-not
for (i=0;i<n;i++)
if (a[i] == x)
{
found=1;
pos=i;
break;
}
if (found)
printf("%d found at position
%d\n",x,pos);
else
printf("%d not found\n",x);
}
TIME TO THINK????
Array elements are stored in
(a) Random memory location
(b) Sequential memory locations
(c) Scattered memory locations
(d) Direct memory locations
If the size of an integer array is n, the number of elements that can be stored in it is
(a) n-1
(b) n-2
(c) n+1
(d) N
Under which of the following conditions, the size of the array need not be
specified?
(a) when initialization is a part of definition
(b) when the compiler is smart
(c) when it is a declaration
(d) when it is a formal parameter location of memory at file saving time