Thursday, April 10, 2014

Array with Method in Window Form

 Q. 1. Create a Dim array size 100. 2. Initialize it with random value (-100, 100). 3. Display it. 4. Get average of Negatives. 5. Get average of odd positives. 6. Add -5 to the positives and +10 to the negatives.




namespace ArrayMethod
{
    public partial class Form1 : Form
    {
        //one dim array declaration
        int[] A = new int[100];

        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }
        //Display Array
        private void Display(int[] array)
        {
            //clear richtextbox
            richTextBox1.Clear();

            for (int i = 0; i < array.Length; i++)
            {
                    richTextBox1.AppendText(array[i] + ", ");
            }
        }
        //2. Initialize it with random value (-100, 100). 
        private void InitializeArray(int[] array)
        {
            richTextBox1.Clear();

            for (int i = 0; i < array.Length; i++)
            {
              array[i] = rand.Next(-100, 100);
            }
        }
        //4. Get average of Negatives.
        private double AvgOfNeg(int[] array)
        {
            richTextBox1.Clear();
            int sum = 0;
            int counter = 0;
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(-100, 100);
                if (array[i] < 0)
                {
                    sum += array[i];
                    counter++;
                }

            }
            double avg = sum / counter;
            return avg;
        }
        //5. Get average of odd positives. 
        private double AvgOfOddPositive(int[] array)
        {
            richTextBox1.Clear();
            int sum = 0;
            int counter = 0;
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(-100, 100);
                if (array[i] % 2 != 0 && array[i] > 0)
                {
                    sum += array[i];
                    counter++;
                }
            }
            double avg = sum / counter;
            return avg;
        }
        //6. Add -5 to the positives and +10 to the negatives 
        private void AddnSubtract(int[] array)
        {
            richTextBox1.Clear();
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(-100, 100);
                if (array[i] > 0)
                {
                    array[i] += -5;
                }
                else if (array[i] < 0)
                {
                    array[i] += 10;
                }
            }
        }
        //=================Buttons=====================
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            InitializeArray(A);
            Display(A);
        }
        //Button for Average of Negatives Values
        private void btnAvgOfNeg_Click(object sender, EventArgs e)
        {
            double AvgOfNegatives = AvgOfNeg(A);
            richTextBox1.Text = AvgOfNegatives.ToString();
        }
        //Button for Average of Odd Positive Values
        private void btnAvgOfOP_Click(object sender, EventArgs e)
        {
            double AvgOfOP = AvgOfOddPositive(A);
            richTextBox1.Text = AvgOfOP.ToString();
        }
        private void btnAddnSub_Click(object sender, EventArgs e)
        {
            AddnSubtract(A);
            Display(A);
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

No comments:

Post a Comment