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

+6 votes
asked Feb 28, 2021 by Shumirai Chaipa (180 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

2 Answers

+1 vote
answered Mar 2, 2021 by Peter Minarik (86,040 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.

0 votes
answered Nov 27, 2023 by nasiya (240 points)

#include<stdio.h>

int main(){

    int i,j,n,arr[100],temp;

    printf("Enter limit:");

    scanf("%d",&n);

    printf("Enter values:");

   

        for(i=0;i<n;i++)

        {

            scanf("%d",&arr[i]);

        }

        for(i=0;i<n;i++)

        {

            for(j=i+1;j<n;j++)

            {

                if(arr[i] < arr[j])

                {

                    temp = arr[i];

                    arr[i] = arr[j];

                    arr[j] = temp;

                }

            }

        }

        printf("\nAcsending order array:\n");

        for(i=0;i<n;i++)

        {

            printf("%d\n",arr[i]);

        }

   

}

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.
...