473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Nov 17 '05 #1
3 1292
I would try the for each approach.

int maxSpeed = 0;
car tempCar;
for each [car currentCar] in [Array enumerator]
if currentCar.brand.equals(@"Ferrari")
{
if ( currentCar.MaxSpeed > 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:eY**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #2
Hi

If you want some already written sorting routines, then you can get them at
my site
http://www.publicjoe.f9.co.uk/csharp/sort00.html

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" <mo**@speedocs.co.il> wrote in message
news:uH*************@TK2MSFTNGP12.phx.gbl...
I would try the for each approach.

int maxSpeed = 0;
car tempCar;
for each [car currentCar] in [Array enumerator]
if currentCar.brand.equals(@"Ferrari")
{
if ( currentCar.MaxSpeed > 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:eY**************@tk2msftngp13.phx.gbl...
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


Nov 17 '05 #3
"Moty Michaely" <mo**@speedocs.co.il> wrote in
news:uH*************@TK2MSFTNGP12.phx.gbl:
int maxSpeed = 0;
car tempCar;
for each [car currentCar] in [Array enumerator]
if currentCar.brand.equals(@"Ferrari")
{
if ( currentCar.MaxSpeed > maxSpeed )
{
tempCar = currentCar;
}
maxSpeed = car.MaxSpeed;
}
next


You might try moving maxSpeed = car.MaxSpeed;
inside the if ( currentCar.MaxSpeed > maxSpeed ).
Otherwise is plain wrong :-)
--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
5
by: junky_fellow | last post by:
How do we calculate the complexity of an algorithm? Am i right if i say the complexity of an algorithm is the number of comparisons done in that algorithm? thanx in advance .......
12
by: No Such Luck | last post by:
Hi All: I'm not sure if this is the right place to ask this question, but I couldn't find a more appropriate group. This is more of a theory question regarding an algorithm implemented in C, not...
2
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
1
by: vermarajeev | last post by:
Hi, EveryBody This question is really interesting one. My question is related to "STL Find Algorithm" Defination Direct from book Now my questions are
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.