Presentation is loading. Please wait.

Presentation is loading. Please wait.

情報基礎 A Lecture 9 Takeshi Tokuyama ・ Jinhee Chun Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis.

Similar presentations


Presentation on theme: "情報基礎 A Lecture 9 Takeshi Tokuyama ・ Jinhee Chun Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis."— Presentation transcript:

1 情報基礎 A Lecture 9 Takeshi Tokuyama ・ Jinhee Chun Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information Systems

2 PROGRAMMING VBA ARRAY, FOR - NEXT

3 Array Collection of the same data type – For large amount of data or sequential reading of data a b c d e f Integers Array of Integer x(0)x(1)x(2)x(3) x(4)x(5) Index Name of Array Element

4 Array Declaration x(1)x(2)x(3)x(4) x(5)x(6) x(0)x(1)x(2)x(3) x(4)x(5) Six boxes to store Integer type variables Box name: x, Index: 0 to 5 Dim x(5) As Integer Array name Maximum index Data type x(5) : create six boxes named x which allows to contain Integer Index starts at “0” Index starts at “0”

5 Array Declaration score(0) score(1) score(2)score(3) score(4) score(5) StudentID: 1 Score: 100 StudentID: 2 Score: 65 StudentID: 3 Score: 76 StudentID: 4 Score: 87 StudentID: 5 Score: 61 StudentID: 6 Score: 99 score(0) = 100 score(1) = 65 score(2) = 76 score(3) = 87 score(4) = 61 score(5) = 99

6 Array Declaration with Index x(1)x(2)x(3)x(4) x(5)x(6) Six boxes to store Integer type variables Box name: x, Index: 1 to 6 Dim x(1 to 6) As Integer Array name Index Range Data type Element Box x(1), x(2), …, x(6) Array nameIndex

7 Array Declaration 1Sub array1() 2 Dim score(5) As Integer 3 score(0) = 100 4 score(1) = 65 5 score(2) = 76 6 score(3) = 87 7 score(4) = 61 8 score(5) = 99 9 10 MsgBox score(0) 11 MsgBox score(1) 12 MsgBox score(2) 13 MsgBox score(3) 14 MsgBox score(4) 15 MsgBox score(5) 16End Sub

8 Array Declaration 1Sub array2() 2 Dim score(5) As Integer 3 Dim id As Integer ‘variable to put StudentID 4 5 score(0) = 100 6 score(1) = 65 7 score(2) = 76 8 score(3) = 87 9 score(4) = 61 10 score(5) = 99 11 12 id = InputBox(“Enter StudentID(1 to 6) to see the score.”) 13 MsgBox “StudentID: ” & id & “, Score: ” & score(id -1) 14 15End Sub

9 For - Next Execute code repeatedly for defined times – Counter variable to define the range of iteration Counter variable: i Action1 FALSE TRUE i=0 i<=5 i=i+1 overwrite i with i+1 For i = 0 To 5 Step 1 Next i i: Counter name Counter initial value Counter maximum value Action1 Counter increment step 6 times

10 Counter What is i = i + 1 ? – Store i + 1 to i – Increment i by 1 Dim i As Integer Counter name(arbitrary name): i 0 0 i i i i + 1 i i

11 i i i i 0+1 i i 0 i i + 1 i i 1+1 1 2 i i + 1 i i 2+1 3 i = i +1

12 1Sub array3() 2‘Display StudentID and score of all student using For - Next 3‘score: array name, i: counter name 4 Dim score(5) As Integer 5 Dim i As Integer 6 7 score(0) = 100 8 score(1) = 65 9 score(2) = 76 10 score(3) = 87 11 score(4) = 61 12 score(5) = 99 13 14 For i = 0 To 5 Step 1 15 MsgBox “StudentID: ” & id & “, Score: ” & score(i - 1) 16 Next i 17End Sub

13 14 name(0) = “Koji Tanaka” 15 name(1) = “Hiroshi Abe” 16 name(2) = “Akiko Ito” 17 name(3) = “Ichiro Suzuki” 18 name(4) = “Takako Kato” 19 name(5) = “Junpei Kimura” 20 21 For i = 0 To 5 Step 1 22 MsgBox “StudentID: ” & i & “, Name: ” & name(i) & “, Score: ” & score(i) 23 Next I 24End Sub

14 Exercise Change array4() program to display if a student is “Pass” or “Fail” – Program name Grading() – Ex. StudentID: 1, Name: Koji Tanaka, Score: 100, Pass Display in order of StudentID (six students) – Grading “Pass” if score is equal or more than 79, “Fail” otherwise – Hints Apply “For-Next” in array4() and “If-Then-Else” in grade1() Put “If-Then-Else” into “For-Next”

15 Sum of Scores Calculate the sum of score for all students score(0) score(1) score(2)score(3) score(4) score(5) sum=sum+score(i) FALSE TRUE i=0, sum=0 i<=5 i=i+1 sum When i=0 sum =sum+score(0) i i

16 Sum of Scores 100 score(0) 65 score(1) 76 score(2) 87 score(3) 61 score(4) 99 score(5) 0 sum 0i0i i=0 i=1 i=2 0 sum 100 sum 1i1i 100 score(0) 100 sum 165 sum 2i2i 65 score(1)

17 Sum of Scores i=3 i=6 165 sum 241 sum 3i3i 76 score(2) 389 sum 488 sum 6i6i 99 score(1)

18 1Sub sum() 2‘Calculate the sum of score for all student using For - Next 3‘score: array name, i: counter, sum: variable for sum 4 Dim score(5) As Integer 5 Dim i As Integer 6 Dim sum As Integer 7 sum = 0 8 score(0) = 100 9 score(1) = 65 10 score(2) = 76 11 score(3) = 87 12 score(4) = 61 13 score(5) = 99 14 For i = 0 To 5 Step 1 15 xxxxxxxxxxxxxxx 16 Next i 17 MsgBox “Sum of score for ” & i+1 & “students is ” & sum 18 19End Sub

19 1Sub sum() 2‘Calculate the sum of score for all student using For - Next 3‘score: array name, i: counter 4‘sum: variable for sum, ave: variable for average 5 Dim score(5) As Integer 6 Dim i As Integer 7 Dim sum As Integer 8 Dim ave As Single 9 sum = 0 10 ave = 0.0 11 score(0) = 100 12 score(1) = 65 13 score(2) = 76 14 score(3) = 87 15 score(4) = 61 16 score(5) = 99 17 For i = 0 To 5 Step 1 18 xxxxxxxxxx 19 Next i 20 ave = yyyyyyy 21 MsgBox “Sum of score for ” & i+1 & “students is ” & sum 22 MsgBox “Average is ” & ave 23End Sub


Download ppt "情報基礎 A Lecture 9 Takeshi Tokuyama ・ Jinhee Chun Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis."

Similar presentations


Ads by Google