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. How can i get array index to satisfy my requirement fastly?

How can i get array index to satisfy my requirement fastly?

Scheduled Pinned Locked Moved C#
tutorialquestiondatabasealgorithmsdata-structures
2 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.
  • S Offline
    S Offline
    smallkubi
    wrote on last edited by
    #1

    Hi ,i have a questions ,and i don't know how to handle it. I have a double array named array1=new double[100], and each element is function result, like array1[0]=fun(0),array2[1]=fun(1)... each element is positive or negative, and generally speaking, a[0] to a[n] is positive, and a[n+1] to end is negative, so how can i get n ? i need efficient algorithm, like divide-and-conquer method for example... and case 2: if near the end i get wrong result, for example, a[90] is +,how can i get the first negative index n ?

    OriginalGriffO 1 Reply Last reply
    0
    • S smallkubi

      Hi ,i have a questions ,and i don't know how to handle it. I have a double array named array1=new double[100], and each element is function result, like array1[0]=fun(0),array2[1]=fun(1)... each element is positive or negative, and generally speaking, a[0] to a[n] is positive, and a[n+1] to end is negative, so how can i get n ? i need efficient algorithm, like divide-and-conquer method for example... and case 2: if near the end i get wrong result, for example, a[90] is +,how can i get the first negative index n ?

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      In case one, a simple Binary search algorithm - Wikipedia, the free encyclopedia[^] should do it, assumign there is no "bad" data. When you data goes bad, that's different, and it becomes necessary to actually search the whole array - if there is one bad value, then it's likely there are more. But the alternative to both is to to it all while you fill the array - since that is already a time consuming process (compared to a search of the results) adding a little extra checking to find the first negative as you fill the values is trivial, and allows you to cope with the second case as well.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      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