0% found this document useful (0 votes)
0 views

Arrays Reference

The document provides an overview of arrays in C programming, detailing their types (single, double, and multi-dimensional) and how to declare and initialize them. It includes examples of reading and printing arrays, as well as programs for finding maximum and minimum elements, counting positive and negative numbers, searching for an element, and copying one array to another. Additionally, it explains memory representation and address calculation for array elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Arrays Reference

The document provides an overview of arrays in C programming, detailing their types (single, double, and multi-dimensional) and how to declare and initialize them. It includes examples of reading and printing arrays, as well as programs for finding maximum and minimum elements, counting positive and negative numbers, searching for an element, and copying one array to another. Additionally, it explains memory representation and address calculation for array elements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

‘c’ programming . R.V.R & J.

C COLLEGE OF
ENGINEERING.

Arrays
An array is a collection of elements of similar type, those sharing a common name and
occupies contiguous locations.

Arrays are 3 types.


1) Single dimensional arrays.
2) Double dimensional arrays.
3) Multi dimensional arrays.

DECLARATION OF ARRAYS
Array may be of one dimensional or multi dimensional. Declarations of the one
dimensional arrays, two dimensional arrays and three dimensional arrays are as follows.

Declaration of a one dimensional array

The syntax of the declaration of a one dimensional array is

data_type arrayname1 [size1], arrayname2 [size2]…arraynameN [sizeN];

Here data_type refers to the data type of the elements in the array and it can be a
primitive data type. arrayname1, arrayname2…..etc refers to the identifiers which
represent the array name. Size is an integer expression representing total number of
elements in the array.

Example
int Num[10];

It is the declaration of a one dimensional array which defines an integer array name Num
of size 10 and that represents a block of 10 consecutive storage locations. Here each
element in the array is represented by Num[0], Num[1],….,Num[9], where 0, 1, 2, …, 9
represents subscripts or indices of the array.

In general Num [ i ] refers to the i th element of the array. The representation of


one dimensional array in memory is as follows.

Num[0] Num[1] Num[2] Num[3] Num[4] Num[5] Num[6] Num[7] Num[8]


Num[9]

1024 1026 1028 1030 1032 1034 1036 1038 1040 1042
Addresses

In the memory representation of one dimensional array each element is


referenced by its name of the array with the subscript and the addresses. The name of
the array it self represents the base address of that entire array.

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

Let us consider the above example Num it self represent the base address of the
entire array. &Num [0] also represents the base address. &Num [5] represents the
address of the 6th element in the array Num, and it is calculated using the formulae

Address of the ith element = base address + i * scale factor

The scale factor is the number of byes allocated for each element of the array.
The address of array Num [5] is calculated as
&Num [5] = 1024 + 5 * 2 = 1034.
Here &Num [5] is also represented as (num+5).

Write a program to read an array and print that array.


#include<stdio.h>
main( )
{
int a[10], i, n ;
printf (“ Enter how many values you want to read : “);
scanf (“ %d “, &n );
for( i=0; i<n; i++ )
{
printf ( “ Enter the value of a[%d] : “ , i );
scanf (“ %d “, &a[ i ]);
}
printf (“ The array elements are ; “);
for( i=0; i<n; i++ )
printf (“ \t %d “, a[ i ]);
}

Declaration of a two dimensional array

The syntax of the declaration of a two dimensional array is


data_type array_name [size1][size2];

Here data_type refers to the data type of the elements in the array and it can be a
primitive data type. array_name refers to the identifiers which represent the array name.
Size1 is an integer expression representing the row size in the array and Size2 is also
an integer expression representing the column size in the array. In a two dimensional
array two subscripts are used in two pairs of square brackets.

The total number of elements in a two dimensional array is calculated by


multiplying number of rows and the number of columns. In a two dimensional array the
values are stored in a row major order.

Example
int Num[2][2];
It is the declaration of a two dimensional array which defines an integer array name Num
of row size 2 with column size 2 and that represents a block of 4 consecutive storage

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

locations. Here each element in the array is represented by Num[0][0], Num[0][1],Num[1]


[0] and num[1][1].

In general Num[ i ][ j ] refers to the element of the i th row and jth column in the
array. The representation of a two dimensional array in the memory is as follows.

Num[0][0] Num[0][1] Num[1][0] Num[1][1]

1024 1026 1028 1030


Addresses

In the memory representation of a two dimensional array each element is


referenced by its name of the array with row and column subscripts, and by the
addresses. The name of the array it self represents the base address of that entire array.

