19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

224 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

NumberOfRecords = n<br />

End Function<br />

Private Sub ReadData(nom() As String, yearBorn() As Integer, numPeople As _<br />

Integer)<br />

Dim index As Integer<br />

‘Read data from file into arrays<br />

Open “YOB.TXT” For Input As #1<br />

For index = 1 To numPeople Input #1, nom(index), yearBorn(index)<br />

Next index<br />

Close #1<br />

End Sub<br />

Private Sub ShowData(nom() As String, yearBorn() As Integer, numPeople As _<br />

Integer)<br />

Dim index As Integer<br />

‘Display the sorted list<br />

picShowData.Cls<br />

For index = 1 To numPeople<br />

picShowData.Print nom(index), yearBorn(index)<br />

Next index<br />

End Sub<br />

Private Sub SortData(nom() As String, yearBorn() As Integer, numPeople As<br />

Integer)<br />

Dim passNum As Integer, index As Integer<br />

‘Bubble sort arrays by year of birth<br />

For passNum = 1 To numPeople - 1<br />

For index = 1 To numPeople - passNum<br />

If yearBorn(index) > yearBorn(index + 1) Then<br />

Call SwapData(nom(), yearBorn(), index)<br />

End If<br />

Next index<br />

Next passNum<br />

End Sub<br />

Private Sub SwapData(nom() As String, yearBorn() As Integer, index As Integer)<br />

Dim stemp As String, ntemp As Integer<br />

‘Swap names <strong>and</strong> years<br />

stemp = nom(index)<br />

nom(index) = nom(index + 1)<br />

nom(index + 1) = stemp<br />

ntemp = yearBorn(index)<br />

yearBorn(index) = yearBorn(index + 1)<br />

yearBorn(index + 1) = ntemp<br />

End Sub<br />

Private Sub WriteData(nom() As String, yearBorn() As Integer, numPeople As<br />

Integer)<br />

Dim index As Integer<br />

‘Write data back into file<br />

Open “YOB.TXT” For Output As #1<br />

For index = 1 To numPeople<br />

Write #1, nom(index), yearBorn(index)<br />

Next index

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!