Highest and lowest values in a two dimension array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geolinc
    New Member
    • Apr 2010
    • 2

    #1

    Highest and lowest values in a two dimension array.

    What code do I use to find the lowest and the highest value in a two dimension array. I have written the code to have a user enter values into the array, as well as getting a total and average of all values in the array. This is the last part of an assignment due on 04/22/2010.

    Thank you!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    The principles are the same as a one dimensional array:
    Go through every element.
    If it's the lowest so far, set it to be the current lowest
    If it's the highest so far, set it to be the current highest
    add the element to your total

    To find the average, average = total / number of elements.

    Comment

    • geolinc
      New Member
      • Apr 2010
      • 2

      #3
      Originally posted by jkmyoung
      The principles are the same as a one dimensional array:
      Go through every element.
      If it's the lowest so far, set it to be the current lowest
      If it's the highest so far, set it to be the current highest
      add the element to your total

      To find the average, average = total / number of elements.
      Thank you for reply. How do I set up the lowest = array(0) in a one dimension to lowest = ? in two dimension array. How do I then set up the nested loops?

      Thank you!

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Usually a 2 dim array is traversed using 2 loops. Think of a 2 dimension array
        as made up of a number of rows of columns. The first loop specifies the row and the second (nested) loop specifies the column. Array{5][6] could be thought of as an array holding 5 rows, each row comprising 6 columns. In such an array the seventh element would reside at array[i][j] where i=1(2nd row) and j=1(2nd col) because the first element`s array address is array[0][0]

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Initially lowest and highest value point to [0][0]th index. Something like this
          Code:
          int lowest = array[0][0];
          int highest = array[0][0];
          After this traverse through 2 loops(basically one for each dimension). And compare the value with lowest and highest. And change the value of highest and lowest accordingly.

          Regards
          Dheeraj Joshi

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Highest and loweset element of a 2D array is not a single int.

            This code:

            Code:
            int array[3][4];
            would require that your find the lowest array of 4 int elements in the 2D array of 3 elements.

            If you are only interested in the lowest single int, then treat this array as an 1D array of 12 ints. You don't need double loops, etc.

            Comment

            Working...