Searching Array help!

Can someone help me construct a code that will search this array for max number and region associated with it ?

// Here is what I have so far....
#include <iostream>
#include <iomanip>
using namespace std;


int main ()


{

const int SIZE = 5;
string names[SIZE] = {"North", "South " , "East" , "West", "Central" };
int numOf_autoAcc[5];


for (int count = 0; count < SIZE ; count++)
{

cout << "Please enter the number of accidents in the " << names[count] << " region ";
cin >> numOf_autoAcc[count];
}
// Just making sure that my arrays are holding the proper information
for(int i = 0; i < SIZE; i++) // for loop
{

cout << "For: "<< names[i]<<": " << numOf_autoAcc[i]<<endl;

}

return 0 ;

}
You can either use standard algorithm std::max_element declared in header <algorithm> or write such an algorithm yourself.

For example

1
2
3
size_t n = std::max_element( numOf_autoAcc, numOf_autoAcc + 5 ) - numOf_autoAcc;

std:;cout << "Maximum accidents " << numOf_autoAcc[n] << " were in region " << names[n] << std::endl;

Last edited on
Topic archived. No new replies allowed.