Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active April 29, 2025 11:03
Show Gist options
  • Save Strelok78/89829ff5039f7415c658346006a70761 to your computer and use it in GitHub Desktop.
Save Strelok78/89829ff5039f7415c658346006a70761 to your computer and use it in GitHub Desktop.
get max value in two dimensional array and replace with zero
using System.Collections;
using System.Diagnostics;
using System.Text;
namespace iJuniorPractice;
class Program
{
static void Main(string[] args)
{
int[,] numbers =
{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
};
int maxValue = int.MinValue;
int valueReplacer = 0;
int rowDimensionLength = numbers.GetLength(0);
int columnDimensionLength = numbers.GetLength(1);
Console.WriteLine("Original array:");
for (int i = 0; i < rowDimensionLength; i++)
{
for (int j = 0; j < columnDimensionLength; j++)
{
maxValue = Math.Max(maxValue, numbers[i,j]);
Console.Write(numbers[i,j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nUpdated array:");
for (int i = 0; i < rowDimensionLength; i++)
{
for (int j = 0; j < columnDimensionLength; j++)
{
if(maxValue == numbers[i,j])
{
numbers[i,j] = valueReplacer;
}
Console.Write(numbers[i,j] + " ");
}
Console.WriteLine();
}
Console.WriteLine($"\nMax value = {maxValue}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment