Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.

Similar presentations


Presentation on theme: "LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals."— Presentation transcript:

1 LAB-10 1-D Array I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

2 ICS101-LAB10-Term073 2 Background We’d like to develop a program to calculate student’s grades. How is it to store 5 grades? What if we have 100 grades? Simple variables are cumbersome for large number of values Array can store many values with one name Each value is identified by its index

3 ICS101-LAB10-Term073 3 Simple Variables vs 1-D Array INTEGER GRADE1, GRADE2, GRADE3, GRADE4, … OR INTEGER GRADE(100)

4 ICS101-LAB10-Term073 4 Declaring a 1-D Array TYPE ARRAY_NAME (DIMENSION) The index will be from 1 to DIMENSION For character values, specify the length: CHARACTER NAMES(10)*20 TYPE ARRAY_NAME(RANGE1:RANGE2) The index will be from RANGE1 to RANGE2 REAL X(-15:35) DIMENSION ARRAY_NAME(DIMENSION) DIMENSION ARRAY_NAME(RANGE1:RANGE2)

5 ICS101-LAB10-Term073 5 Accessing a Value in 1-D Array Use the index Do not use index outside the range of the array e.g.: INTEGER QUIZ1(10) QUIZ1(20) = 87 Above example does not cause error so be careful

6 ICS101-LAB10-Term073 6 Initializing Values In our IDE, the values are set to 0 for INTEGER and REAL We can use a loop to initialize the values DO 100 I = 1, 10 QUIZ (I) = 60 100CONTINUE We can also initialize it without mentioning the index QUIZ = 60

7 ICS101-LAB10-Term073 7 Using READ with 1-D Array Single line input Without index READ*, QUIZ Using implied loop READ*, (QUIZ(J), J = 1, 4) Multi-line input Using DO…CONTINUE or DO WHILE…END DO

8 ICS101-LAB10-Term073 8 Using PRINT with 1-D Array Single line output Without index PRINT*, QUIZ Using implied loop PRINT*, (QUIZ(J), J = 1, 4) Multi-line output Using DO…CONTINUE or DO WHILE…END DO

9 ICS101-LAB10-Term073 9 Questions When do we use array? How are the elements accessed? What happens if we give an index which is out of the range of the array? In what condition we need to use loop instead of mentioning the array name without index in reading or printing array values?

10 ICS101-LAB10-Term073 10 Exercises 1. Write a program to get 5 values and store them in an array. Calculate the sum and average of those values. Show them. 2. Check if the all of the values in the array are the same and show a message. 3. Write a program to store maximum 30 students. We have mid-term and final exam marks. Get the number of students N and then ask for the grades (only accept 0<=grade<=100). Mid-term and final exam get 40% and 60% weight respectively. Calculate the final marks and determine the minimum, maximum and average. Print the final marks, the min, max, and average.

11 ICS101-LAB10-Term073 11 Exercises 4. Now, we also have 3 quizzes. The weight for each quiz is 10%. Mid-term and final exam get 30% and 40% weight respectively. Print the final marks, the min, max, and average.

12 ICS101-LAB10-Term073 12 Lab 11 Exercises (1) Write a program to ask for REAL values. Use -99 as stopping value. The maximum number of values is 20. Keep the values in a 1-D array, excluding the stopping value. Calculate and show the following: total, average, minimum and maximum of the values.

13 ICS101-LAB10-Term073 13 Using Array as Subprogram Argument An array can be sent to a subprogram by using its name The dimension should also be sent to accommodate different dimensions

14 ICS101-LAB10-Term073 14 Example REAL TOT, X(10) READ*, X; TOT = TOTAL (X, 10) PRINT*, x PRINT*, TOT END REAL FUNCTION TOTAL (A, N) INTEGER N REAL A(N) INTEGER I DO 100 I = 1, N TOTAL = TOTAL + A(I) 100 CONTINUE RETURN END

15 ICS101-LAB10-Term073 15 Lab 11 Exercises (2) Write a function to check if two arrays are identical. Write a program that reverses the elements of a 1-D array having 5 integer elements. Example: if the elements are 2, 5, 3, 10, and 7, they are reversed into 7, 10, 3, 5, and 2. Write a general function/subroutine that reverses a 1-D array of integers.


Download ppt "LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals."

Similar presentations


Ads by Google