CP Lab Array 2
CP Lab Array 2
CP Lab Array 2
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:
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
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:
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
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
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.
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
Mathematic Physic
[0] [1]
studentmark2[0]
studentmark2[1]
…
studentmark2[8]
studentmark2[9]
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:
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