Beruflich Dokumente
Kultur Dokumente
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
Examples:
char s[255];
int a[10];
float X[25];
double Data[1000];
Key Point!
1
C Programming Handout #6: ARRAYS 2
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
a[5]
MyArray[5]
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:
printf(“%d\n”, a[1]);
printf(“%lf\n”, Data[i]);
s[0] = ‘c’;
X[2] = 3.1416;
X[i] = i + 1;
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.
int i;
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.
4
C Programming Handout #6: ARRAYS 5
int 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
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.
0
1 m
2
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]
RAM
7
C Programming Handout #6: ARRAYS 8
Key Points!
printf(“%d\n”, MatrixA[0][2]);
MatrixB[3][1] = 1.25;
8
C Programming Handout #6: ARRAYS 9
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.
int i, j;
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