Let us consider the above example Num it self represent the base address of the
entire array. &Num [0][0] also represents the base address. &Num [1][0] represents the
address of the 2nd row 1st column element in that array Num, and it is calculated using
the formulae

Address of the [ i, j]th element = base address + i * total no of columns * scale factor + j * scale factor

The scale factor is the number of byes allocated for each element of the array.
The address of array Num [1][1] is calculated as
&Num [1][1] = 1024 + 1 * 2 *2 + 1*2= 1030.

Write a program to read a two dimensional array and print that array.
#include<stdio.h>
main( )
{
int a[10][10], i, j, m, n ;
printf (“ Enter the number of rows and columns: “);
scanf (“ %d %d“, &m, &n );
for( i=0; i<m; i++ )
{
for( j=0; j<n; j++ )
{
printf ( “ Enter the value of a[%d][%d] : “ , i, j );
scanf (“ %d “, &a[ i ][ j ]);
}
}
printf (“ The array elements are ; “);
for( i=0; i<m; i++ )
for( j=0; j<n; j++)
printf (“ \t %d “, a[ i ][ j ]);
}

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

Declaration of a three dimensional array

The syntax of the declaration of a three dimensional array is

data_type array_name [size1][size2][size3];

In the three dimensional array 3 subscripts are placed as three pairs of square brackets,
Where as size1 represents the number of pages, size2 represents the number of rows
and size3 represents the number of columns. The total number of elements in a three
dimensional array is calculated as No of pages * No of rows * No of columns.

Example
int a[2][2][2];

The total number of elements= 2 * 2 * 2 =8.

The value in the kth page, ith row and jth column is referred to by a[ k ][ i ][ j ]. The memory
representation of a three dimensional array is as follows.

a[0][0][0] a[0][0][1] a[0][1][0] a[0][1][1] a[1][0][0] a[1][0][1] a[1][1][0] a[1][1][1]


1024 1026 1028 1030 1032 1034 1036 1038
Page0 page1

