情報基礎 a lecture 9 takeshi tokuyama ・ jinhee chun tohoku university graduate school of...

19
情情情情 A Lecture 9 Takeshi Tokuyama Jinhee Chun Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information Systems

Upload: colin-logan

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

情報基礎 A   Lecture 9

Takeshi Tokuyama ・ Jinhee Chun

Tohoku University Graduate School of Information Sciences

System Information SciencesDesign and Analysis of Information Systems

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

PROGRAMMING VBAARRAY, FOR - NEXT

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

Array

• Collection of the same data type– For large amount of data or sequential

reading of data

ab

c d

efIntegers

Array of Integerx(0) x(1) x(2) x(3) x(4) x(5)

IndexName of ArrayElement

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

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 variablesBox 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”

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

Array Declaration

score(0) score(1) score(2) score(3) score(4) score(5)

StudentID: 1Score: 100

StudentID: 2Score: 65

StudentID: 3Score: 76

StudentID: 4Score: 87

StudentID: 5Score: 61

StudentID: 6Score: 99

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

Array Declaration with Index

x(1) x(2) x(3) x(4) x(5) x(6)Six boxes to store Integer type variablesBox name: x, Index: 1 to 6

Dim x(1 to 6) As Integer

Array name Index Range Data type

ElementBox x(1), x(2), … , x(6)

Array name Index

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

Array Declaration1 Sub array1()2 Dim score(5) As Integer3 score(0) = 1004 score(1) = 655 score(2) = 766 score(3) = 877 score(4) = 618 score(5) = 99910 MsgBox score(0)11 MsgBox score(1)12 MsgBox score(2)13 MsgBox score(3)14 MsgBox score(4)15 MsgBox score(5)16 End Sub

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

Array Declaration1 Sub array2()2 Dim score(5) As Integer3 Dim id As Integer ‘variable to put StudentID45 score(0) = 1006 score(1) = 657 score(2) = 768 score(3) = 879 score(4) = 6110 score(5) = 9911 12 id = InputBox(“Enter StudentID(1 to 6) to see the score.”)13 MsgBox “StudentID: ” & id & “, Score: ” & score(id -1)1415 End Sub

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

For - Next

• Execute code repeatedly for defined times– Counter variable to define the range of iteration

• Counter variable: i

Action1FALSE

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 step6 times

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

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

i

i + 1 i

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

i + 1 i

0+1

i

0

i + 1 i

1+1

12

i + 1i

2+1

3 i = i +1

i = i +1

i = i +1

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

1 Sub array3()2 ‘Display StudentID and score of all student using For - Next3 ‘score: array name, i: counter name4 Dim score(5) As Integer5 Dim i As Integer67 score(0) = 1008 score(1) = 659 score(2) = 7610 score(3) = 8711 score(4) = 6112 score(5) = 9913 14 For i = 0 To 5 Step 115 MsgBox “StudentID: ” & id & “, Score: ” & score(i - 1)16 Next i17 End Sub

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

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 122 MsgBox “StudentID: ” & i & “, Name: ” & name(i) & “, Score: ” & score(i)23 Next I24 End Sub

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

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”

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

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+1sum

When i=0sum =sum+score(0)

i

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

Sum of Scores

100score(0)

65score(1)

76score(2)

87score(3)

61score(4)

99score(5)

0sum

0i

i=0

i=1

i=2

0sum

100sum

1i

100score(0)

100sum

165sum

2i

65score(1)

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

Sum of Scores

i=3

i=6

165sum

241sum

3i

76score(2)

389sum

488sum

6i

99score(1)

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

1 Sub sum()2 ‘Calculate the sum of score for all student using For - Next3 ‘score: array name, i: counter, sum: variable for sum4 Dim score(5) As Integer5 Dim i As Integer6 Dim sum As Integer7 sum = 08 score(0) = 1009 score(1) = 6510 score(2) = 7611 score(3) = 8712 score(4) = 6113 score(5) = 9914 For i = 0 To 5 Step 115 xxxxxxxxxxxxxxx

16 Next i17 MsgBox “Sum of score for ” & i+1 & “students is ” & sum1819 End Sub

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

1 Sub sum()2 ‘Calculate the sum of score for all student using For - Next3 ‘score: array name, i: counter4 ‘sum: variable for sum, ave: variable for average5 Dim score(5) As Integer6 Dim i As Integer7 Dim sum As Integer8 Dim ave As Single9 sum = 010 ave = 0.011 score(0) = 10012 score(1) = 6513 score(2) = 7614 score(3) = 8715 score(4) = 6116 score(5) = 9917 For i = 0 To 5 Step 118 xxxxxxxxxx

19 Next i20 ave =  yyyyyyy

21 MsgBox “Sum of score for ” & i+1 & “students is ” & sum22 MsgBox “Average is ” & ave23 End Sub