Sie sind auf Seite 1von 11

C Programming Handout #6: ARRAYS 1

Handout #6

ARRAYS

What is an array?

 An array is a group of elements with the same data type (i.e., it is homogenous)

 An array is characterized by its name, dimension, size and element data type

 An array has a dimension which we will denote by d

- when d = 1, we say that the array is 1-dimensional; it is also referred to as a list

- when d = 2, we say that the array is 2-dimensional; it is also referred to as a


table

How do you declare a one-dimensional array in C?

The syntax is:

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

Examples:

char s[255];

int a[10];

int MyArray[100], M[50];

float X[25];

double Data[1000];

Key Point!

- array elements, if not explicitly initialized, will have garbage values

1
C Programming Handout #6: ARRAYS 2

How do you reference an element in a one-dimensional array?

The syntax is:

<array name> [<index>]

i.e., simply type the array name, followed by the left square bracket, followed by the array element index
and finally by the corresponding right square bracket.

Key Points!

- the range of values that you can use as array index is from 0 to <size> - 1

- the first element therefore is <array name>[0]

- the last element is <array name>[<size> - 1]

Examples of valid array element references are:


s[0]

a[5]

MyArray[5]

Examples of invalid array element references are:

s[-1] // index cannot be negative

X[2.5] // index cannot be a real number

Data[1000] // last index should size - 1

What operations can you perform on arrays?

Only operations on a per-array element basis are possible. Operations such as array to array assignment, or
adding two arrays are not supported by C language.

2
C Programming Handout #6: ARRAYS 3

There are basically two operations that can be performed on array elements, namely:

 read operation, specifically, read the content of an array element

 write operation, specifically, overwrite the original content of an array element

Examples of read operations are:

printf(“%d\n”, a[1]);

// assume that i was declared as an int variable

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

printf(“%lf\n”, Data[i]);

Examples of write operations are:

s[0] = ‘c’;

X[2] = 3.1416;

// assume that i was declared as an int variable

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

X[i] = i + 1;

Examples of combined read/write operations are:

M[3] = a[2] + MyArray[10];

Data[55] = X[9]/2.5 * a[1];

Can you pass arrays as parameters?

3
C Programming Handout #6: ARRAYS 4

Yes, it’s almost like passing simple variables as parameters. However, there are some subtle differences
that will be discussed when we go to the topic on pointers.

Example: The following code shows an example of a function that accepts an integer array as parameter.
The function simply prints all the elements from index 0 to n-1.

void PrintArrayElements(int A[], int n)

int i;

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

printf(“Element number %d = %d\n”, i, A[i]);

An example call to this function is:

PrintArrayElements(MyArray, 50);

Key Points!

- note that the size of the one-dimensional array is optional in the parameter declaration

- n in the example function above need not be the same as the size of the array; it maybe less
than the size but cannot be more because otherwise a logical error will occur

Example: The following code shows an example that will add two arrays, i.e., on a per element basis. The
sum will be stored on a third array.

void AddArrays(double C[], double A[], double B[], int n)

4
C Programming Handout #6: ARRAYS 5

int i;

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

C[i] = A[i] + B[i];

EXERCISES:

1. Write a function that will initialize a double data array elements to 0.0. Pass the array and a
size identified by an integer n as parameters.

2. Same as the problem above, but allow the user to input the value via scanf() statement.

3. Write a function that will return the lowest value in a double data type array. Pass the array
and a size identified by an integer n as parameters.

4. Write a function that will return the sum of all the values in a double data type array. Pass the
array and a size identified by an integer n as parameters.

5. Write a function that will return the count of negative values in an integer data type array.
Pass the array and a size identified by an integer n as parameters.

6. Write a function that will determine whether an integer say x exists or not in an integer array.
The function should return 1 if x is in the array, otherwise, it should return 0. Pass the array, a size
identified by n, and the integer x as parameters.

7. Let A and B be two arrays of integers. Assume that A has been initialized. Write a function
that will copy the contents of A to array B. Pass the two arrays and a size identified by n as
parameters.

