algorithm question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matthias s.

    algorithm question

    hi,

    as a self-taught programmer I've got little clue about coding patterns and
    general algorithms. so I'm asking here hoping to improve my skills:

    I've got a large array of objects, say Car(s) where a Car has got a MaxSpeed
    and Brand property. How would I go most effectively to search the array for
    the car with a Brand equal to 'Ferrari' and the highest MaxSpeed?

    It's not that I couldn't code it, but with your help, I probably could code
    it cleaner/faster.

    Thanks for your time!

    /matthias


  • Moty Michaely

    #2
    Re: algorithm question

    I would try the for each approach.

    int maxSpeed = 0;
    car tempCar;
    for each [car currentCar] in [Array enumerator]
    if currentCar.bran d.equals(@"Ferr ari")
    {
    if ( currentCar.MaxS peed > maxSpeed )
    {
    tempCar = currentCar;
    }
    maxSpeed = car.MaxSpeed;
    }
    next

    tempCar should be your car.

    This is the simple approach.

    If you'd like, try quick sorting the array by car brand and then optimizing
    your itteration. This has a potential of a complex way.

    - Moty -

    "matthias s." <postamt[theat]emvoid[thedot]de> wrote in message
    news:eYK4uRkdFH A.1036@tk2msftn gp13.phx.gbl...[color=blue]
    > hi,
    >
    > as a self-taught programmer I've got little clue about coding patterns and
    > general algorithms. so I'm asking here hoping to improve my skills:
    >
    > I've got a large array of objects, say Car(s) where a Car has got a
    > MaxSpeed
    > and Brand property. How would I go most effectively to search the array
    > for
    > the car with a Brand equal to 'Ferrari' and the highest MaxSpeed?
    >
    > It's not that I couldn't code it, but with your help, I probably could
    > code
    > it cleaner/faster.
    >
    > Thanks for your time!
    >
    > /matthias
    >
    >[/color]


    Comment

    • Publicjoe

      #3
      Re: algorithm question

      Hi

      If you want some already written sorting routines, then you can get them at
      my site


      Hope this helps

      Publicjoe

      C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
      C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html - 71
      Chapters
      VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html - 28
      Chapters
      C++ Ebook at http://www.publicjoe.f9.co.uk/cppt/samples/ebook.html - 8
      Chapters
      Java Ebook at http://www.publicjoe.f9.co.uk/java/samples/ebook.html - 2
      Chapters


      "Moty Michaely" <moty@speedocs. co.il> wrote in message
      news:uHotT7kdFH A.220@TK2MSFTNG P12.phx.gbl...[color=blue]
      > I would try the for each approach.
      >
      > int maxSpeed = 0;
      > car tempCar;
      > for each [car currentCar] in [Array enumerator]
      > if currentCar.bran d.equals(@"Ferr ari")
      > {
      > if ( currentCar.MaxS peed > maxSpeed )
      > {
      > tempCar = currentCar;
      > }
      > maxSpeed = car.MaxSpeed;
      > }
      > next
      >
      > tempCar should be your car.
      >
      > This is the simple approach.
      >
      > If you'd like, try quick sorting the array by car brand and then[/color]
      optimizing[color=blue]
      > your itteration. This has a potential of a complex way.
      >
      > - Moty -
      >
      > "matthias s." <postamt[theat]emvoid[thedot]de> wrote in message
      > news:eYK4uRkdFH A.1036@tk2msftn gp13.phx.gbl...[color=green]
      > > hi,
      > >
      > > as a self-taught programmer I've got little clue about coding patterns[/color][/color]
      and[color=blue][color=green]
      > > general algorithms. so I'm asking here hoping to improve my skills:
      > >
      > > I've got a large array of objects, say Car(s) where a Car has got a
      > > MaxSpeed
      > > and Brand property. How would I go most effectively to search the array
      > > for
      > > the car with a Brand equal to 'Ferrari' and the highest MaxSpeed?
      > >
      > > It's not that I couldn't code it, but with your help, I probably could
      > > code
      > > it cleaner/faster.
      > >
      > > Thanks for your time!
      > >
      > > /matthias
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Mihai N.

        #4
        Re: algorithm question

        "Moty Michaely" <moty@speedocs. co.il> wrote in
        news:uHotT7kdFH A.220@TK2MSFTNG P12.phx.gbl:
        [color=blue]
        > int maxSpeed = 0;
        > car tempCar;
        > for each [car currentCar] in [Array enumerator]
        > if currentCar.bran d.equals(@"Ferr ari")
        > {
        > if ( currentCar.MaxS peed > maxSpeed )
        > {
        > tempCar = currentCar;
        > }
        > maxSpeed = car.MaxSpeed;
        > }
        > next[/color]

        You might try moving maxSpeed = car.MaxSpeed;
        inside the if ( currentCar.MaxS peed > maxSpeed ).
        Otherwise is plain wrong :-)


        --
        Mihai Nita [Microsoft MVP, Windows - SDK]
        ------------------------------------------
        Replace _year_ with _ to get the real email

        Comment

        Working...