Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Maximum value of an array

Maximum value of an array

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    crushinghellhammer
    wrote on last edited by
    #1

    What is the quickest way to determine the maximum value in an array. I have a two dimensional array, let's say, SampleArray[17, 127]. For each of the 17 sub-arrays I want to find the maximum of the 127 samples. Thanks for your help.

    H 1 Reply Last reply
    0
    • C crushinghellhammer

      What is the quickest way to determine the maximum value in an array. I have a two dimensional array, let's say, SampleArray[17, 127]. For each of the 17 sub-arrays I want to find the maximum of the 127 samples. Thanks for your help.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Either use Array.Sort with the 2nd dimension of each 1st dimension and grab the last element, or enumerate (or iterate) through each one, compare values. If the next value is higher than the current, store the the next value. Either way, continue. Both are O(n) operations but unless your array is already sorted, there is no faster algorithm.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      C 1 Reply Last reply
      0
      • H Heath Stewart

        Either use Array.Sort with the 2nd dimension of each 1st dimension and grab the last element, or enumerate (or iterate) through each one, compare values. If the next value is higher than the current, store the the next value. Either way, continue. Both are O(n) operations but unless your array is already sorted, there is no faster algorithm.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        C Offline
        C Offline
        crushinghellhammer
        wrote on last edited by
        #3

        Hello again, Heath! I used the following code...is this acceptable? for (int i = 0; i < 17; i++) { for (ka = PosCtr; ka < 125; ka++) { MaxVal = 0; MaxVal = Math.Max(MaxVal, AutocorrArray[i, ka]); } PeakValueArray[i] = MaxVal; }

        H 1 Reply Last reply
        0
        • C crushinghellhammer

          Hello again, Heath! I used the following code...is this acceptable? for (int i = 0; i < 17; i++) { for (ka = PosCtr; ka < 125; ka++) { MaxVal = 0; MaxVal = Math.Max(MaxVal, AutocorrArray[i, ka]); } PeakValueArray[i] = MaxVal; }

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Shouldn't 125 be 127? I'm not sure what PosCtr is either, but you could probably just hardcode 0 to start at the beginning of the sub-array. You also shouldn't reset MaxVal each time. Just do something like this:

          for (int i=0; i < 17; i++)
          {
          int max = 0;
          for (int j=0; j < 127; j++)
          max = Math.Max(max, arr[i, j]);
           
          peekValues[i] = max;
          }

          If you reset MaxVal (or max in my example), then the value will always be the max because it's greater than 0. This means that AutocorrArray[i, 126] will be the max value whether or not it is.

          Microsoft MVP, Visual C# My Articles

          C 1 Reply Last reply
          0
          • H Heath Stewart

            Shouldn't 125 be 127? I'm not sure what PosCtr is either, but you could probably just hardcode 0 to start at the beginning of the sub-array. You also shouldn't reset MaxVal each time. Just do something like this:

            for (int i=0; i < 17; i++)
            {
            int max = 0;
            for (int j=0; j < 127; j++)
            max = Math.Max(max, arr[i, j]);
             
            peekValues[i] = max;
            }

            If you reset MaxVal (or max in my example), then the value will always be the max because it's greater than 0. This means that AutocorrArray[i, 126] will be the max value whether or not it is.

            Microsoft MVP, Visual C# My Articles

            C Offline
            C Offline
            crushinghellhammer
            wrote on last edited by
            #5

            PosCtr is just a counter variable that starts at 31. It starts at 31 and not 0 because I'm not looking at the first eighth of an initial record of length 256. When I tried your approach I had the following problem: The max values detected are wrong, and the position is either 122, 123 or 124. When I used my code: Whenever there was a sequence of negative numbers followed by a series of positive numbers, the max value recorded is 0 :mad: What am I doing wrong?

            H 1 Reply Last reply
            0
            • C crushinghellhammer

              PosCtr is just a counter variable that starts at 31. It starts at 31 and not 0 because I'm not looking at the first eighth of an initial record of length 256. When I tried your approach I had the following problem: The max values detected are wrong, and the position is either 122, 123 or 124. When I used my code: Whenever there was a sequence of negative numbers followed by a series of positive numbers, the max value recorded is 0 :mad: What am I doing wrong?

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              The whole point of using Math.Max is to compare the current array value with the previous max value. Whichever one is greater gets stored as the max value and compared to the next value, and so on. My code works under normal circumstances, but it might not work for the constraints you have (like your starting indexes, etc.). You must not reset the max each time, though, otherwise you're comparing the current array value with 0, so the last positive number in your array (or subset - whatever you're comparing) will be stored as the max value. You must remember it as I did in my example so you only store the max. If you want to take negative numbers into account, set the max value initially to Int32.MinValue.

              Microsoft MVP, Visual C# My Articles

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups