\'In the name of Allah
\'SEP 2011 CSC301 Part B Question 1
\'Write a program that will read data from "NEWSPAPER.TXT",
\'which consists of the list of newspaper and total sales.
\'By using the information from "NEWSPAPER.TXT",
\'write the code to calculate the average sale of all newspapers
\'and display the top newspapers (the newspapers with total sales greater than average sales).
\'The result will be displayed if the users click TOP NEWS PAPER button [15 marks]
Option Explicit
\'Const textPath = "G:\\Visual Basic 6\\My Programs\\(1) NewsPaper Sep 2011 CSC301 Part D Question 1\\NEWSPAPER.TXT"
Dim textPath As String
Dim strBackSlash As String
Private Sub cmdExit_Click()
End
End Sub
Private Sub cmdTopNews_Click()
txtDisplay.Text = "Average Sales of Newspaper per day = " & averageSale() & vbCrLf & vbCrLf
txtDisplay.Text = txtDisplay.Text & "Top newspaper (more than average sales):" & vbCrLf & topNews()
End Sub
Private Function averageSale() As Double
\'Open <filename> [For mode] As [#] <filenumber>
Open textPath For Input As #1
Dim sum, count, totalSales As Integer
Dim newspaperName As String
sum = 0
count = 0
Do Until EOF(1)
Input #1, newspaperName, totalSales
count = count + 1
sum = sum + totalSales
Loop
averageSale = sum / count
Close #1
End Function
Private Sub Form_Load()
strBackSlash = IIf(Right$(App.Path, 1) = "\\", "", "\\")
textPath = App.Path & strBackSlash & "NEWSPAPER.TXT"
End Sub
Private Function topNews() As String
Dim average As Double
average = averageSale()
Open textPath For Input As #1
Dim output, newspaperName As String
Dim sales As Integer
Do Until EOF(1)
Input #1, newspaperName, sales
If sales > average Then
output = output & newspaperName & vbCrLf
End If
Loop
topNews = output
Close #1
End Function