Write a program to read p pages, m rows and n columns of elements and print them.
#include<stdio.h>
main()
{
int a[3][3][3], i, j, k, p, m, n;
printf ( “ Enter how many page numbers, rows and columns you want\n “);
scanf ( “ %d %d %d “, &p, &m, &n);
for( k=0; k<p; k++)
{
for( i=0; i<m; i++)
{
for( j=0; j<n; j++)
{
printf ( “ Enter element for a[%d][%d][%d] : “,k, i, j);
scanf ( “ %d “, &a[ k ][ i ][ j ]);

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

}
}
}

for( k=0; k<p; k++)


{
for( i=0; i<m; i++)
{
for( j=0; j<n; j++)
{
printf ( “The value in a[%d][%d][%d] = %d “,k, i, j, a[ k ][ i ][ j ] );
}
}
}

INITIALISATION OF ARRAYS

The elements of an array may be assigned with the values using initialization
instead of reading them by the I/O functions. An array can be initialized in its declaration
only. The lists of values are enclosed in braces. The values are separated by commas
and they must be constants or constant expressions.

One dimensional array can be initialised as given below.


int a[4]={10,20,30,40};
The values with in the braces are scanned from left to right and assigned them to a[0],
a[1], and soon.

A two dimensional array can be initialised as given below.


int a[3][2]={20,30,40,50,60,70};
The values are assigned to the array elements according to the order of the storage in
the memory. The above example can also be represented as
int a[3][2]={{20,30},{40,50},{60,70}};
Here, the initialised values with in the inner braces are assigned to each row.

If the number of values initialised for an array is less than the size mentioned, the
missing elements are assigned to zero.
Example
int a[3][4]={ {1,2},{3,4,5} };
In the above example the elements a[0][0], a[0][1] of the 0 th row and a[1][0], a[1]
[1] and a[1][2] of the 1 st row are initialised with the values 1,2,3,4 and 5 respectively. All
the other elements are initialised to zero. If the number of initial values exceeds the size
of the array, it is an error.

Example
int a[3][4]={1,2,3,4,5};
In the above example the values are assigned from left end to a[0][0], a[0][1], a[0]
[2], a[0][3] and a[1][0] according to the order of the storage representation.

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

The size of a one dimensional array need not be mentioned in its initialization. In
this case, the compiler will count the values assigned and take it as the size of that
array. In multi dimensional arrays the leftmost subscript may be omitted and all others
must be specified.
Example
int x[ ]={1,2,3,4,5};
The initialization makes the array x having 5 elements
Example
int a[ ][2]={1,2,3,4};
The initialization makes the array a having 2 rows and 2 columns.

For example a ten element array all having value 1 is initialised as


int num[10]={1,1,1,1,1,1,1,1,1,1};
But, an indirect way of initializing many elements of the array to zero value may be done
as follows because missing elements are assigned zero value automatically.
int num[10]={0};

Example
float x[ ][3]={{1},{2},{3}};
The declaration represents a three by three array with only the first element in each row
is initialised.

Example
float x[ ][3]={ {1,2,3} };
The above declaration represents a one by three array. Hence, the internal braces are
important in fixing the size of the dimension when leftmost dimension is omitted. The
missing elements of a initialised array are assigned the value zero.

à write a program to find maximum and minimum elements in the given array.

#include<stdio.h>
main()
{
int a[20],i, n, max, min;
printf ( "Enter value of n : ");
scanf ( "%d" , &n);
for( i=0; i<n; i++)
{
printf ( "Enter element for a[%d] : ",i );
scanf ( "%d", &a[ i ]);
}
max=min=a[0];
for( i=1; i<n; i++)
{
if(max < a[ i ])
max=a[ i ];
if(min > a[ i ])
min=a[ i ];
}

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

printf ( "Maximum element =%d\n Minimum element=%d\n", max, min);


}

OUTPUT:

Enter value of n : 5
Enter element for a[0] : 2
Enter element for a[1] : 5
Enter element for a[2] : 6
Enter element for a[3] : 3
Enter element for a[4] : 8
Maximum element =8
Minimum element=2

à Write a program to find no.of +ve and –ve elements in the given array.

main()
{
int a[10],p=0,n=0,i,x;
printf("enter the size of the array \n");
scanf("%d",&x);
printf("enter the elements in the array\n");
for(i=0;i<x;i++)
scanf("%d",&a[i]);
for(i=0;i<x;i++)
{
if(a[i]>0)
p++;
else
n++;
}
printf("no.of +ve numbers are %d",p);
printf("no of -ve numbers are %d",n);
}

OUTPUT:
enter the size of the array
5
enter the elements in the array
2
-8
5
-9
1
no.of +ve numbers are 3no of -ve numbers are 2

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

à Write a program to find given element is present in the array or not.


main()
{
int a[10],n,i,ele,c=0;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the 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\t",a[i]);
printf("\nenter the serching element\n");
scanf("%d",&ele);
for(i=0;i<n;i++)
{
if(a[i]==ele)
c++;
}
if(c>0)
printf("the element is found and %d times occurred\n",c);
else
printf("the element is not found\n");
}

OUTPUT:

enter the size of the array


5
enter the elements in to the array
3
1
9
4
2
the array elements are
3 1 9 4 2
enter the serching element
2
the element is found and 1 times occurred

à write a program to copy one array elements to another array.

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

main()
{
int a[10],b[10],n,i;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements in A are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n;i++)
b[i]=a[i];
printf("\n the array elements in B are\n");

for(i=0;i<n;i++)
{
printf("%d\t",b[i]);
}
}

OUTPUT:

enter the size of the array


5
enter the elements in to the array
2
1
4
3
5
the array elements in A are
2 1 4 3 5
the array elements in B are
2 1 4 3 5

à Write a program to print the elements in reverse order.

main()
{
int a[10],n,i;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements in A are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n the reverse elements in A are\n");

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

for(i=n-1;i>=0;i--)
{
printf("%d\t",a[i]);
}
}

OUTPUT:

enter the size of the array


5
enter the elements in to the array
1
2
3
4
5
the array elements in A are
1 2 3 4 5
the reverse elements in A are
5 4 3 2 1

à Write a program to write the given number in words.

main()
{
int a[10],n,i,size;
printf("enter the number\n");
scanf("%d",&n);
i=0;
while(n>0)
{
a[i]=n%10;
n=n/10;
i++;
}
size=i;
for(i=size-1;i>=0;i--)
{
switch(a[i])
{
case 0:printf("zero\t");break;
case 1:printf("one\t");break;
case 2:printf("two\t");break;
case 3:printf("three\t");break;
case 4:printf("four\t");break;
case 5:printf("five\t");break;
case 6:printf("six\t");break;

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

case 7:printf("seven\t");break;
case 8:printf("eight\t");break;
case 9:printf("nine\t");break;
}
}
}
OUTPUT:
enter the number
2345
two three four five
à write a program to find the binary equivalent of the given number

