Arrays in C Programming Language

Arrays in C Programming Language

An array is a sequence of data in memory, wherein all data are of the same type and are placed in physically adjacent locations. Thus array is a collective name given to the group of similar types and quantities.

Array declarations:

Syntax

<data type><array name>[no. of elements];

e.g.

int arr[10];

Accessing array elements

once an array is declared, we refer the individual elements with the help of subscript. The number in the brackets following the array name is called “subscript”. This number specifies the element’s position in the array.

Entering data into an array

Usually, we use a “for” loop to enter data into the elements of an array. Note that before using an array its type and size must be mentioned.

Array initialization

int num[6]={2,4,12,5,45,6};

Passing array elements to a function

array elements can be passed to a function by calling the function by value or by reference. Following is an example of the “call by value” method.

#include<stdio.h>
main()
{
void display(int );
int i;
int a[]={35, 45, 55, 65, 75};
for (i=0; i<5; i++)
display(a[i]);
}
void display(int m)
{
printf(“%d”, m);
}

Passing an entire array into a function

#include<stdio.h>
main()
{
void display(int *);
int a[]={35, 45, 55, 65, 75};
display(&a[0]);
}
void display(int *m)
{
int i;
for(i=0; i<5; i++)
{
printf(“%d”, *m);
}
}

Naming and declaring arrays

The rules for assigning names to arrays are the same as for the variable name. an array name must be unique. It can’t be used for another array or for any other identifier (variable, constant, and so on). As you have probably realized, array declarations follow the same form as declarations of non-array variables, except that the number of elements in the array must be enclosed in square brackets immediately following the array name. when you declared an array, you can specify the number of elements with a literal constant or with a symbolic constant created with the #define directive. Thus, the following:

#define MONTHS 12
int array[MONTHS];

is equivalent to this statement:

int array[12];

with most compilers, however, you can’t declare in array’s elements with a symbolic constant created with a const keyword.

const int MONTHS=12;
int array[MONTHS]; /*wrong*/

Single-dimensional arrays

A single-dimensional array has only a single subscript. A subscript is a number in brackets that follow an array’s name. this number can identify the number of individual elements in the array. An example should make this clear. For the business expenses program, you could use the following line to declare an array of type float:

float expenses[12];

The array is named expenses, and it contains 12 elements. Each of the 12 elements is the exact equivalent of a single float variable. All of C’s data types can be used for arrays. C arrays elements are always numbered starting at 0, so the 12 elements of expenses are numbered 0 through 11. In the preceding example, January’s expenses total would be stored in expenses[0], February’s expenses[1], and so on.

When you declare an array, the compiler sets aside a block of memory large enough to hold the location of array declarations in your source code is important. As with no array variables, the declaration’s location affects how your program can use the array.

Individual elements of the array are accessed by using the array name followed by the element subscript enclosed in square brackets. For example, the following statement stores the value 89.95 in the second array element (remember, the first array element is expenses[0], not expenses[1]).

expenses[1]=89.95;

Likewise, the statement

expenses[10]=expenses[11];

Assign a copy of the value that is stored in array element expenses[11] into array element expenses[10]. When you refer to an array element, the array subscript can be a literal constant, as in these examples. However, your programs might often use a subscript that is a C integer variable or expression, or even another array element. Here are some examples.

float expenses[100];
int a[10];
/*additional statement goes here*/
expenses[i]=100;
expenses[2+3]=100;
expenses[a[2]]=100;

The last example might need and explanation. If, for instance, you have an integer array named a[] and the value 8 is stored in element a[2], writing expenses[a[2]] has the same effect as writing expenses[8];

When you use arrays, keep the element numbering scheme in mind. In an array of n elements, the allowable subscripts range from 0 to n-1. If you use the subscript value n, you might get program errors. The C compiler doesn’t recognize whether your program uses a subscript that is out of bounds. Your program compiles and links, but out-of-range subscripts generally produce erroneous results.

Sometimes you might want to treat an array of n elements as if its elements were numbered 1 through n. for instance, in the previous example, a more natural method might be to store January’s expense total in expenses[1], February’s expenses[2], and so on. The simplest way to do this is to declare the array with one more element than needed and ignore element 0. In this case, you would declare the array as follows. You could also store some related data in element 0 (the yearly expense total, perhaps).

float expenses[13];

Multi-dimensional array

so far we have explored arrays with only one dimension. It is also possible for arrays to have two or more dimensions. Here we will learn how to access a two-dimensional array.

Declaration

Syntax:

<data type><array name>[size][size];

e.g.

int arr[5][5];

Initializing a two-dimensional array

e.g.

int rollmarks[4][2]= {
{1, 45},{2, 55},{3, 65},{4, 75}
}

Entering data into a 2-dimensional array

The values are entered into a 2-dimensional array using nested for loop. Note that before using the array its type and size must be mentioned.

main()
{
int i, j, arr[2][2];
for(i=0;i<2;i++)
for(j=0;j<2:j++)
{
printf(“Enter the value for cell %d%d:”, i, j);
}
}

Program for double dimensional Array

#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], i, j, r, c;
clrscr();
printf(“Enter order of matrix:”);
scanf(“%d%d”, &r, &c);
printf(“Enter %d values”, r*c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf(“%d”, &a[i][j]);
printf(“\n Elements of array a:”);
for(i=0;i<r;i++)
{
printf(“%d\t”, a[i][j]);
}
}
getch();
}

Program for matrix addition

#include<stdio.h>
#include<conio.h>
main()
{
int a[10][1], b[10][10], d[10][10];
int i, j, r, c, p, q;
clrscr();
printf(“Enter order of matrix a:”);
scanf(“%d%d”, &r, &c);
/*accepting values from user*/
printf(“Enter %d values”, r*c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf(“%d”, &a[i][j]);
printf(“Enter order of matrix b:”);
scanf(“%d%d”, &p, &q);
/*accepting values from user*/
printf(“Enter %d values”, p*q);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf(“%d”, &a[i][j]);
/*printing values*/
printf(“Elements of array a:”);
for(i=0;i<r;i++)
{
printf(“\n”);
for(j=0;j<c;j++)
{
printf(“%d\t”, a[i][j]);
}
}
printf(“Elements of array b:”);
for(i=0;i<p;i++)
{
printf(“\n”);
for(j=0;j<q;j++)
{
printf(“%d\t”, b[i][j]);
}
}
/*if rows and columns of two matrices are equal*/
if(r==p && c==q)
{
for(i=0;i<r;i++)
{
for(j=0;j<q;j++)
d[i][j]=a[i][j]+b[i][j];
}
printf(“\nElements of array d:”);
for(i=0;i<p;i++)
{
printf(“\n”);
for(j=0;j<q;j++)
{
printf(“%d\t”, d[i][j]);
}
}
}
else
printf(“\nAddition is not possible”);
getch();
}

Maximize array size

Because of the way memory models work, you shouldn’t try to create more than 64 KB of data variables for now. Generally, 64 KB is enough data space for programs, particularly the relatively simple programs you will write as you work through this book. A single array can take up the entire 64 KB of data storage if your program uses no other variables. Otherwise, you need to apportion the available data space as needed.

The size of an array in bytes depends on the number of elements it has, as well as each element’s size. Element size depends on the data type of the array and your computer. These are the data type sizes for many PCs.

Element data type Element size (bytes)
Int 2 or 4
Short 2
Long 4
Float 4
Double 8