8. Same as in the problem above, but the contents of B should be in reverse order as that of array
A, i.e., the first element of A should appear as the last element of B, the second element of A should
appear as the second to the last element of B and so on. Pass the two arrays and a size identified by n
as parameters.

9. Write a function that will return 1 if two character arrays A and B have the same array
elements, otherwise it should return 0. Assume that A and B have been initialized. Pass the arrays and
a size identified by n as parameters.

5
C Programming Handout #6: ARRAYS 6

How do you declare a two-dimensional array in C?

The syntax is:

<data type> <array name> [ <row size> ] [<column size>]

The array elements are arranged in row-major order.

Examples:

int m[3][3];

int MatrixA[3][3];

float MatrixB[4][8];

double Table[5][20];

6
C Programming Handout #6: ARRAYS 7

The graphical representation of two-dimensional array, in the case, for example of m is shown below.

Ex. int m[3] [3]; 0 1 3

0
1 m
2

A graphical representation of a matrix

0 Row 0 m[0][0]

1 m[0][1]

2 m[0][2]
Row 1
1 2
m[0][0] m[0][1] m[0][2]

m[1][0] m[1][1] m[1][0]


m[1][2]
m[2][0] m[2][1] m[1][1]
m[2][2]

A graphical representation row major order

RAM

7
C Programming Handout #6: ARRAYS 8

How do you reference an element in a two-dimensional array?

The syntax is:

<array name> [<row index>][<column index>]

Key Points!

- the range of row index is from 0 to <row size> - 1

- the range of column index is from 0 to <column size> - 1

- the first element therefore is <array name>[0][0]

- the last element is <array name>[<row size> - 1][<column size> - 1]

Examples of valid array access are:

printf(“%d\n”, MatrixA[0][2]);

MatrixB[3][1] = 1.25;

Table[0][0] = Table[1][2] + Table[5][1];

8
C Programming Handout #6: ARRAYS 9

Examples of invalid array access are:

printf(“%d\n”, MatrixA[1,2]); // syntax error!

MatrixB[-1][1] = 1.25; // negative index

Table[5][0] = 3.1416; // incorrect row index

How do you pass two-dimensional arrays as function parameters?

Basically the same as in the one-dimensional array case, but the size of the column will have to supplied! It
is not optional like the row size.

Example: The following function will print a table with 5 rows and 10 columns.

void PrintTableElements(int A[5][10])

int i, j;

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

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

printf(“Row %d, column %d element = %d\n”,

i, j, A[i][j]);

Key Points!

- normally, a double loop is used as control structure for processing two-dimensional array as
shown in the previous example

- also, normally, the processing is done row-by-row, and column-by-column within the same
row

9
C Programming Handout #6: ARRAYS 10

EXERCISES:

In the following exercises, assume that the 2D array to be manipulated will have at most 10 rows and 10
columns.

1. Write a function that will initialize the contents of a double data type 2D array to 0.0. Pass
the array as parameter.

2. Same as the problem above, but allow the user to input the value of the array element via
scanf() statement.

3. Write a function that will display all the elements of the 2D array. Elements should be printed
starting from the first row up to the last row, and from the first column to the last column within the
same row. Elements of the same row should be printed on the same line separated by two spaces. Pass
the array as parameter.

4. Write a function that will return the number of negative elements in the 2D array. Pass the
array as parameter.

5. Write a function that will print only the elements on the main diagonal of the 2D array. Pass
the array as parameter.

6. Write a function that will return the sum of the elements on a specified row. Pass the array
and the row as parameters.

7. Write a function that will return the sum of the elements on a specified column. Pass the
array and the column as parameters.

8. Assume three matrices of the same size, say matrix A, B and C. Write a function that will add
the two matrices A and B and store the sum to C. Matrix addition is done component-wise,

i.e., C[i ,j] = A[i, j] + B[i, j] where i, j are the row and column index respectively. Pass the three

arrays as parameters.

1
C Programming Handout #6: ARRAYS 11

Das könnte Ihnen auch gefallen