Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

may someone please help me know why my program of arranging numbers in an array in ascending order is not working

+4 votes
asked Feb 28 by Shumirai Chaipa (160 points)
Module VBModule
    Sub Main()
    dim arrMarks(0 to 9) as Integer
    dim sum, minimum as Integer
    
    dim p as Integer
    dim ave as decimal
    for i = 0 to 9
    
        Console.WriteLine("Enter the input data into an array")
        arrMarks(i) = Console.Readline()
        sum = sum + arrMarks(i)
        next i
        ave= sum/i
        minimum = arrMarks(0)
        for i = 1 to 9
        if(arrMarks(i)<minimum) then
        minimum=arrMarks(i)
        end if
        next i
        arrMarks(0)= p(0)
        for i = 0 to 9
        for p = 0 to 9
        
        if (arrMarks(i) < minimum) then
        arrMark(i) = p=(p-1)
        end if
        next i
        Console.WriteLine("you entered:"& arrMarks)
        Console.ReadLine()
    End Sub
End Module

1 Answer

+1 vote
answered Mar 2 by Peter Minarik (22,620 points)

You got the for - next loop syntax wrong. Please read how it works here: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-next-statement

I've tried to fix things and it seems to work now.

I'm not sure why you needed the sum and minimum variables.

You can use a different sorting algorithm too. I picked bubble-sorting for its simplicity: in every iteration, you swap adjacent items if the left one is larger; this effectively makes the largest one rise to the top, just like bubbles in a fizzy-drink. :)

And here's the code:

Module VBModule
    Sub Main()
        rem --== Variables ==--
        dim arrMarks(0 to 9) as Integer
        dim i as Integer
        dim p as Integer
        dim tmp as Integer
        
        rem --== Initalise the array ==--
        for i = 0 to 9
            Console.Write("Enter the " & i & ". input data into an array: ")
            arrMarks(i) = Console.Readline()
        next i

        rem --== Bubble-sorting ==--
        for i = 0 to 8
            for p = 0 to 8 - i
                if (arrMarks(p) > arrMarks(p + 1)) then
                    tmp = arrMarks(p)
                    arrMarks(p) = arrMarks(p + 1)
                    arrMarks(p + 1) = tmp
                end if
            next p
        next i
        rem Console.WriteLine("you entered:"& arrMarks)
        
        rem --== Print the result ==--
        Console.WriteLine("The array after sorting:")
        for i = 0 to 9
            Console.WriteLine(i & ". " & arrMarks(i))
        next i
    End Sub
End Module

Disclaimer

I do not work with Visual Basic, so probably the code could have been done better by someone who uses Visual Basic regularly.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...