Solved

Find lowest in array

Posted on 2007-04-11
2
173 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

13 Experts available now in Live!

Get 1:1 Help Now