Solved

Finding the element of an array with the highest value

Posted on 2000-03-09
1
205 Views
Last Modified: 2010-04-02
In other words, suppose you have declared a variable:
float Array[10]
And suppose the elements of the array have been assigned some random numbers.
I am looking for code that can identify which element of the array has the highest value.  That is, in math notation, I need to get a number j between 0 and 9 such that Array[j] is the highest value assumed by any element in the array.
0
Comment
Question by:mm162
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 25 total points
Comment Utility
float Array[MAX_ARRAY];

int nHighestIdx = 0;
float fHighestVal = 0.0;

//...

for ( int i = 0 ; i < MAX_ARRAY; i++)
{
 if ( Array [ i] > fHighestVal)
 {
   fHighestVal = Array [ i];
   nHighestIdx = i;
 }
}

When the loop terminates, 'nHighestIdx' contains the array index of the highest value in the array...
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

6 Experts available now in Live!

Get 1:1 Help Now