MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT
AXBOROT TEXNOLOGIYALARI UNIVERSITETI
“Kampyuter injinering” FAKULTETI PRG-003
GURUH
TALABASI
Ergashev Sardorbek
“Dasturlash
-2
” fanidan bajargan
4-amaliy ishi
O
’qituvchi: Normo’minov Akbar
10
Formada a[N][M] massiv elementlarini [-300; 300] oraliqdagi tasodifiy sonlar
bilan to’ldiring. N va M ni
TextBox
komponetalari yordamida kiriting. Massivni
dataGridWiew
komponentasiga joylashtiring. Massivni chap diagonal
elementlarini kamayish tartibida formaning
label
komponentasida chiqaring.
Natija:
private void buttonGenerate_Click(object sender, EventArgs e)
{
// Parse the values of N and M from the text boxes int
N = int.Parse(textBoxN.Text);
int M = int.Parse(textBoxM.Text);
Random random = new Random();
int[,] array = new int[N, M];
// Fill the array with random numbers in the range
[-300; 300]
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
array[i, j] = random.Next(-300, 301); // 301 is exclusive to
include 300
}
}
dataGridViewArray.DataSource = GetDataTableFromArray(array);
int product = 1;
foreach (int num in array)
{
if (num < 0)
{
product *= Math.Abs(num);
}
}
labelNegativeNumbers.Text = "Product of negative numbers: " +
product;
}
private DataTable GetDataTableFromArray(int[,] array)
{
int rows = array.GetLength(0);
int cols = array.GetLength(1);
DataTable dataTable = new DataTable();
for (int j = 0; j < cols; j++)
{
dataTable.Columns.Add(j.ToString());
}
for (int i = 0; i < rows; i++)
{
DataRow dataRow = dataTable.NewRow();
for (int j = 0; j < cols; j++)
{
dataRow[j] = array[i, j];
}
dataTable.Rows.Add(dataRow);
}
return dataTable;
}
|