dimanche 14 octobre 2018

C#, finding min and max with a method

I need to write four methods for Button_Click events

  1. One that randomly generates values between 1-100 for all elements in a 15 element array.
  2. One that finds the maximum int and opens a MessageBox to display the returned value
  3. One that finds the minimum int and opens a MessageBox to display the returned value.
  4. One that sorts the array in descending order and opens a MessageBox to display the returned value.

I'm having issues with getting the maximumIntButton Method & minimumIntButton_click method getting the values generated in my randomGenerateButton_Click method. Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        // Click event handler for the goButton control.
        private void goButton_Click(object sender, EventArgs e)
        {
            // Create an int array.
            int[] numbers = { 1, 2, 3 };

            // Display the array in the list box.
            outputListBox.Items.Add("The array's original contents:");
            foreach (int number in numbers)
            {
                outputListBox.Items.Add(number);
            }
            RandomNumberGenerator();
        }

        // Code for random number generation method
        public void RandomNumberGenerator()
        {
            // Create an array to hold the numbers.
            const int SIZE = 15;
            int[] randomNumbers = new int[SIZE];

            // Create a Random object
            Random rand = new Random();   

            // Fill the array with random numbers, in the range
            // of 0 through 99.
            for (int index = 0; index < randomNumbers.Length; index++)
            {
                // Assign a random number between 0-99 to randomNumbers array
                randomNumbers[index] = rand.Next(100);
            }
            // Repeats 15 times
            // Display the current array value in the box
            outputListBox.Items.Add("15 random numbers have been assigned");
        }

        // Click event handler for the exitButton control.
        private void exitButton_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }

        private void findMaxButton_Click(object sender, EventArgs e)
        {               
            int largest = randomNumbers[14];
            for (int i = 0; i < 15; i++)
            {   
                if (randomNumbers[i] > largest)
                {
                    largest = randomNumbers[i];   
                }
            }
            outputListBox.Items.Add("The largest number is: " + largest);    
        }

        private void findMinButton_Click(object sender, EventArgs e)
        {             
            int smallest = randomNumbers[14];
            for (int i = 0; i < 15; i++)
            {   
                if (randomNumbers[i] > smallest)
                {
                    smallest = randomNumbers[i];   
                }
            }
            outputListBox.Items.Add("The smallest number is: " + smallest);
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire