Solved

Find lowest in array

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

813 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