Department of Computer Science CSC 121 Test 3 Total Marks: 50 Duration: 2 Hours
Department of Computer Science CSC 121 Test 3 Total Marks: 50 Duration: 2 Hours
8. Write a program that emulates 50 tosses of a dice and prints out the number of times the number
6 occurs. [4]
Private Function TossDice() As Integer
Return (Int(6 * Rnd()) + 1)
End Function
9. A file called “numbers.txt” contains an unknown number of integers. Write a program that will
read the numbers from the file and print out: [8]
a. All odd numbers.
b. All perfect numbers.
c. The average of all the numbers.
Dim fileReader As IO.StreamReader = IO.File.OpenText("numbers.txt")
Dim sumFactors, count, sum, num, k As Integer
count = 0
sum = 0
While Not fileReader.EndOfStream
num = Val(fileReader.ReadLine)
sumFactors = 0
For k = 1 To num
If num Mod k = 0 Then
sumFactors = sumFactors + k
End If
Next
If sumFactors = 2 * num Then
ListBox2.Items.Add(num)
End If
End While
fileReader.Close()
Console.WriteLine(“{0}{2}{1}”, “Average of all the numbers is ”, sum/count, vbTab)
Elementary Computer Programming
August 26, 2016
10. A delimited file called “employees.txt” contains information about 100 employees of a
company in format: name, employee number, salary, tax. Write a program that will read the
information from the file and print out: [10]
a. The average salary of all the employees.
b. The total number of employees whose tax is more than R5000.
c. The employee names and employee numbers of all the employees who earn more than
R25000.
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(“employees.txt”)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow(3) As String
Dim k, count, emp_num As Integer
Dim salary, tax, sum As single
Dim name As String
Dim fmtStr As String = "{0, 20}{1, 20}"
sum = 0
count = 0
ListBox1.Items.Add(“Employees Who Earn More Than R25000”)
For k = 1 To 100
currentRow = MyReader.ReadFields()
name = currentRow(0)
emp_num = currentRow(1)
salary = currentRow(2)
tax = currentRow(3)
sum = sum + salary
If tax > 5000 Then
count = count + 1
End If
If salary > 25000 Then
ListBox1.Items. Add(String.Format(fmtStr, name, emp_num))
End If
Next
End Using
11. Write a program that generates 500 integers less than 1000, if the integer generated is prime,
the program should write it to a file called “prime.txt”. [6]
Dim k, j, num, count As Integer
Dim fw As IO.StreamWriter
fw = New System.IO.StreamWriter("prime.txt", True)
For k = 1 To 500
num = Int(Rnd()*1000)
count = 0
For j = 1 To num
If num Mod j = 0 Then
count = count + 1
End If
Next
If count = 2 Then
Fw.WriteLine(num)
End If
Next
Fw.Close()
Elementary Computer Programming
August 26, 2016
12. A file “strings.txt” contains 20 Strings. Write a program that will write all palindrome strings
in the file into a file called “pali.txt”. [6]
Dim fileReader As IO.StreamReader = IO.File.OpenText("strings.txt")
Dim str, rev As String
Dim k, j, sum As Integer
Dim fp As IO.StreamWriter
fp = New System.IO.StreamWriter("pali.txt ", True)
For k = 1 To 20
str = fileReader.ReadLine
rev = “”
For j = Len(str) To 1 Step -1
rev = rev + Mid(str, j,1)
Next
If str = rev Then
fp.WriteLine(str)
End If
Next k
fp.Close()