Max element in matrix

This is a discussion on Max element in matrix within the C# Programming forums, part of the General Programming Boards category; Hello I am beginner in programming and I need a little help. In given matrix, I have to find elements ...

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    Max element in matrix

    Hello

    I am beginner in programming and I need a little help. In given matrix, I have to find elements that are maximal for its row and column.

    For example in matrix

    5 7 3 4 8
    4 5 9 4 6
    3 10 6 7 5
    6 4 8 1 9

    program should check first row (5 7 3 4 8) and second column (7 5 10 4) and return 10. After that it should continue search and for e.g second row (4 5 9 4 6) and third column (3 9 6 1) return 9. And so on...

    I know how to find max for whole matrix, but I need to find (multiple) max(es) for every row and column together.

    Here is my code:
    Code:
    
    for (int i = 0; i < n; i++)
     {
         for (int j = 0; j < m; j++)
         {
             if (a[i, j] > max)
             {
                 max = a[i, j];
             }
         }
     }
    listBox1.Items.Add(max);
    
    
    
    I think I am close to solution, but I am not quite sure how to do this exactly

    If there's any question, I'll try to answer

    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So why not just do two for loops (without resetting max in between them)?

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Assuming it's already late, and this is more extravagant than your teacher would expect, this would work:

    Code:
        class Program
        {
            private static void Main(string[] args)
            {
                int[,] matrix = new int[4, 5]
                    {
                        { 5, 7, 3, 4, 8 },
                        { 4, 5, 9, 4, 6 },
                        { 3, 10, 6, 7, 5 },
                        { 6, 4, 8, 1, 9 }
                    };
    
                int[,] maxes = new int[4, 5];
    
                for (int row = 0; row <= matrix.GetUpperBound(0); ++row)
                {
                    for (int column = 0; column <= matrix.GetUpperBound(1); ++column)
                    {
                        maxes[row, column] = matrix.GetMaxValue(row, column);
                    }
                }
    
                for (int row = 0; row <= matrix.GetUpperBound(0); ++row)
                {
                    for (int column = 0; column <= matrix.GetUpperBound(1); ++column)
                        Console.Write("{0,2} ", maxes[row, column]);
    
                    Console.WriteLine();
                }
            }
        }
    
        public static class Extensions
        {
            public static int GetMaxValue(this int[,] matrix, int row, int column)
            {
                int max = int.MinValue;
    
                for (int c = 0; c <= matrix.GetUpperBound(1); ++c)
                    if (max < matrix[row, c])
                        max = matrix[row, c];
    
                for (int r = 0; r <= matrix.GetUpperBound(0); ++r)
                    if (max < matrix[r, column])
                        max = matrix[r, column];
    
                return max;
            }
        }
    Output:
    Code:
     8 10  9  8  9
     9 10  9  9  9
    10 10 10 10 10
     9 10  9  9  9
    If you understand what you're doing, you're not learning anything.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Ok well one could have said:
    For min, set start value to max. If any value is less than start value, set start value to this value and continue.
    For max, set start value to min. If any value is greater than start value, set start value to this value and continue.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    Thank you everyone I finished this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2013, 05:11 AM
  2. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  3. Replies: 1
    Last Post: 02-28-2008, 12:30 PM
  4. Matrix element access
    By nepper271 in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2008, 08:08 AM
  5. add element of matrix-Urgent
    By braddy in forum C Programming
    Replies: 1
    Last Post: 03-03-2006, 11:31 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21