Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


Computing for Engineers

Lecture 5

Arrays


1. If there were no arrays in C …

We have seen so far in C that the variables in our programs are of the basic built in types int, float, char and double.

Suppose we want to write a program to read in the marks of 1000 students on a  course.  If  we  wanted  to  write  this  using  only  the  techniques  we  know already:

int student_mark_1;

int student_mark_2;

.

.

int student_mark_998;

int student_mark_999;

int student_mark_1000;

.

.

printf ("Enter mark for student 1\n");

scanf ("%d",&student_mark_1);

printf ("Enter mark for student 2\n");

scanf ("%d",&student_mark_2);

printf ("Enter mark for student 3\n");

scanf ("%d",&student_mark_3);

.

.

printf ("Enter mark for student 1000\n"); scanf ("%d",&student_mark_1000);

Clearly this is extremely inefficient and would be impossible to type in.

Using an array (with a loop such as a for loop) would reduce the size of the code from several 1000 lines down to around 10 lines.

2. Introduction to arrays

Arrays are data structures which hold multiple variables of the same data type. They allow rapid direct access to any of the variables held.

A program written without using an array might look like:

int id1 = 101;

int id2 = 253;

int id3 = 516;

We declare an array  by making a declaration with the number of elements indicated in square brackets (3 in this example).

We  can  then  access  the  array  by  using  the  variable  name,  but  in  square brackets we insert the index of the array – “index” means its position starting at zero.

In our example above, we could use arrays as follows :

/* The number of elements */ /* First element is always zero */

/* Last element is always

no. of elements - 1 */

The variable id has been declared as an array of 3 integer variables.

Thus, the whole collection of person ids can be handled through referencing a single variable name. Each individual id within the collection can be easily and rapidly accessed.

Note again that all arrays in C start at zero. Therefore, a 100-element array:

int n[100];

would go from n[0] to n[99].

3. Declaring arrays

Arrays may consist of any of the valid data types. Arrays are declared along        with all other variables in the declaration section of the program. The following

program declares an array of 100 integer and 200 floating point variables.        Individual array elements can then be used in exactly the same way as integer and floating point variables:

#include

int main(void)

{

int numbers[100];

float averages[200];

float sum;

numbers[10]=5;

numbers[10]++;

averages[5]=2.3

sum=averages[5]+numbers[10];

return 0;

}

3.1.     Initializing arrays and reading values into arrays

Obviously we can assign values to array elements after we have declared the    arrays. However, we can also initialize arrays (that is, give the array elements    initial values at the same time as declaring the array.) This is done by enclosing a list of initial values in braces {..}.

#include

int main(void)

{

int x, values[]={10,20,30,40,50,60,70,80};

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

printf("values [%d] is %d \n",

x,values[x]);

return 0;

}

Sample program output

values[0] is 10

values[1] is 20

.

.

values[7] is 80

Notice that inside the square bracket when the array is declared, there is no  variable to indicate how big the array is to be (eg. int values[]). In this case, C

initializes the array to the number of elements that appear within the braces.    So in the above program, values has 8 elements which are indexed from 0 to 7.

In real applications it is much more common to read array values from the    user. Consider the program we saw at this start of this lecture for reading in 1000 student numbers:

int student_mark_1;

int student_mark_2;

.

.

int student_mark_1000;

.

.

printf ("Enter mark for student 1\n");

scanf ("%d",&student_mark_1);

printf ("Enter mark for student 2\n");

scanf ("%d",&student_mark_2);

.

printf ("Enter mark for student 1000\n"); scanf ("%d",&student_mark_1000);

We can easily rewrite this program to read values into an int array:

#include

int main(void)

{

int i, student_mark[1000];

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

{

printf("Enter mark for student %d:", i+1); scanf ("%d", &student_mark[i]);

}

return 0;

}

In this program we have decided to use (i+1) within “printf” statement, so that the output messages run from 1-1000, instead of 0-999.


4. Multi-dimensional arrays

We can think of the arrays we have seen so far as one dimensional. In other       words, array elements are accessed by a single index. Multi-dimensional arrays have two or more indices. Thus a two dimensional array requires two indices     with which to access each element. Think of a two dimensional array as a grid   (or matrix) of numbers arranged row and column wise:

(0,0)

(0,1)

(0,2)

(0,3)

(0,4)

(1,0)

(1,1)

(1,2)

(1,3)

(1,4)

(2,0)

(2,1)

(2,2)

(2,3)

(2,4)

(3,0)

(3,1)

(3,2)

(3,3)

(3,4)

(4,0)

(4,1)

(4,2)

(4,3)

(4,4)

The diagram above shows a 5x5 grid with each box containing its index values. Thus element (1,2) specifies the 2nd  row and 3rd column (remember row and     column indices start from zero!) We can think of a two dimensional array in      exactly the same way. Thus we could declare a two dimensional integer array   as follows: