Solved

# Find lowest in array

Posted on 2007-04-11
173 Views
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
Question by:-Karamja-

LVL 6

Accepted Solution

_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

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];
IAW C++ standard, names that begin with an underscore are reserved for the implementation.

0

## Featured Post

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++.