Home > Back-end >  Array with random Generator using methods
Array with random Generator using methods

Time:12-29

Hi I'm struggling with this assignment. When the program starts 4 methods are being executed where the next thing hapens.

  • One array gets filled with 100 random numbers between 1 and 1000 000 (method1)
  • The first listbox gets filled with aal the numbers from the array (method2)
  • The second listbox gets filled with the 5 smalest numbers from the array (method3)
  • The third listbox gets filled with the 5 biggest numbers from the array (method3)

This has to run when the program starts.

So far my array gets filled and the first listbox gets filled to. But only in one and not through different methods. I tried to split it up but without success. And my deadline is near.

Sorry for the rookie mistakes btw.

public partial class RandomArray : Form
{   
// array met onze 100 waarden
        int[] arrRandomNumbers = new int[100];

        // random variabele
        Random randomGenerator = new Random();
        int iRandomNumber = 0;
        
        public RandomArray()
        {
            InitializeComponent();
            AddNumbers();
        }

        private void AddNumbers()
        {
            for (int i = 0; i < arrRandomNumbers.Length; i  )
            {
                iRandomNumber = randomGenerator.Next(1, 1000000);
                arrRandomNumbers[i] = iRandomNumber;
            }

            foreach (var number in arrRandomNumbers)
            {
                lbxOne.Items.Add(number);
            }
        }
}

CodePudding user response:

If I understand, you're trying to populate each list in a different function. This is untested but something like this should work:

using System.Linq;  //Make sure to add Linq access

public partial class RandomArray : Form
{   
    // array met onze 100 waarden
    int[] arrRandomNumbers = new int[100];

    // random variabele
    Random randomGenerator = new Random();
    int iRandomNumber = 0;
    
    public RandomArray()
    {
        Console.WriteLine("Creating new RandomArray Class.");
        AddNumbers();
        PopulateListOne();
        PopulateListTwo();
        PopulateListThree();
    }

    private void AddNumbers()
    {
        for (int i = 0; i < arrRandomNumbers.Length; i  )
        {
            iRandomNumber = randomGenerator.Next(1, 1000000);
            arrRandomNumbers[i] = iRandomNumber;
        }
    }

    private void PopulateListOne() {
        Console.WriteLine("All Numbers:");
        foreach (var number in arrRandomNumbers)
        {
            Console.WriteLine(number.ToString());
            //lbxOne.Items.Add(number);
        }
    }

    private void PopulateListTwo() {
        Console.WriteLine("Top 5 Numbers:");
        var top5 = arrRandomNumbers.OrderByDescending(x => x).Take(5).ToArray();
        foreach (var number in top5)
        {
            Console.WriteLine(number.ToString());
            //lbxTwo.Items.Add(number);
        }
    }

    private void PopulateListThree() {
        Console.WriteLine("Bottom 5 Numbers:");
        var bot5 = arrRandomNumbers.OrderBy(x => x).Take(5).ToArray();
        foreach (var number in bot5)
        {
            Console.WriteLine(number.ToString());
            //lbxThree.Items.Add(number);
        }
    }
}

In a fiddle: fig1

I suggest the following:

  1. create a data model to hold the data (the number lists).

    public class RandomNumberLists
    {
        static readonly Random rng = new Random();
    
        public RandomNumberLists(int count)
        {
            Numbers = new int[count];
            for (int i = 0; i < Numbers.Length; i  )
            {
                Numbers[i] = rng.Next(1, 1000000);
            }
            MinFive = Numbers.OrderBy((x) => x).Take(5).ToArray();
            MaxFive = Numbers.OrderByDescending((x) => x).Take(5).ToArray();
        }
    
        public int[] Numbers { get; }
        public int[] MinFive { get; }
        public int[] MaxFive { get; }
    }
    

    Use the built-in methods of .OrderBy() and OrderByDescending() to sort the list of random numbers from bottom to top and top to bottom. Then extract the first n-th numbers with the .Take(n) method and finally make a copy with the .ToArray().

  2. Use the listbox .DataSource property to set the items in the listbox for display

    public partial class Form1 : Form
    {
        RandomNumberLists numberLists;
    
        public Form1()
        {
            InitializeComponent();
    
            numberLists = new RandomNumberLists(100);
        }
    
        protected override void onl oad(EventArgs e)
        {
            base.OnLoad(e);
    
            listBox1.DataSource = numberLists.Numbers;
            listBox2.DataSource = numberLists.MinFive;
            listBox3.DataSource = numberLists.MaxFive;
        }
    }
    

    Here I initialize the numbers in the form constructor, and assign the lists to the listboxes in the .OnLoad() method which happens before initial display of the form.

CodePudding user response:

In this example, the program generates an array with 100 random numbers between 1 and 1000 000 when the form loads. Then, the Method2, Method3, and Method3 methods are called to fill the three listboxes with the numbers from the array.

The Method2 method simply iterates over the array and adds each value to the listbox.

The Method3 method takes a boolean argument that specifies whether to fill the listbox with the smallest or biggest values from the array. It does this by ordering the array in ascending or descending order, and then taking the first 5 values from the sorted array using the Take method. Finally, it adds the values to the listbox.

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ArrayExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Generate the array with random numbers
            int[] array = Method1();

            // Fill the first listbox with all the numbers from the array
            Method2(array, listBox1);

            // Fill the second listbox with the 5 smallest numbers from the array
            Method3(array, listBox2, true);

            // Fill the third listbox with the 5 biggest numbers from the array
            Method3(array, listBox3, false);
        }

        // Method1: Generate an array with 100 random numbers between 1 and 1000 000
        private int[] Method1()
        {
            Random random = new Random();
            int[] array = new int[100];

            for (int i = 0; i < array.Length; i  )
            {
                array[i] = random.Next(1, 1000001);
            }

            return array;
        }

        // Method2: Fill a listbox with the values from an array
        private void Method2(int[] array, ListBox listBox)
        {
            listBox.Items.Clear();
            foreach (int value in array)
            {
                listBox.Items.Add(value);
            }
        }

        // Method3: Fill a listbox with the smallest or biggest values from an array
        private void Method3(int[] array, ListBox listBox, bool smallest)
        {
            listBox.Items.Clear();

            if (smallest)
            {
                // Order the array in ascending order and take the first 5 values
                var values = array.OrderBy(x => x).Take(5);
                foreach (int value in values)
                {
                    listBox.Items.Add(value);
                }
            }
            else
            {
                // Order the array in descending order and take the first 5 values
                var values = array.OrderByDescending(x => x).Take(5);
                foreach (int value in values)
                {
                    listBox.Items.Add(value);
                }
            }
        }
    }
}
  • Related