Lab 6 Arrays
Lab 6 Arrays
Lab 6 Arrays
LAB 8
ARRAYS I
1
DKT121 – Fundamental of 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 Array1, which has 10 components of type integer:
int Array1[10];
Index / Subscript
Array1[0]
Name of array
Array1[1]
Array1[2]
Array1[3]
Array1[4]
Array1[5]
Array1[6]
Array1[7]
Array1[8]
Array1[9]
2
DKT121 – Fundamental of Computer Programming Laboratory Module
Example of an array data structure named Array2, which has 6 components of type integer.
int Array2[3][2];
[0][0] [0][1]
[1][0] [1][1]
[2][0] [2][1]
3. TASKS:
______________________________________________________________
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
DKT121 – Fundamental of Computer Programming Laboratory Module
3.2 The program below is to ask user to enter 5 integers into an array named value. Then it displays
the elements in the array to the monitor. Type, compile and run the program.
#include <stdio.h>
int main ()
{
int value[5]; //array declaration int i;
b. Amend the program, change the second for loop statement into:
4
DKT121 – Fundamental of Computer Programming Laboratory Module
3.3 The program is to initialize a (2-D) two dimensional array with predetermined values. The
program uses a nested for loop to display the contents of the array named array2. Type and
compile the program.
#include <stdio.h>
int main ()
{
int array2[2][3] = {51,52,53,54,55,56}; //array declaration and initialization int i,j;
b. Amend the program so that the output is displayed in the following format.
51 52 53
54 55 56
5
DKT121 – Fundamental of Computer Programming Laboratory Module
3.4 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.5 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:
6
DKT121 – Fundamental of Computer Programming Laboratory Module
Array list
list[0] list[1] list[2] list[3] list[4] list[5]
30 12 51 17 45 62
b. Write a for loop that sums the even-numbered elements (elements 0, 2, and 4) from
array list. For the list shown in (a), the sum would be 126 (30 + 51 + 45).