Lab 8 Arrays I: EKT 120 - Computer Programming Laboratory Module

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

EKT 120 – Computer Programming Laboratory Module

LAB 8
ARRAYS I

School of Computer and Communication Engineering


Universiti Malaysia Perlis

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.

2.1 (1-D) One dimensional array

Array declaration format :

<data type> <variable_name>[subscript/index]

Subscript or index shall start with 0.

Example of an array data structure named aiArray1, which has 10 components of type integer:

int aiArray1[10];

The illustration of the above array:

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

2.2 (2-D) Two dimensional array

Array declaration format :

<data type> <variable_name>[subscript/index][subscript/index]

Subscript or index shall start with 0.

Example of an array data structure named aiArray2, which has 6 components of type integer.

int aiArray2[3][2];

The illustration of the above array:

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

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

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

3. TASKS:

3.1 Declare the below array type variable.


a. Variable name is mark, consists of 20 components with data type of float.

b. Variable name is terracehouse, consists of 15 components with data type of


integer.

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

3.2 For Questions 3.2 (a - c) assume the following environment.


#define MAX 50
int a[MAX], i, j, temp;

a. What is the effect of this program segment?

for (i = 0; i < MAX / 2; ++i)


{
temp = a[i];
a[i] = a[MAX - i - 1];
a[MAX - i - 1] = temp;
}

b. What is the effect of the following program segment?

for (i = 0; i < MAX - 1; ++i)


if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}

c. What is the effect of the following program segment?


temp = 0;
for (i = 1; i < MAX; ++i)
if (a[i] > a[0])
++temp;

d. How many numbers can be stored in the array declared below?

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?

int k[6] = {0, 0, 0, 0, 0, 0}; Data: 2 0 1


int i, n;
for (i = 3; i < 6; ++i)
{
scanf("%d", &n);
k[n] = i;
}

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;

f. printf("%.2f\n", x[j] + 1);

g. printf("%.2f\n", x[j + 1]);

h. printf("%.2f\n", x[j * j]);

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;

ii. for (i = M; i < N; ++i)


m[i] = p[i] + q[i];

iii. for (i = 0; i < M; ++i)


for (j = 0; j < N; ++j)
m[i][j] = p[i][j] + q[i][j];

iv. for (i = 0; i < M; ++i)


for (j = i; j < N; ++j)
m[j][i] = p[j][i] + q[j][i];

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];
}

k. If m is a 7 x 7 integer matrix, what is displayed by this code fragment?


for (i = 0; i < 7; ++i)
printf("%8d", m[3][i]);
printf("\n");

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:

Enter five numbers : 12 76 34 52 89


The sum of the numbers is : 263
The numbers in reverse order are : 89 52 34 76 12

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

You might also like