main()
{
int a[10],n,i,size;
printf("enter the number\n");
scanf("%d",&n);
i=0;
while(n>0)
{
a[i]=n%2;
n=n/2;
i++;
}
size=i;
for(i=size-1;i>=0;i--)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
enter the number
12
1 1 0 0

à Write a program to reverse the contents of the array in given 2 positions.


main()
{
int a[10],n,i,j,k,temp=0;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the 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\t",a[i]);
printf("\nenter the positions to be reversed\n");
scanf("%d%d",&j,&k);
temp=a[j-1];

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

a[j-1]=a[k-1];
a[k-1]=temp;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}

OUTPUT:
enter the size of the array
5
enter the elements in to the array
1
2
3
4
5
the array elements are
1 2 3 4 5
enter the positions to be reversed
3
4
1 2 4 3 5
à Write a program to replace an element in the array with the given element.
main()
{
int a[10],n,i,old,new,c=0;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the 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\t",a[i]);
printf("\nenter the element to be replaced\n");
scanf("%d",&old);
printf("enter the replacing elment\n");
scanf("%d",&new);
for(i=0;i<n;i++)
{
if(a[i]==old)
{
a[i]=new;
c++;
}
}
if(c==0)
printf("the element is not found\n");
else

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

{
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
}

OUTPUT:
enter the size of the array
5
enter the elements in to the array
1
2
3
4
5
the array elements are
1 2 3 4 5
enter the element to be replaced
4
enter the replacing elment
9
1 2 3 9 5

à Write a program to sort the array elements in ascending order.


main()
{
int a[10],n,i,j,k,temp=0;
printf("enter the size of the array\n");
scanf("%d",&n);
printf("enter the elements in to the 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\t",a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf(“after sorting the array elements are\n”);
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
à Write a program to find sum of array elements.

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

main()
{
int a[10],i,n,sum=0;
printf("enter the array size\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("the sum of the array elements is %d",sum);
}
OUTPUT:
enter the array size
5
enter the array elements
1
2
3
4
5
the sum of the array elements is 15

à Write a program to find sum of even no’s of sum of odd no’s in the given array.
#include<stdio.h>
main()
{
int a[10],i,n,evensum=0,oddsum=0;
printf("enter the array size\n");
scanf("%d",&n);
printf("enter the array elements\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",a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
evensum=evensum+a[i];
else
oddsum=oddsum+a[i];
}
printf("the sum of the even numbers are %d\n",evensum);
printf("the sum of odd numbers are %d\n",oddsum);
}
OUTPUT:
enter the array size
5

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

enter the array elements


1
2
3
4
5
the array elements are
12345
the sum of the even numbers are 6
the sum of odd numbers are 9

à Write a program to read elements from the keyboard and display the even number
array and odd number array separately.
#include<stdio.h>
main()
{
int a[10],i,n,e[10],o[10],oi=0,ei=0;
printf("enter the array size\n");
scanf("%d",&n);
printf("enter the array elements\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\t",a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
e[++ei]=a[i];
else
o[++oi]=a[i];
}
printf("\nthe even numbers array is\n");
for(i=1;i<=ei;i++)
printf("%d\t",e[i]);

printf("\nthe odd numbers array is\n");


for(i=1;i<=oi;i++)
printf("%d\t",o[i]);
}

OUTPUT:
enter the array size
6
enter the array elements
1
2

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

3
4
5
6
the array elements are
1 2 3 4 5 6
the even numbers array is
2 4 6
the odd numbers array is
1 3 5

à Write a program to print Fibonacci series using arrays.


main()
{
int a[10],n,i;
a[0]=0;
a[1]=1;
for(i=2;i<10;i++)
{
a[i]=a[i-1]+a[i-2];
}
for(i=0;i<10;i++)
printf("%d\t",a[i]);
}
OUTPUT:

0 1 1 2 3 5 8 13 21 34

à Write a program for linear search


main()
{
int a[10],i,x;
printf("enter a number\n");
scanf("%d",&x);
printf("enter the array elements\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("the array elements are\n");
for(i=0;i<10;i++)
printf("%d\t",a[i]);
for(i=0;i<10;i++)
if(a[i]==x)
break;

if(a[i]!=x)
printf("%d elememt is not found\n",x);
else
printf("%d element is found\n",x);
}
OUTPUT:

1
‘c’ programming . R.V.R & J.C COLLEGE OF
ENGINEERING.

enter a number
9
enter the array elements
1
2
3
4
5
5
6
7
8
9
the array elements are
1 2 3 4 5 5 6 7 8 9
9 element is found

You might also like