Solved

Find lowest in array

Posted on 2007-04-11
2
178 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

624 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