CP Lab Array 2

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

EKT 120 –Computer Programming

LAB 8

ARRAY (PART I)

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 must 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

Name of Array Array1[0]


Array1[1]
Array1[2]
Array1[3]
Array1[4]
Array1[5]
Array1[6]
Array1[7]
Array1[8]
Array1[9]
EKT 120 –Computer Programming
LAB 8

2.2 (2-D) Two dimensional array

Array declaration format :

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

Subscript or index must start with 0.

Example of an array data structure named Array2, which has 10 (5 x 2) components of


type integer.
int Array2[5][2];

The illustration of the above array:

Index / Subscript

[0] [1]
Name of Array Array2[0]
Array2[1]
Array2[2]
Array2[3]
Array2[4]
EKT 120 –Computer Programming
LAB 8

3. TASKS:

SHORT ANSWERED QUESTIONS

3.1 Write C statements for the followings.


A-1) Declare 1-dimensional array data type of float with size of 5 named number.
A-2) Using loops, read and store 5 values entered by user to array number.
A-3) Reassign the 3rd element with value of 10.
A-4) Using loops, display the 5 values from array number.

B-1) Declare 2-dimensional array data type of integer named round_number. The
size of row and column must be 10 and 2 respectively.
B-2) Assign the value of 10 to the first row and column of array round_number.
B-3) Using nested loops, read and store values from keyboard to all elements inside
the array.
B-4) Using nested loops, display the all values from the array round_number.

3.2 The following incomplete program in Figure 1 read 10 marks from keyboard, store in
the array and display the total marks and average marks at the end. Complete the
program by converting the pseudo-code written in line 10, 11, 12, 15, 18 and 20 into C
statements.
1 #include<stdio.h>
2
3 int main()
4 {
5 float marks[10],sum=0,average=0;
6 int i;
7
8 printf(“Please enter marks\n”);
9
10 for(i=0;i<10;i++) // loops to store all marks in the array
11 // read marks from keyboard and store them in the array
12 // compute sum of marks
13 //end loop
14
15 //compute average marks
16
17 for(i=0;i<10;i++) //loops to display all marks in the array
18 //display all marks from the array
19
20 //display total and average marks
21 return 0;
22 }

Figure 1
EKT 120 –Computer Programming
LAB 8

3.3 The following program in Figure 2 converts temperature in Celsius from array tempC
to Fahrenheit and store the value in parallel array tempF. The program will display
the temperature if the temperature exceeded certain threshold. Type, compile and run
the program.

1 #include<stdio.h>
2
3 int main()
4 {
5 float tempC[10]={5.3,3.2,1.7,-2.5,-4.5,-6.1,-0.5,0.3,1.2,3.4};
6 float tempF[10]={0};
7 int i;
8 for(i=0;i<10;i++)
9 {
10 tempF[i]=tempC[i]*33.8;
11 if(tempF[i]<32) // 0 C == 32 F
12 printf(“Low Temperature: %.2f\n”,tempF[i]);
13 }
14
15
16 return 0;
17 }

Figure 2

(a) Write the output of the program


(b) Add C statement in line 14 to display the value of temperature in both Celsius and
Fahrenheit (with 2 decimal point) to produce the following output.
tempC[0]=5.30, tempF[0]=41.54
tempC[1]=3.20, tempF[1]=37.76
tempC[2]=1.70, tempF[2]=35.06


tempC[9]=3.40, tempF[4]=38.12

3.4 The two dimensional (2-D) array in Figure 3 is initialized with predetermined values
of positive and negative numbers. Type and compile the program.

1 #include<stdio.h>
2
3 int main()
4 {
5 int listnum[5][2]={2,-3,7,9,-5,0,-8,1,-4,10};
6 int i,j,neg=0,pos=0;
7
EKT 120 –Computer Programming
LAB 8

8 for(i=0;i<5;i++)
9 {
10 for(j=0;j<1;j++)
11 {
12 if(listnum[i][j]<0)
13 neg++;
14 else
15 pos++;
16 }
17 }
18 printf(“Total positive=%d, total negative=%d\n”, pos,neg);
19 return 0;
20 }
Figure 3

(a) Predict the output of the program.


