1

I've been working on a sorting algorithm and one condition says that the elements in the array should be program generated.

This is the code I've been working on.

        int f;

        Console.WriteLine("Enter how many elements you want to be sorted:");
        f = Convert.ToInt32(Console.ReadLine());

        int[] myArray = new int[f];
        
        int smallest, tmp;

        Console.WriteLine("Unsorted List");
        foreach (int a in myArray)
            Console.Write(a + " ");

       
        for (int i = 0; i < myArray.Length - 1; i++)
        {
            smallest = i;
            for (int j = i + 1; j < myArray.Length; j++)
            {
                if (myArray[j] < myArray[smallest])
                {
                    smallest = j;
                }

                tmp = myArray[smallest];
                myArray[smallest] = myArray[i];
                myArray[i] = tmp;
            }

           
        }
        Console.WriteLine("\nSorted List Using Selection Sort:");
        for (int i=0; i < myArray.Length; i++)
        {
            Console.Write(myArray[i] + " ");

The result of this program is just 0. How can I make the elements in array program generated? Is there a specific code block?

1 Answer 1

2

Please note we should use i < myArray.Length to Iterate the array instead of i < myArray.Length-1.

You can try the following code to add the elements in program generated array.

   static void Main(string[] args)
        {
            int f;
            Console.WriteLine("Enter how many elements you want to be sorted:");
            f = Convert.ToInt32(Console.ReadLine());
            int[] myArray = new int[f];
            int temp = 0;
            int minIndex = 0;
            Console.WriteLine("Unsorted List");
            for (int i = 0; i < myArray.Length; i++)
            {
                myArray[i] = Convert.ToInt32(Console.ReadLine());
            }
            for (int i = 0; i < myArray.Length; i++)
            {
                minIndex = i;
                for (int j = i; j < myArray.Length; j++)
                {
                    if (myArray[j] < myArray[minIndex])
                    {
                        minIndex = j;
                    }
                }
                temp = myArray[minIndex];
                myArray[minIndex] = myArray[i];
                myArray[i] = temp;
            }
            Console.WriteLine("\nSorted List Using Selection Sort:");
            for (int i = 0; i < myArray.Length; i++)
            {
                Console.Write(myArray[i] + " ");
            }

            Console.ReadKey();
        }

Besides, I have modified the related code about Selection Sort, which makes it can produce the correct sort.

Result:

enter image description here

Update for generate the random array:

    int f;
    Console.WriteLine("Enter how many elements you want to be sorted:");
    f = Convert.ToInt32(Console.ReadLine());
    int[] myArray = new int[f];
    Random randNum = new Random();
    for (int i = 0; i < myArray.Length; i++)
    {
        myArray[i] = randNum.Next(1, 100);
    }
    Console.WriteLine("The radnom array is ");
    foreach (var item in myArray)
    {
        Console.WriteLine(item);
    }

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for this, but does c# has a specific code block on how the elements of the array is generated by the program?
@tayblinks live, how do you want to generate the elements by the program? Such as generate an array randomly?
Yes, is that possible?
@tayblinks live, I have updated my code for the random array.
So much thanks for this!!! <3 This will really help me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.