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 / C++ / MFC
  4. how to get index of minimum of one array

how to get index of minimum of one array

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasedata-structurestutorialquestion
4 Posts 3 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.
  • G Offline
    G Offline
    gentleguy
    wrote on last edited by
    #1

    dear all how to get index of minimum of one array? the following is my code...but however the outcome is wrong after compiled...actually the error is corrNum. fitness[10] is an 10 elements array,first i would like to calculate minimum value of fitness, and then i need to use its index of this minimum value of array,but result is wrong.anyone can help me? thanks a lot. for (int i=0;i<10;i++) { double minValue = fitness[9]; if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }

    Li Zhiyuan

    Steve EcholsS P 2 Replies Last reply
    0
    • G gentleguy

      dear all how to get index of minimum of one array? the following is my code...but however the outcome is wrong after compiled...actually the error is corrNum. fitness[10] is an 10 elements array,first i would like to calculate minimum value of fitness, and then i need to use its index of this minimum value of array,but result is wrong.anyone can help me? thanks a lot. for (int i=0;i<10;i++) { double minValue = fitness[9]; if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }

      Li Zhiyuan

      Steve EcholsS Offline
      Steve EcholsS Offline
      Steve Echols
      wrote on last edited by
      #2

      Try moving the line "double minValue = fitness[9];" outside of the for (int i...) loop. It's getting assigned to fitness[9] every time through the loop. double minValue = fitness[9]; for (int i=0;i<10;i++) { if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }


      - S 50 cups of coffee and you know it's on!

      • S
        50 cups of coffee and you know it's on!
        Code, follow, or get out of the way.
      G 1 Reply Last reply
      0
      • G gentleguy

        dear all how to get index of minimum of one array? the following is my code...but however the outcome is wrong after compiled...actually the error is corrNum. fitness[10] is an 10 elements array,first i would like to calculate minimum value of fitness, and then i need to use its index of this minimum value of array,but result is wrong.anyone can help me? thanks a lot. for (int i=0;i<10;i++) { double minValue = fitness[9]; if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }

        Li Zhiyuan

        P Offline
        P Offline
        pierre_ribery
        wrote on last edited by
        #3

        #include <algorithm> using namespace std; int main() { double myArray[10] = {2.12,432.12,543.1,0.32,0.65,12.,54.2,542.5,100,200}; double* start = myArray; double* minimumPosition = min_element(myArray, myArray + 10); double minValue = *minimumPosition; int indexOfMinimum = minimumPosition - start; return 0; }

        1 Reply Last reply
        0
        • Steve EcholsS Steve Echols

          Try moving the line "double minValue = fitness[9];" outside of the for (int i...) loop. It's getting assigned to fitness[9] every time through the loop. double minValue = fitness[9]; for (int i=0;i<10;i++) { if (fitness[i] <= minValue) { minValue = fitness[i]; for (int j=0;j<150;j++) { value = h[i][j] - irisIndNew[j]; if (value == 0) corrNum = corrNum + 1; } } }


          - S 50 cups of coffee and you know it's on!

          G Offline
          G Offline
          gentleguy
          wrote on last edited by
          #4

          thanks friend. could you help me to check the following code? why i couldn't assign value of p[gbest_Ind][][] to gbest1[gbest_Ind][][], thanks a lot minValue = fitness[0]; for (int count = 0;count<10;count++) { if (fitness[count] < minValue ) { minValue = fitness[count]; gbest_Ind = count; } } for (int j = 0; j<3; j++) for (int k = 0; k<4; k++) { gbest1[gbest_Ind][j][k]=p[gbest_Ind][j][k]; }

          Li Zhiyuan

          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