Lab assignment:
Create a two dimensional arrays with 5 rows and 7 columns. Provide a method and a button to initialize it with random numbers. (also a display method)
Define a method that takes a 2-dim array and returns the sum of all the elements, the sum of only the odd elements and the sum of only the negative elements using the out keyword. Call this method and display its results.
Define an other method that takes a 2 dim array and returns the averages of the odd values, the positive values, and the negative even values. Call this method and display its results.
namespace TwoDimArrayWithMethod_Lab
{
public partial class Form1 : Form
{
int[,] A = new int[5, 7];
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
//Method to initialize and display 2 arrays.
private void Display(int[,] array)
{
txtDisplay.Clear();
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = rand.Next(11, 50);
txtDisplay.AppendText(array[i, j] + " ");
}
txtDisplay.AppendText("\n");
}
}
//Define a method that takes a 2-dim array and returns the sum of all the elements, the sum of only the odd
//elements and the sum of only the negative elements using the out keyword. Call this method and display its results.
private void ProcessSum(int[,] array, out double SumAll, out double OddSum, out double NegSum)
{
SumAll = 0; OddSum = 0; NegSum = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
for(int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = rand.Next(-11, 50);
SumAll += array[i,j];
if (array[i,j] % 2 != 0)
{
OddSum += array[i,j];
}
if (array[i,j] < 0)
{
NegSum += array[i,j];
}
}
}
}
//Define an other method that takes a 2 dim array and returns the averages of the odd values, the positive values,
//and the negative even values. Call this method and display its results.
private void ProcessAverage(int[,] array, out double OddAvg, out double PosAvg, out double NegEvenAvg)
{
OddAvg = 0; PosAvg = 0; NegEvenAvg = 0;
double OddSum = 0;
int numOddSum = 0;
double PosSum = 0;
int numPosSum = 0;
double NegEvenSum = 0;
int numNegEvenSum = 0;
for (int i = 0; i < array.GetLength(0); i++)
{
for(int j = 0; j < array.GetLength(1); j++)
{
array[i,j] = rand.Next(-20, 45);
if (array[i,j] % 2 != 0)
{
OddSum += array[i, j];
numOddSum++;
}
if (array[i, j] > 0)
{
PosSum += array[i, j];
numPosSum++;
}
if (array[i, j] % 2 == 0 && array[i,j] < 0)
{
NegEvenSum += array[i, j];
numNegEvenSum++;
}
OddAvg = OddSum / numOddSum;
PosAvg = PosSum / numPosSum;
NegEvenAvg = NegEvenSum / numNegEvenSum;
}
}
}
//========================Buttons=============================
//Button to display 2 array
private void btnDisplay_Click(object sender, EventArgs e)
{
Display(A);
}
private void btnSum_Click(object sender, EventArgs e)
{
double Sum, OddSum, NegSum;
ProcessSum(A, out Sum, out OddSum, out NegSum);
txtDisplay.Text = "Sum = " + Sum + "\n" + "Odd Values Sum = " + OddSum + "\n" + "Negative Sum = " + NegSum;
}
private void btnAvg_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
double Odd, Positive, NegEven;
ProcessAverage(A, out Odd, out Positive, out NegEven);
txtDisplay.AppendText("Average of Odd = " + Odd.ToString("n2") + "\n" + "Average of Positive = " + Positive.ToString("n2") + "\n" + "Average of Even Negative = " + NegEven.ToString("n2"));
}
}
}

No comments:
Post a Comment