Arrays

 

The simple data types we have considered so far represent single values. In many situations, however, it is necessary to process a collection of values that are related in some way, for example, a list of student marks or a collection of measurements resulting from some experiment. Processing such collections using only simple variables can be extremely cumbersome and for this reason, most high level languages include data structure known as array.

There are two types of arrays:

 

One dimensional array:

In some cases, it is better to store the data items in a sequence of main memory locations, each of which can be accessed directly. Such a data structure is called an array. In C programming language, we can refer to each individual item or element in the array by a subscript in brackets.

For example, if the marks of ten students are to be processed in a program, we might use an array to store them. The computer must first be instructed to reserve a sequence of ten memory locations for the marks.

 

Declaring one dimensional array:

An array can be declared by specifying its data type and size as follows:

data-type variable[size of array];

Suppose you want to store the marks of ten students in an array then array for storing marks can be declared as:

int marks [10];

Here int specifies the type of elements or items that will be stored in the array and the word marks is the name of the array. The [10] following the array name tells how many variables of type int will be in our array and brackets tell the compiler that we are dealing with an array.

To refer to an individual element of the array, we use subscripts, the numbers in the brackets following the array name. This number specifies the element’s position in the array. In C programming language, all the array elements are numbered, starting at 0.

For example, the assignment statement

marks [4] = 83;

It stores the value 83 in the fifth location of the array because the numbering starts with 0. Thus, the last array element will have subscript one less than the size of the array. That means the array subscript will be in the range of 0 to 9.

Consider the following program that reads marks of ten students and prints the marks that are above average.

# include <stdio. h>

# include <conio. h>

void main ()

{

int marks[10], j, sum, avg;

for (j = 0; j < 10; j ++)

{

printf (“enter marks:”);

scanf (“%d”, marks [j]);

}

sum = 0;

for (j = 0; j < 10 ; j ++)

sum = sum + marks [j];

avg = sum/10;

printf (“list of marks above average\n”);

for (j = 0; j < 10; j ++)

if (marks [j] > avg)

printf (“\n %d”, marks[j]);

}

In the above example, the first for loop allows the user to enter 10 marks and stores them in the marks array. The second loop finds the sum of all the marks and then the average marks are calculated. Finally the last for loop compares the marks of each student with the average marks and prints those that are greater than average.

 

Initializing one dimensional array:

Consider the following example in which the program demonstrates the initialization of an array:

CODE:

# include <stdio. h>

# include <conio. h>

# define len 5

void main ()

{

int a [len] = {10, 34, 16, 40, 22}, j, sum, avg;

sum = 0;

for (j = 0; j < len; j ++)

sum = sum + a [j];

avg = sum / 5;

printf (“\nthe average of given five numbers is %d”, avg);

}

The above program used the variable len that is a constant and is defined as 5.

int a [len] = {10, 34, 16, 40, 22}

We can also omit the size of the array, leaving an empty pair of brackets following the array name such as:

int a [] = {10, 34, 16, 40, 22};

If the size of the array is not defined, the compiler will count the number of items in the initialization list and fix that as the array size.

If the number of values supplied is less than the actual number of items in the list then extra spaces in the array will be filled in with zeros. If the number of values supplied is greater than the actual number of items then the compiler will give an error message.

 

Compile time Array Initialization:

Compile time array initialization and simple array initialization are the same. Consider the following line of code which can be called as array initialized at compilation time:

data-type variable [size of array] = {list of variables};

 

Run time Array initialization:

We can initialize an array at run time that is by using the scanf statement. Consider the following lines of code:

for (int x = 0; x < 9; x ++)

{

printf (“Enter elements of array”);

scanf (“%d”, &arr [x]);

}

Run time initialization of array is done when we have an array of large size or when the user wants to enter elements according to his need.

 

Two dimensional arrays:

There are many problems in which the data being processed can be naturally organized as a table. For example, if for each of twenty five different students, four test marks are to be processed in a program, the data can be arranged in a table having twenty five rows and four columns.

Student Number                                Test Number

                      

1 2 3 4
1 79 80 64 91
2 99 90 92 97
. . . . .
. . . . .
25 100 62 73 88

In this table, the four marks of student 1 are in the first row, the four marks of student 2 are in the second row, and so on. These one hundred data items can be conveniently stored in a two dimensional array. The declaration

int marks[25][4];

It reserves one hundred memory locations for these data items. The array element marks [1][2] refers to the entry in the second row and third column of the table, that is, to the marks 92 earned by 2 students in test 3. In general,

Marks[i][j]

Refers to the entry in the row i and column j, that is, to the marks of student i in test j.

EXAMPLE:

To calculate the average marks in the four tests for each of the twenty five students, it is necessary to calculate the sum of the entries in each row and to divide each of these sums by 4. The following program can be used to carry out this computation and to display the average marks for each student.

# include < stdio. h>

# include < conio. h>

void main()

{

int marks [50][10], i,  j, student, test, sum, avg;

printf (“enter number of students”);

scanf (“%d”, &student);

printf (“enter number of tests”);

scanf (“%d”, &test);

printf (“enter marks of each students in one line”);

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

for (j = 0; j < test; j ++)

scanf (“%d”, marks [i][j]);

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

{

sum = 0;

for (j = 0; j < test; j ++)

sum = sum + marks [i][j];

avg = sum / test;

printf (“average marks for student %d are %d”, i+1, avg;

}

}

 

Matrix Transpose:

The transpose of a two dimensional array is one formed by interchanging the rows and columns of the original matrix. For example, if

For example

Then the transpose of A is

Then the transpose of A is

If the matrix has the same number of rows and columns, it is called a square matrix.

The following program reads a square matrix and prints its transpose.

# include <stdio. h>

# include <conio. h>

void main()

{

int i, j, a [10][10], n, holder;

printf (“\nenter size of square matrix(max. 10):”);

scanf (“%d”, n);

printf (“enter each row in one line\n”);

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

{

for (j = 0; j < n; j ++)

scanf (“%d”, &a [i][j]);

}

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

for(j = i + 1; j < n; j ++)

{

holder = a [i][j];

a [i][j] = a [j][i];

a [j][i] = holder;

}

printf (“\n*****THE TRANSPOSE*****\n”);

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

{

printf (“\n”);

for (j = 0; j < n; j ++)

printf (“%d”, a[i][j]);

}

}

 

Initializing Two-Dimensional Array

The following program demonstrates the initializing of a two dimensional array and count the number of positive values in it.

# include <stdio. h>

# include <conio. h>

#define row 3

# define col 4

void main()

{

int i, j, pos;

int a [row][col] = { {4,45,-6,9}, {-8,22,-37,10}, {50,-5,62,85} };

pos = 0;

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

for (j = 0; j < col; j ++)

if (a [i][j] > 0)

pos = pos+1;

printf (“the number of positive values = %d”, pos);

}

In the above program note the format used to initialize the array, an outer set of braces and then 3 inner sets of braces, each with 4 items separated by commas as well.

 

Run time initialization of Array:

Consider the following example in which the two dimensional array is initialized at run time:

CODE:

# include <stdio. h>

# include <conio. h>

void main ()

{

int array [3][4];

int x, y, k;

printf (“Enter elements of array”);

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

{

for (y = 0; y < 4; y ++)

{

scanf (“%d”, &array [x][y]);

}

}

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

{

for (y = 0; y < 4; y ++)

{

printf (“%d”, array [x][y]);

}

}

getch ();

}