(b) Amend the program so that the process between line 12 to 15 is performed
against all elements in listnum. The output should be as follows:

Total positive=6, Total negative=4


EKT 120 –Computer Programming
LAB 8

PROBLEM SOLVING TASKS

3.5 Write a program that reads five (5) numbers and stores it to an array named number;
then calculate the sum of all 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

3.6 Write a program that reads Mathematic test marks of 10 (ten) student and stores it to
a 1-dimensional (1-D) array named studentmark1.Then, determine the
followings:
(a) Average marks of the test
(b) Highest mark
(c) Lowest mark
(d) Suppose that pass mark is 60 and above. Determine the number of passed and
failed students.

The sample output should be as follows:

Enter test marks: 72 64 53 81 40 90 76 39 68 84


Average marks : 66.70
Highest mark : 90.00
Lowest mark : 39.00
Number of passed students: 7
Number of failed students: 3

ADDITIONAL TASKS
EKT 120 –Computer Programming
LAB 8

3.7 Rewrite the program in task 3.6 using function to perform tasks (a) to (d).

3.8 Write a program that reads Mathematics and Physics test marks of 10 (ten) student and
stores it to a 2-dimensional (2-D) array named studentmark2.Then, determine the
followings:
(a) Average marks of each subject
(b) Highest mark of each subject
(c) Lowest mark of each subject
(d) Suppose that pass mark is 60 and above. Determine the number of passed and
failed students of each subject

Your array structure should be as follows:

Mathematic Physic
[0] [1]
studentmark2[0]
studentmark2[1]

studentmark2[8]
studentmark2[9]

Your program must produce the following output:

Enter Mathematic test marks: 72 64 53 81 40 90 76 39 68 84


Enter Physic test marks : 62 74 51 37 82 45 39 66 85 60

Average marks (Mathematic) : 66.70


Highest mark (Mathematic) : 90.00
Lowest mark (Mathematic) : 39.00
Number of passed students(Mathematic): 7
Number of failed students(Mathematic): 3

Average marks (Physic) : 60.10


Highest mark (Physic) : 85.00
Lowest mark (Physic) : 37.00
Number of passed students(Physic): 6
Number of failed students(Physic): 4

3.9 Rewrite the program in task 3.8 using function to perform tasks (a) to (d).
EKT 120 –Computer Programming
LAB 8

3.10 Write a program that calculates the multiple body mass index (BMI) where the
program read multiple weight and height and stored it into a parallel array weight[]
and height[], then the program calculates and display BMI based on weight in
kilograms (kg) and height in meters (m) based on the following formula:
weight
BMI 
height  height

For each BMI values, display the weight status of underweight, normal, overweight,
or obese, based on the following table:

BMI Weight Status


< 30.0 Underweight
< 36.0 Normal
< 46.0 Overweight
>= 46.0 Obese

At the end of the program, determine the percentage of each weight status that have
been calculated. The sample output should be as follows:
Enter height(m) and weight(kg) : 1.72 100
Enter more? ‘Y’ for yes, ‘N’ for no : Y

Enter height(m) and weight(kg) : 1.56 79


Enter more? ‘Y’ for yes, ‘N’ for no : Y

Enter height(m) and weight(kg) : 1.63 67


Enter more? ‘Y’ for yes, ‘N’ for no : Y

Enter height(m) and weight(kg) : 1.51 45


Enter more? ‘Y’ for yes, ‘N’ for no : Y

Enter height(m) and weight(kg) : 1.65 132


Enter more? ‘Y’ for yes, ‘N’ for no : Y

Enter height(m) and weight(kg) : 1.69 63


Enter more? ‘Y’ for yes, ‘N’ for no : N

Height=1.72, Weight=100.00, BMI=33.80


Height=1.56, Weight= 79.00, BMI=32.46
Height=1.63, Weight= 67.00, BMI=25.22
Height=1.51, Weight= 45.00, BMI=19.74
Height=1.65, Weight=132.00, BMI=48.48
Height=1.69, Weight= 63.00, BMI=22.06

Percentage Underweight = 50.00


Percentage Normal = 33.33
Percentage Overweight = 0.00
Percentage Obese = 16.67

You might also like