0% found this document useful (0 votes)
4 views10 pages

Programming Examples: Search & Count

The document contains multiple programming modules in Visual Basic, including a program for inputting test marks and performing a linear search, a countdown program using loops and recursion, and a vehicle tollgate calculator. Each module demonstrates different programming concepts such as arrays, loops, conditionals, and functions. The programs prompt user input and display results based on the calculations performed.

Uploaded by

kingkid21man
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Programming Examples: Search & Count

The document contains multiple programming modules in Visual Basic, including a program for inputting test marks and performing a linear search, a countdown program using loops and recursion, and a vehicle tollgate calculator. Each module demonstrates different programming concepts such as arrays, loops, conditionals, and functions. The programs prompt user input and display results based on the calculations performed.

Uploaded by

kingkid21man
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SECTION B (PROGRAMMING)

Number 4

Module Program

Sub Main(args As String())

[Link]("===== TEST MARKS ARRAY AND LINEAR SEARCH =====")

[Link]()

Dim test As Integer() = New Integer(4) {}

[Link]("Enter 5 test marks:")

[Link]()

For inputIndex As Integer = 0 To 4

[Link]("Enter mark " & (inputIndex + 1) & ": ")


Dim userInput As String = [Link]()

test(inputIndex) = CInt(userInput)

Next

[Link]()

[Link]("===== TEST MARKS ARRAY =====")

[Link]("The following test marks have been inserted into the array:")

[Link]()

For index As Integer = 0 To [Link] - 1

[Link]("Index " & index & ": " & test(index))

Next

[Link]()

[Link]("===== LINEAR SEARCH =====")

[Link]("Enter the number to search: ")

Dim userSearchInput As String = [Link]()

Dim valueToFind As Integer = CInt(userSearchInput)

Dim foundIndex As Integer = PerformLinearSearch(test, valueToFind)

[Link]()

[Link]("Searching for: " & valueToFind)

[Link]()

If foundIndex >= 0 Then

[Link]("SUCCESS: Value " & valueToFind & " was FOUND at index " &
foundIndex)

Else

[Link]("Value " & valueToFind & " was NOT found in the array")

End If

[Link]()

[Link]("Press any key to exit...")

[Link]()
End Sub

Function PerformLinearSearch(arr As Integer(), target As Integer) As Integer

For i As Integer = 0 To [Link] - 1

If arr(i) = target Then

Return i

End If

Next

Return -1

End Function

End Module

bi)
Module Program

Sub Main()

' Program to count down from 5 to 0 using a loop

[Link]("===== COUNTDOWN PROGRAM =====")

[Link]()

[Link]("Counting down from 5 to 0:")

[Link]()

For count As Integer = 5 To 0 Step -1

[Link]("Current count: " & count)

Next

[Link]()

[Link]("Countdown complete!")

[Link]()

[Link]("Press any key to exit...")

[Link]()

End Sub

End Module
ii)

Sub Main()

[Link](" COUNTDOWN PROGRAM (RECURSIVE) ")

[Link]()

[Link]("Counting down from 5 to 0:")

[Link]()

CountdownRecursive(5)

[Link]()

[Link]("Countdown complete!")

[Link]()

[Link]("Press any key to exit...")

[Link]()

End Sub

Sub CountdownRecursive(number As Integer)

If number < 0 Then

Return

End If
[Link]("Current count: " & number)

CountdownRecursive(number - 1)

End Sub

End Module

c)i)

Initialize totalVehicles to 0

Initialize totalFees to 0

REPEAT

Prompt user to enter vehicle class (A, B, C, D or X to exit)

Read vehicleClass

IF vehicleClass is "X" or "x" THEN

EXIT loop

INCREMENT totalVehicles by 1

SELECT vehicleClass

CASE "A" or "a":

fee = 2

CASE "B" or "b":

fee = 5

CASE "C" or "c":

fee = 10

CASE "D" or "d":

fee = 20

DEFAULT:

Display "Invalid vehicle class"

CONTINUE to next iteration

ADD fee to totalFees

Display "Fee for this vehicle: $" + fee


UNTIL user enters "X" or "x"

Display "Total vehicles: " + totalVehicles

Display "Total fees collected: $" + totalFees


CODE

Module Program

Sub Main()

Dim totalVehicles As Integer = 0

Dim totalFees As Decimal = 0


Dim vehicleClass As String

Dim fee As Decimal

[Link](" VEHICLE tollgate CALC by MUSUNGAPASI")

[Link]()

[Link]("Vehicle Classes:")

[Link](" A - $2")

[Link](" B - $5")

[Link](" C - $10")

[Link](" D - $20")

[Link](" X - Exit")

[Link]()

Do

[Link]("Enter vehicle class (A, B, C, D or X to exit): ")

vehicleClass = [Link]()

If vehicleClass = "X" Or vehicleClass = "x" Then

Exit Do

End If

totalVehicles = totalVehicles + 1

Select Case vehicleClass

Case "A", "a"

fee = 2

Case "B", "b"

fee = 5

Case "C", "c"

fee = 10

Case "D", "d"

fee = 20

Case Else
[Link]("Invalid vehicle class. Please enter A, B, C, or D.")

totalVehicles = totalVehicles - 1

[Link]()

Continue Do

End Select

totalFees = totalFees + fee

[Link]("Fee for this vehicle: $" & fee)

[Link]()

Loop

[Link]()

[Link]("SUMMARY ")

[Link]("Total vehicles: " & totalVehicles)

[Link]("Total fees collected: $" & totalFees)

[Link]()

[Link]("Press any key to exit...")

[Link]()

End Sub

End Module

You might also like