using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Delegate_Assignment
{
public partial class Form1 : Form
{
//Lab Assignment: Due @Thursday Jan 7th, 15
//New Project
//Define a delegate type that takes an array of integers and returns a double
//Define a method ComputeAverage to compute and return the average value of an array of integers
//Define a method ComputeAvgOfOddNegatives to compute and return the average value of all the odd negative values in an array
//Add a button, within the button creates a delegate object of the first method invoke it and display its results
//Re-assignthe delegate object to the second method, Invoke it and display its results
//Define a delegate type that takes an array of integers and returns a double
public delegate double AverageCalculation(int[] arr);
int[] intArray = new int[20];
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
//Define a method ComputeAverage to compute and return the average value of an array of integers
private double ComputeAvg(int [] arr)
{
richTextBox1.Clear();
int sum = 0;
int counter = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
counter++;
}
double avg = sum / counter;
return avg;
}
//Define a method ComputeAvgOfOddNegatives to compute and return the average value of all the odd negative values in an array
private double ComputeAvgOfOddNegatives(int[] arr)
{
richTextBox1.Clear();
int sum = 0;
int counter = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] < 0 && arr[i] % 2 != 0)
{
sum += arr[i];
counter++;
}
}
if (counter > 0)
{
return sum / counter;
}
else
{
return 0;
}
}
private void btnCalculateAvg_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
AverageCalculation simpleAvg = ComputeAvg;
double avg = simpleAvg(intArray);
simpleAvg = ComputeAvgOfOddNegatives;
double oddNegAvg = simpleAvg(intArray);
richTextBox1.Text = String.Format("Average value of array is: {0}", avg);
richTextBox1.Text += String.Format("\nThe Average of Odd Negative Value is: {0}", oddNegAvg);
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < intArray.Length; i++)
{
intArray[i] = rand.Next(-100, 100);
}
}
}
}

No comments:
Post a Comment