Answer to Question #167985 in Visual Basic for hey

Question #167985

write a code in VB which will input the details in 3 parallel arrays (Item_name, quantity, price). Print the list of 5 items purchased in order of costliest to cheapest


1
Expert's answer
2021-03-02T00:53:10-0500
Module Module1


    Sub Main()
        ' 3 parallel arrays (Item_name, quantity, price). 
        Dim item_name(4) As String
        Dim quantity(4) As Integer
        Dim price(4) As Double
        For i As Integer = 0 To 4
            'get item name from the user
            Console.Write("Enter the item name: ")
            item_name(i) = Console.ReadLine()
            'get item quantity from the user
            Console.Write("Enter the quantity: ")
            quantity(i) = Integer.Parse(Console.ReadLine())
            'get item price from the user
            Console.Write("Enter the price: ")
            price(i) = Double.Parse(Console.ReadLine())
            Console.WriteLine()
        Next
        selectionSort(item_name, quantity, price)


        'Print the list of 5 items purchased in order of costliest to cheapest
        Console.WriteLine("The list of 5 items purchased in order of costliest to cheapest: ")
        For i As Integer = 0 To 4
            Console.WriteLine("The item name: " + item_name(i))
            Console.WriteLine("The quantity: " + quantity(i).ToString())
            Console.WriteLine("The price: " + price(i).ToString("C"))
            Console.WriteLine()
        Next
        Console.ReadLine()
        
    End Sub
    ''' <summary>
    ''' Selection sort. Sort by price
    ''' </summary>
    ''' <param name="item_name"></param>
    ''' <param name="quantity"></param>
    ''' <param name="price"></param>
    ''' <remarks></remarks>
    Sub selectionSort(ByRef item_name() As String, ByRef quantity() As Integer, ByRef price() As Double)
        For i As Integer = 0 To item_name.Length - 1
            Dim minIndex As Integer
            For j As Integer = i + 1 To item_name.Length - 1
                minIndex = i
                If (price(j) < price(minIndex)) Then
                    minIndex = j 'find min value
                End If
            Next
            'Swap item names
            Dim tempItemName As String = item_name(minIndex)
            item_name(minIndex) = item_name(i)
            item_name(i) = tempItemName
            'Swap quantity
            Dim tempQuantity As Integer = quantity(minIndex)
            quantity(minIndex) = quantity(minIndex)
            quantity(i) = tempQuantity
            'Swap prices
            Dim tempPrice As Double = price(minIndex)
            price(minIndex) = price(i)
            price(i) = tempPrice
        Next
    End Sub
 
End Module

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS