introduction to computing dr. nadeem a khan. lecture 23
Post on 19-Dec-2015
221 views
TRANSCRIPT
Processing List of DataProcessing List of Data
► File PHONE.TXT contains the following four File PHONE.TXT contains the following four lines:lines:
““Ahmad”, “5884184”Ahmad”, “5884184”
““Aslam”, “5886185”Aslam”, “5886185”
““Bhati”, “5861613”Bhati”, “5861613”
““Jedallah”, “5887164”Jedallah”, “5887164”
=>Write a program to display names and =>Write a program to display names and numbers?snumbers?s
Processing List of Data Processing List of Data (Contd.)(Contd.)► Program 1Program 1
Sub_Command1_ClickSub_Command1_Click
Dim nom As String, phoneNum As StringDim nom As String, phoneNum As String
count%=0count%=0
Open “PHONE.TXT” For Input As #1Open “PHONE.TXT” For Input As #1
Do While count<=4Do While count<=4
Input #1, nom, phoneNumInput #1, nom, phoneNum
Picture1.Print nom, phoneNumPicture1.Print nom, phoneNum
count=count+1count=count+1
LoopLoop
Close #1Close #1
End SubEnd Sub
Processing List of Data Processing List of Data (Contd.)(Contd.)
How to write the same program without How to write the same program without knowing the number of enteries ?knowing the number of enteries ?
Processing List of Data Processing List of Data (Contd.)(Contd.)
► End of File FunctionEnd of File Function
EOF(n)EOF(n)
where n is the reference number where n is the reference number of the of the open fileopen file
Processing List of Data Processing List of Data (Contd.)(Contd.)
► Program 2Program 2
Sub_Command1_ClickSub_Command1_Click
Dim nom As String, phoneNum As StringDim nom As String, phoneNum As String
Open “PHONE.TXT” For Input As #1Open “PHONE.TXT” For Input As #1
Do While Not EOF(1)Do While Not EOF(1)
Input #1, nom, phoneNumInput #1, nom, phoneNum
Picture1.Print nom, phoneNumPicture1.Print nom, phoneNum
LoopLoop
Close #1Close #1
End SubEnd Sub
Counters/AccumulatorsCounters/Accumulators
►What are:What are:
Counters and Accumulators?Counters and Accumulators?
► Identify them in the following Identify them in the following programprogram
Sub Command1_Click ( )Sub Command1_Click ( )
Dim numCoins As Integer, sum!, value!Dim numCoins As Integer, sum!, value!
Open “COINS.TXT” For Input As #1Open “COINS.TXT” For Input As #1
Let numCoins=0Let numCoins=0
Let sum =0Let sum =0
Do While Not EOF(1)Do While Not EOF(1)
Input #1, valueInput #1, value
Let numCoins = numCoins +1Let numCoins = numCoins +1
Let sum = sum + valueLet sum = sum + value
LoopLoop
Picture1.Print numCoins;“Coins of value”;sum; Picture1.Print numCoins;“Coins of value”;sum; “cents”“cents”
Close #1Close #1
End SubEnd Sub
FlagFlag
► Flag: A variable to keep track Flag: A variable to keep track whether a certain event has occurredwhether a certain event has occurred
► How should we realize the following How should we realize the following task?task?
Flags Flags (Contd.)(Contd.)
► File WORDS.TXT contains the following three File WORDS.TXT contains the following three lines:lines:
““cambist”, “croissant”, “deification”cambist”, “croissant”, “deification”
““hydrophyte”, “incisor”, “maculature” hydrophyte”, “incisor”, “maculature”
““macerate” , “narcolepsy”, “shallon”macerate” , “narcolepsy”, “shallon”
=>Task: =>Task:
Count the number of wordsCount the number of words
andand
Report if the words are in alphabetical Report if the words are in alphabetical orderorder
Flags Flags (Contd.)(Contd.)► Solution:Solution:
Sub Command1_Click ( )Sub Command1_Click ( )‘‘First PartFirst PartDim orderFlag%, wordCounter%Dim orderFlag%, wordCounter%Dim word1$, word2$Dim word1$, word2$Let orderFlag=0Let orderFlag=0Let wordCounter=0Let wordCounter=0Let word1= “”Let word1= “”Open “WORDS.TXT” For Input As #1Open “WORDS.TXT” For Input As #1‘‘program continues on next slideprogram continues on next slide
Flags Flags (Contd.)(Contd.)‘‘The second partThe second partDo While Not EOF(1)Do While Not EOF(1)
Input #1, word2Input #1, word2Let wordCounter = wordCounter+1Let wordCounter = wordCounter+1If word1>word2 ThenIf word1>word2 Then
Let orderFlag =1Let orderFlag =1End IfEnd IfLet word1=word2Let word1=word2
LoopLoopClose#1Close#1‘‘Program continues on the next slideProgram continues on the next slide
Flags Flags (Contd.)(Contd.)
‘‘The last partThe last part
Picture1.Print “The number of words is”; Picture1.Print “The number of words is”; WordCounterWordCounter
If orderFlag =0 ThenIf orderFlag =0 Then
Picture1.Print “Wsords are in alphabetical order.”Picture1.Print “Wsords are in alphabetical order.”
ElseElse
Picture1.Print “Words are not in alphabetical Picture1.Print “Words are not in alphabetical order.”order.”
End IfEnd If
‘‘End of programEnd of program
End Sub End Sub
Exit DoExit Do
► Can we not exit the do loop when it is Can we not exit the do loop when it is useless to continue its execution further?useless to continue its execution further?
Exit Do (Contd.)Exit Do (Contd.)
► Exit Do: Exits the Do loop Exit Do: Exits the Do loop
► Modify the previous program using Exit Modify the previous program using Exit DoDo
Exit DoExit Do(Contd.)(Contd.)‘‘The second partThe second partDo While Not EOF(1)Do While Not EOF(1)
Input #1, word2Input #1, word2Let wordCounter = wordCounter+1Let wordCounter = wordCounter+1If word1>word2 ThenIf word1>word2 Then
Let orderFlag =1Let orderFlag =1Do ExitDo Exit ‘exits the do loop if wrong ‘exits the do loop if wrong
orderorderEnd IfEnd IfLet word1=word2Let word1=word2
LoopLoopClose#1Close#1‘‘Program continues on the next slideProgram continues on the next slide
Nested LoopsNested Loops
► Nested loop: Loop inside a loop Nested loop: Loop inside a loop
► What will be printed by the following What will be printed by the following program?program?
Sub Command1_Click ( )Sub Command1_Click ( )Dim num As Integer, counter As IntegerDim num As Integer, counter As IntegerLet counter=1Let counter=1
Do While counter<=4Do While counter<=4 Let num=1Let num=1
Do While num<=10Do While num<=10Picture1.Print num;Picture1.Print num;Let num=num+1Let num=num+1
LoopLoopLet counter=counter+1Let counter=counter+1Picture1.PrintPicture1.PrintLoopLoopEnd Sub End Sub
Nested Loops (Contd.) Nested Loops (Contd.)
►The result:The result:
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
Nested Loops Nested Loops
►The same: The same:
Do Do intCounter=intCounter+1intCounter=intCounter+1
Loop Until intCounter =10Loop Until intCounter =10
Do Do intCounter=intCounter+1intCounter=intCounter+1
Loop While intCounter <10Loop While intCounter <10
Other variants of Do Other variants of Do