help with multiple arrays

In this function I am trying determine and display the name of the person with the highest percent and the actual number. If two people are tied for the highest, how would I display both percents?

1
2
3
4
5
6
7
8
9
10
11
void FrontRunner(string firstnames[], string lastnames[], int percents[])
{
     int maxIndex=0, largest;
     for(int i=0; i<12; i++)
    {
    if(percents[maxIndex]<percents[i])
       maxIndex=i;
    largest=percents[maxIndex];
}
cout<<"largest"<<largest<<endl;
}
You would need to track the indexes that have the highest value. An example is below, this is untested though as I just knocked it up quickly to demonstrate. It is also probably not the fastest way of doing it but I hope it will put you on the right track.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void FrontRunner(string firstnames[], string lastnames[], int percents[])
{
	std::vector<int> indexes;
	int index = 0, largest = 0;
	for(int i = 0; i < 12; i++)
	{
		if(percents[i] > largest)
		{
			largest = percents[i];
			indexes.clear();
			indexes.push_back(i);
		}
		if(percents[i] = largest)
		{
			indexes.push_back(i);
		}
	}
	
	std::vector<int>::iterator it;
	it = indexes.begin();
	while(it != indexes.end())
	{
	     std::cout << firstnames[*it] << " " << lastnames[*it] << std::endl;
	}
}


Phil.
Last edited on
Thank you. I have a good idea of what I need to do now.
Topic archived. No new replies allowed.