Lab 8 Arrays I: EKT 120 - Computer Programming Laboratory Module
Lab 8 Arrays I: EKT 120 - Computer Programming Laboratory Module
Lab 8 Arrays I: EKT 120 - Computer Programming Laboratory Module
LAB 8
ARRAYS I
1
EKT 120 – Computer Programming Laboratory Module
1. OBJECTIVES:
1.1 To introduce the array data structure.
1.2 To be able to define an array, initialize an array and refer to individual elements of an array.
1.3 To be able to use arrays to store and sort data in a program.
2. INTRODUCTION:
Array is a collection or a data structure of a fixed number of components or elements where in all of
the components or elements are of the same type.
To declare an array type of data structure, the command we use as below format.
Example of an array data structure named aiArray1, which has 10 components of type integer:
int aiArray1[10];
Index / Subscript
aiArray1[0]
Name of array
aiArray1[1]
aiArray1[2]
aiArray1[3]
aiArray1[4]
aiArray1[5]
aiArray1[6]
aiArray1[7]
aiArray1[8]
aiArray1[9]
2
EKT 120 – Computer Programming Laboratory Module
Example of an array data structure named aiArray2, which has 6 components of type integer.
int aiArray2[3][2];
[0][0] [0][1]
[1][0] [1][1]
[2][0] [2][1]
3. TASKS:
c. Variable name is matrix1, consists 5 rows and 5 columns with data type of
double.
d. Variable name is flathouse, consists of 15 rows and 10 columns with data type of
integer.
3
EKT 120 – Computer Programming Laboratory Module
double arr[10][5][6];
e. What will be the values of k[1] and k[3] after execution of the code segment below using the data
shown?
4
EKT 120 – Computer Programming Laboratory Module
For Questions f - h, refer to the declarations and initializations below. Indicate whether each of the
statements is valid. If the statement is valid, indicate what value is displayed. If the statement is
invalid, explain why.
double x[8] = {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5};
int j = 5;
i. Which code fragment below fills M x N matrix m with the sums of corresponding elements of M x
N matrices p and q?
i. m = p + q;
j. What is accomplished by this code fragment if m is a matrix with r rows and c columns?
for (i = 0; i < r; ++i)
{
sum[i] = 0;
for (j = 0; j < c; ++j)
sum[i] += m[i][j];
}
5
EKT 120 – Computer Programming Laboratory Module
l. Write a C program segment that takes a single M x N integer matrix argument, and finds and
display the largest value in the matrix.
m. Assume that v is a 5-element array, m is a 5 x 4 matrix, and r is a 4-element array. All three arrays
contain type double values. Write a code fragment that multiplies v by matrix m producing result r.
6
EKT 120 – Computer Programming Laboratory Module
3.3 Write a program that declares and initializes an array of 10 elements, it is a (1-D) one
dimensional integer array named temperature. Use the following temperatures to initialize
the array:
78 89 65 90 35 20 88 101 56 99
Then, display the contents of the array on the screen and calculate and display the mean (average)
of the temperatures.
3.4 Write a program that reads five (5) numbers and stores it to an array named number; then
calculate the total of the numbers and prints the numbers in reverse order. The output of your
program shall look like this:
Sample output:
7
EKT 120 – Computer Programming Laboratory Module
4 Additional Tasks
You have been asked to write one part of a detector analysis software package for a telescope. Your
program takes as an input the brightness [0 -13] of each point in a two-dimensional array
representing an image of the sky. Use a 5 x 5 integer array for this image. Find and display the x and
y coordinates and the value of the brightest pixel. If more than one pixel has this highest value,
information for all highest-valued pixels should be displayed.
Sample output:
8
EKT 120 – Computer Programming Laboratory Module