Lab 6 Arrays

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

DKT121 – Fundamental of Computer Programming Laboratory Module

LAB 8
ARRAYS I

School of Computer and Communication Engineering


Universiti Malaysia Perlis

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.

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 Array1, which has 10 components of type integer:

int Array1[10];

The illustration of the above array:

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

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 Array2, which has 6 components of type integer.

int Array2[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
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;

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


{
printf("\nEnter value %d : ",i+1);
scanf("%d",&value[i]); //read data entered and store it in array
//named value
}

/*To display the contents of the array*/


for (i=0; i<5; i++)
printf("\n\nvalue[%d] = %d",i,value[i]);
printf(“\n”);
return 0;
}/*end of main*/

a. Write the output of the program.

b. Amend the program, change the second for loop statement into:

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

Write and comment on the output of the program.

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;

/*To display the contents of array*/


for (i=0; i<2; i++)
for (j=0; j<3; j++)
printf("array2[%d][%d] = %d\n",i,j,array2[i][j]);
return 0;
}

a. Write the output of the program.

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:

Enter five numbers : 12 76 34 52 89


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

6
DKT121 – Fundamental of Computer Programming Laboratory Module

3.6 Additional Tasks (Optional):


a. Write a for loop that sums the even values from the LIST_SIZE element array list. For
example, the sum for this would be 104 (30 + 12 + 62).

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).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy