Solved

Find lowest in array

Posted on 2007-04-11
2
174 Views
Last Modified: 2010-04-01
Hello all,

I have the following struct

struct _Player
{
      char *Name;
      float Distance;
}

I then have a array,
_Player Player[33];

The information is entered into the array correctly, but I would like to know what would be the best way to return what array index has the lowest Distance (Player[i].Distance).

The information (Distance) is always changing so one player might be the lowest distance then a different player.

I hope you understand,
Alan.
0
Comment
Question by:-Karamja-
2 Comments
 
LVL 6

Accepted Solution

by:
_iskywalker_ earned 55 total points
ID: 18891131
There are 2 ways:
1) to search it once
2) to sort it every time
the 1) is the easiest way:
min=Player[0].Distance;
index=0;
for(i =1; i<33;i++){
if(min>Player[i].Distance){
min=Player[i].Distance
index=i;
}
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 18891174
I recommend you change the Name type to std::string, or a fixed size array.
struct _Player
{
      char Name[99]; //Max len of name is 98
      float Distance;
};


struct _Player
{
      std::string Name; //No max len
      float Distance;
};

>>_Player Player[33];
You should also avoid using names that start with an underscore.  That makes your code non-portable.
IAW C++ standard, names that begin with an underscore are reserved for the implementation.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now