0% found this document useful (0 votes)
23 views4 pages

A

The document contains code snippets that demonstrate different operations on 2D arrays in C#. Specifically: A) Takes user input to populate a 4x4 integer array and prints the array. B) Takes user input for a 4x4 array, finds the maximum element, and prints the array and maximum. V) Takes input for a 4x4 array, calculates the sum of elements above the diagonal, and prints the array and sum. G) Takes input for a 4x4 array and prints the array, then prints the elements of column 0 and column 2.

Uploaded by

elitsa.p.mihova1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

A

The document contains code snippets that demonstrate different operations on 2D arrays in C#. Specifically: A) Takes user input to populate a 4x4 integer array and prints the array. B) Takes user input for a 4x4 array, finds the maximum element, and prints the array and maximum. V) Takes input for a 4x4 array, calculates the sum of elements above the diagonal, and prints the array and sum. G) Takes input for a 4x4 array and prints the array, then prints the elements of column 0 and column 2.

Uploaded by

elitsa.p.mihova1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

A)

Console.WriteLine("Enter the elements of the array: ");


int[,] array = new int[4, 4];
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
array[row, col] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
}
Console.WriteLine("The array looks like: ");
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
Console.Write(array[row, col] + " ");
}
Console.WriteLine();
}

B)
int[,] array = new int[4, 4];
int max = -100000000;
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
array[row, col] = int.Parse(Console.ReadLine());
if (array[row, col] > max)
{
max = array[row, col];
}
}
Console.WriteLine();
}
Console.WriteLine("The array looks like: ");
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
Console.Write(array[row, col] + " ");
}
Console.WriteLine();
}
Console.WriteLine("The biggest element is: " + max);
V)
Console.WriteLine("Hello, World!");
Console.WriteLine("Enter the elements of the array: ");
int[,] array = new int[4, 4];
int sum = 0;
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
array[row, col] = int.Parse(Console.ReadLine());
if (col > row)
{
sum = sum + array[row, col];
}
}
Console.WriteLine();
}
Console.WriteLine("The array looks like: ");
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
Console.Write(array[row, col] + " ");
}
Console.WriteLine();
}
Console.WriteLine("The sum of the elements above the diagonal is: " + sum);
G)
Console.WriteLine("Enter the elements of the array: ");
int[,] array = new int[4, 4];
int sum = 0;
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
array[row, col] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
}
Console.WriteLine("The array looks like: ");
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
Console.Write(array[row, col] + " ");
}
Console.WriteLine();
}
Console.Write("The elements of column 0 are: ");
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
if (col == 0)
{
Console.Write(array[row, col] + " ");
}
}
}
Console.WriteLine();
Console.Write("The elements of column 2 are: ");
for (int row = 0; row < array.GetLength(0); row++)
{
for (int col = 0; col < array.GetLength(1); col++)
{
if (col == 2)
{
Console.Write(array[row, col] + " ");
}

You might also like