0

So in my homework assignment we are supposed to: Make an array with ten integers, sort them to an order of size, and print them.

A former answer told me how to print the array, now I just have to sort it.

I've managed to make and array that generates the numbers, prints them in the console, but I'm having trouble on having them sorted into an order of size.

Here is what I've got done, and now I'm stuck here:

internal class Program
    {
        private static void RandomNumbers()
        {
            int Min = 0;
            int Max = 100;

            int[] ints = new int[10];
            Random randomNum = new Random();

            for (int i = 0; i < ints.Length; i++)
            {
                ints[i] = randomNum.Next(Min, Max);
            }

            for (int i = 0; i < ints.Length; i++)
            {
                Console.WriteLine($"{i}: {ints[i]}");
            }
        }

        static void Main(string[] args)
        {
            RandomNumbers();
        }
    }

I haven't found a way to sort the randomized numbers by size. It's the last part of the assignment.

  • 1
    https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems – Roman Ryzhiy Dec 09 '22 at 14:00
  • Welcome to SO. Write another `for` loop to loop through and print the values of `ints[i]`. `Console.WriteLine(ints[i]);` will work fine (inside the loop). According to the instructions, you also have a sorting problem to solve. You'll have to do that between creation and printing. – Vic F Dec 09 '22 at 14:02
  • Thanks for updating, but now you need to show your attempt to sort the array at all, even if you can't do it by size. There are features built into .Net to assist this, but for your learning journey you might need to consider this from a first principals approach. Go over the notes or previous classes, courses like this are usually well scaffolded at this stage. – Chris Schaller Dec 17 '22 at 03:24

1 Answers1

-1

To print your numbers before sorting, move the write line to within the loop:

Console.WriteLine($"{i}: {ints[i]}");

Write a separate loop:

for (int i = 0; i < ints.Length; i++)
{
    Console.WriteLine($"{i}: {ints[i]}");
}

Or convert your values to a string so you can write them all to a single line:

Console.WriteLine(string.Join(", " ints));
JonasH
  • 28,608
  • 2
  • 10
  • 23