© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
Patak Punjabi Onion Baji

✅ Extracting max number from 2D array?

Code is below
using System;

namespace Coding.Exercise
{
    public class Exercise
    {
        public static int FindMax(int[,] numbers)
        {
            int max = 0;
            var height = numbers.GetLength(0);
            if(height == 0){
                return -1;
            }
            var width = numbers.GetLength(1);
            if(width ==  0){
                return -1;
            }
            for(int i = 0; i < height; i++){
                for(int j = 0; j < width; j++){
                    if(numbers[i,j] < max){
                        max = numbers[i,j];
                    }
                }
            }
            return max;
        }
    }
}
using System;

namespace Coding.Exercise
{
    public class Exercise
    {
        public static int FindMax(int[,] numbers)
        {
            int max = 0;
            var height = numbers.GetLength(0);
            if(height == 0){
                return -1;
            }
            var width = numbers.GetLength(1);
            if(width ==  0){
                return -1;
            }
            for(int i = 0; i < height; i++){
                for(int j = 0; j < width; j++){
                    if(numbers[i,j] < max){
                        max = numbers[i,j];
                    }
                }
            }
            return max;
        }
    }
}

I have no idea why it shows as -5 instead of 12?
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

extracting data from a text file into an array
C#CC# / help
2y ago
❔ Split 2D array into 4 chunks.
C#CC# / help
3y ago
❔ Working w/ 2D Arrays
C#CC# / help
3y ago