0% found this document useful (0 votes)
10 views5 pages

Arrays

Uploaded by

Anchal sandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Arrays

Uploaded by

Anchal sandhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Arrays

An array is a collection of variables of the homogeneous data type that are referred to through a
common identifier and are stored contiguously in memory. An Individual element in an array is
accessed by an index value. The base address corresponds to the first element and the highest
address to the last element of an array. Arrays may have from one to several dimensions.
Irrespective of their dimensions, the arrays are stored linearly in the memory.
The size of an array is predefined and cannot be changed during run time. It can prove to be a big
disadvantage when working with large size of data elements. Under utilization of elements cause
huge amount of unused memory that is eventually wasted.

Declaration of an array

Like any other variable of built-in data type, array must be explicitly declared to enable the compiler
to allocate space for it in memory. How much memory is to be allocated depends on the size and type
of the array.

Syntax,

type name[size];

For example,
int array[10]; // an array with 10 elements

Memory allocation of array

Memory allocated to array depends on the size or number of elements of an array and the
base type of the array.
Let us consider the following memory allocation to an integer array of 5 elements in
memory

For example,
int array[10]; // an array with 10 elements

integer takes 2 bytes in memory, so an array of 10 elements will take 20 bytes in memory.

Initializing array elements

An array of any data type can be initialized when it is declared. Not initializing an array
results in garbage value in all the elements of the array.
For example,

int array[5] = {10,111,12,2,5}; // all five members initialized

For example,
2

int array[5] = {100,11}; // only two elements initialized

Accessing elements of an array

An element of the array is accessed by indexing the array name. Each element of an array is uniquely
identified by its index. By indexing we mean to place the index of the element within square brackets
after the name of the array. The value or index given in square brackets is also known as subscript
value. The smallest index starts from 0 and the largest index value is size-1.
For example,

int array[5]; // array of 5 elements

array[0] = 200; // accessing 1st element of the array

array[2] = 210; // setting the third element = 210

To display initialized array elements

#include<conio.h>

#include<stdio.h>

void main()

//initializing array elements


int array[10]= {11,22,33,44,55,66,77,88,99,100}, count;

//displaying array elements


printf(“Array elements are initialized with the following values\n”);
for (count = 0; count < 10; count ++)

printf("\nValue of element %d of the array :- %d", count, array[count]);


}
getch();

To find smallest element in the array

#include<conio.h>
3

#include<stdio.h>

void main()

{
int array[10], loc, count;

// to accept input from the user


printf(“Enter array elements\n\n”);
for (count = 0; count < 10; count ++)

{
printf("Enter element %d of the array :- ", count);
scanf("%d",&array[count]);

// to find the smallest element in the array

loc =0; // let the consider the first element to be the smallest element for (count
= 1; count < 10; count ++)
{
if (array[loc] > array[count])
loc = count;

}
printf("Location of the smallest element %d is :- %d",array[loc],loc);
getch();

Multidimensional arrays

C programming also supports implementation of multidimensional arrays. Simplest form of it is 2-D


arrays in which the data is logically represented in the form of rows and columns. We can also
implement 3-D arrays that are used to represent complex data structures like cubes etc.

For example,

int arr[3][3];

The last statement is used to create an array with 3 rows and 3 columns. Following is the logical
representation of array in memory.
4

column
row

arr[0][0] arr[0][1] arr[0][2]


Figure 9.5 Logical
representation of 2-D array. arr[1][0] arr[1][1] arr[1][2]

arr[2][0] arr[2][1] arr[2][2]

To display the diagonal elements of a square matrix

#include<conio.h>

#include<stdio.h>
main( )

int array[3][3], i, j ;

//accepting input of array

printf(”Enter elements of the array:\n\n");

for (i = 0; i < 3; i++)

{
for (j = 0; j < 3; j++)

{
printf("Enter element [%d][%d] of array :- ",i, j);
scanf("%d",&array[i][j]);

//matrix display
printf(”Original
matrix:\n\n"); for (i =
0; i < 3; i++)
{
printf(“\n”);
for (j = 0; j < 3; j++)

{
printf("%d\t",array[i][j]);
5

// diagonal elements of
array
printf(”\n\nDiagonal
elements”); for(i=0,j=0;
i<3; i++, j++)
{

printf("\n%d",array[i][j]);
}

getch( );

You might also like