document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. \'In the name of Allah
  2. \'SEP 2011 CSC301 Part B Question 1
  3. \'Write a program that will read data from "NEWSPAPER.TXT",
  4. \'which consists of the list of newspaper and total sales.
  5. \'By using the information from "NEWSPAPER.TXT",
  6. \'write the code to calculate the average sale of all newspapers
  7. \'and display the top newspapers (the newspapers with total sales greater than average sales).
  8. \'The result will be displayed if the users click TOP NEWS PAPER button [15 marks]
  9.  
  10. Option Explicit
  11. \'Const textPath = "G:\\Visual Basic 6\\My Programs\\(1) NewsPaper Sep 2011 CSC301 Part D Question 1\\NEWSPAPER.TXT"
  12. Dim textPath As String
  13. Dim strBackSlash As String
  14.  
  15. Private Sub cmdExit_Click()
  16.     End
  17. End Sub
  18.  
  19. Private Sub cmdTopNews_Click()
  20.     txtDisplay.Text = "Average Sales of Newspaper per day = " & averageSale() & vbCrLf & vbCrLf
  21.     txtDisplay.Text = txtDisplay.Text & "Top newspaper (more than average sales):" & vbCrLf & topNews()
  22. End Sub
  23.  
  24. Private Function averageSale() As Double
  25.     \'Open <filename> [For mode] As [#] <filenumber>
  26.    Open textPath For Input As #1
  27.     Dim sum, count, totalSales As Integer
  28.     Dim newspaperName As String
  29.     sum = 0
  30.     count = 0
  31.     Do Until EOF(1)
  32.         Input #1, newspaperName, totalSales
  33.         count = count + 1
  34.         sum = sum + totalSales
  35.     Loop
  36.     averageSale = sum / count
  37.     Close #1
  38. End Function
  39.  
  40. Private Sub Form_Load()
  41.     strBackSlash = IIf(Right$(App.Path, 1) = "\\", "", "\\")
  42.     textPath = App.Path & strBackSlash & "NEWSPAPER.TXT"
  43. End Sub
  44.  
  45. Private Function topNews() As String
  46.     Dim average As Double
  47.     average = averageSale()
  48.     Open textPath For Input As #1
  49.     Dim output, newspaperName As String
  50.     Dim sales As Integer
  51.     Do Until EOF(1)
  52.         Input #1, newspaperName, sales
  53.         If sales > average Then
  54.             output = output & newspaperName & vbCrLf
  55.         End If
  56.     Loop
  57.     topNews = output
  58.     Close #1
  59. End Function
');