using System;
using System.Windows.Forms;
namespace Two_Dimensional_Arrays
{
public partial class Form1 : Form
{
/*Notes: Two-Dimensional Array contains rows and columns
* just like a database table.
* Both rows and columns are zero-based.
* A 4 by 5 (4 x 5) two dim array contains 4 rows and 5 columns
*
* rows varies from 0 to 3
* columns vary from 0 to 4
*
* To dim array defines a property Length,
* which is the total amount of elements or items in the array.
*/
//two-dim array declaration
int[,] A = new int [4,5];
//Array A would have 4 rows and 5 columns
//Notice the comma inside the square brackets
//Declare/Create/Initialize
//int[,] B = new int[3, 4] {{4, -5, 12, -2}, {-9, 15, 19, 6}, {18, -33, -1, 7}};
int[,] B = { { 4, -5, 12, -2 }, { -9, 15, 19, 6 }, { 18, -33, -1, 7 } };
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
//define a method that takes two-dim array of ints and displays it to a richtextbox
private void Display(int[,] array)
{
//clear richtextbox
richTextBox1.Clear();
//get number of rows and column array has.
int rows = array.GetLength(0);
int cols = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
richTextBox1.AppendText(array[i, j] + " ");
}
//next line
richTextBox1.AppendText("\n");
}
}
//method to initialize a two dim with random integers
private void InitializeArray(int[,] array)
{
richTextBox1.Clear();
int rows = array.GetLength(0);
int cols = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[i, j] = rand.Next(-30, 31);
}
}
}
//total of all elements
private double TotalOfElements(int[,] array)
{
int rows = array.GetLength(0);
int cols = array.GetLength(1);
double total = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
total += array[i, j];
}
}
return total;
}
//total of odd elements
private double TotalOfOdd(int[,] array)
{
int rows = array.GetLength(0);
int cols = array.GetLength(1);
double total = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (array[i, j] % 2 != 0)
{
total += array[i, j];
}
}
}
return total;
}
//to return the average of all the even negative elements in the array.
private double AvgOfEvenNeg(int[ , ] array)
{
int rows = array.GetLength(0);
int cols = array.GetLength(1);
double total = 0;
int counter = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (array[i, j] < 0 && array[i, j] % 2 == 0)
{
total += array[i, j];
counter++;
}
}
}
double average = total / counter;
return average;
}
//4. to subtract 10 from all the positive elements and add 15 to all the negative elements.
private void AddnSubtract(int[ , ] array)
{
int rows = array.GetLength(0);
int cols = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (array[i, j] >= 0)
array[i, j] -= 10;
else
array[i, j] += 15;
}
}
}
//====================Buttons===========================
private void btnArrayA_Click(object sender, EventArgs e)
{
Display(A);
}
private void btnArrayB_Click(object sender, EventArgs e)
{
Display(B);
}
private void btnInitialize_Click(object sender, EventArgs e)
{
InitializeArray(A);
Display(A);
}
private void btnTotal_Click(object sender, EventArgs e)
{
richTextBox2.Clear();
double totalOfAllElement = TotalOfElements(B);
richTextBox2.Text = totalOfAllElement.ToString();
}
private void btnTotalOfOdd_Click(object sender, EventArgs e)
{
richTextBox2.Clear();
double totalOfOddValues = TotalOfOdd(B);
richTextBox2.Text = totalOfOddValues.ToString();
}
private void btnAvgOfEN_Click(object sender, EventArgs e)
{
richTextBox2.Clear();
double result = AvgOfEvenNeg(B);
richTextBox2.Text = result.ToString();
}
private void btnAddnSub_Click(object sender, EventArgs e)
{
AddnSubtract(B);
Display(B);
}
}
}
//================More Exercises=======================
//Add methods that take a two dim array
//1. to return the total of the elements in the array.
//2. to return the total of the odd elements in the array.
//3. to return the average of all the even negative elements in the array.
//4. to subtract 10 from all the positive elements and add 15 to all the negative elements.
//5. Add buttons to call these methods.

No comments:
Post a Comment