เฉลย Lab 10 Loop
จงเขียนโปรแกรมเพื่อหาค่า Min, Max, Sum, Average ของ N จำนวน โดยเลือกใช้คำสั่งการวนซ้ำที่เหมาะสม Start Read N No N>0 Print No Data Yes Read Score Min = Score Max = Score Sum = Score Count = 1 Count<=N No Average = Sum / N Yes Read Score Print Min Print Max Print Sum Print Average Score < Min No Yes Min = Score Sum = Sum + Score No Score > Max Yes Max = Score Stop
TextBox ชื่อ txtInput PictureBox ชื่อ picOutput CommandButton ชื่อ cmdCompute CommandButton ชื่อ cmdEnd
ประกาศตัวแปร Private Sub cmdCompute_Click() Dim N As Integer Dim score As Double Dim sum As Double Dim count As Double Dim Min As Double Dim Max As Double Dim Average As Double .... End Sub ประกาศตัวแปร
Private Sub cmdCompute_Click() ... If N > 0 Then Else End If .... End Sub Start N = Val(txtInput.Text) Read N No N>0 Print No Data Yes Read Score Min = Score Max = Score Sum = Score Count = 1 Count<=N No Average = Sum / N picOutput.Print “No Data" Yes Read Score Print Min Print Max Print Sum Print Average Score < Min No Yes Min = Score Sum = Sum + Score No Score > Max Yes Max = Score Stop
score = InputBox(“คะแนนของนักเรียนคนที่ 1") Min = score Max = score If N > 0 Then score = InputBox(“คะแนนของนักเรียนคนที่ 1") Min = score Max = score sum = score count = 1 Do While (count <= num) Loop Else picOutput.Print “No Data" End If N>0 Yes Read Score Min = Score Max = Score Sum = Score Count = 1 Count<=N No Yes Read Score Score < Min No Yes Min = Score Sum = Sum + Score No Score > Max Yes Max = Score
Do While (counter <= num) count = count + 1 score = InputBox(“คะแนนของนักเรียนคนที่" & counter) If score < Min Then Min = score Else If score > Max Then Max = score End If sum = sum + score Loop Count<=N No Yes Count = count + 1 Read Score Score < Min No Yes Min = Score Sum = Sum + Score No Score > Max Yes Max = Score
PicOutput.Print " Min = " & Min PicOutput.Print " Max = " & Max Count<=N No Average = Sum / N Average = sum / N PicOutput.Print " Min = " & Min PicOutput.Print " Max = " & Max PicOutput.Print " Sum = " & sum PicOutput.Print " Average = " & Average Print Min Print Max Print Sum Print Average