### Optimizing finding the largest element in an array

I have written some code that executes fine, but I am looking to further optimize the code with any nuanced methods that I probably haven't learned yet in my classes. I am willing to look up and study any methods mentioned. What I learned from reading this forum is that there are a lot of beginner methods taught in schools that aren't really efficient.
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include int main() { int i, num_arr, looped = 0; float arr[100]; char confirm = 'y'; while (confirm != 'n' && confirm != 'N') { std::cout << "\nEnter total elements (1-100) : "; std::cin >> num_arr; for (i = 0; i < num_arr; i++) { std::cout << "\nEnter number " << i + 1 << " : "; std::cin >> arr[i]; } for (i = 0; i < num_arr; i++) { if (arr[0] < arr[i]) arr[0] = arr[i]; } std::cout << "\nThe greatest value is " << arr[0] << "\n\nContinue? (y/n) "; std::cin >> confirm; looped++; } std::cout << "\nProgram ran " << looped << " time(s) and successfully exited"; return 0; }``````
The preferred way is to use std::max_element: https://cplusplus.com/reference/algorithm/max_element/
Ah, okay. I had no idea such a function existed. That makes things easier. Why aren't students taught this right off the bat?
 Why aren't students taught this right off the bat?

The curriculum is outdated, most instructors want to revisit as much pain on students teaching outdated and antiquated programming methods as they suffered through....

Maybe instructors just don't know what modern C++ (C++11 or later) has to offer.

The debate about what best to teach a without any prior knowledge beginning C++ student rages on.

The <algorithm> library has lots of useful operations available.
https://en.cppreference.com/w/cpp/algorithm

C++ is not the best beginner's programming language, the learning curve is quite high for barest minimum proficiency, but it is one of the more common ones.

Too often some new concept being taught could require an idea that is something to be taught later.

I am someone that believes teaching what the C++ Standard Library has to offer first should be a priority. Learn to use C++ containers such as std::vector instead of starting off with regular arrays, etc.

Later intermediate and advanced courses can delve into how to deal with legacy code, craft a custom container, etc.
 Ah, okay. I had no idea such a function existed. That makes things easier. Why aren't students taught this right off the bat?

That's the £1,000,000 question...

However, to find the max element you find it within the input loop - not as a separate loop. Also, just to find the maximum you don't actually need to store the individual numbers. Consider:

 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include int main() { char confirm { 'y' }; size_t looped {}; while (confirm != 'n' && confirm != 'N') { size_t num_arr {}; double maxnum {std::numeric_limits::min()}; std::cout << "\nEnter number of elements: "; std::cin >> num_arr; for (size_t i {1}; i <= num_arr; ++i) { double num {}; std::cout << "Enter number " << i << ": "; std::cin >> num; if (num > maxnum) maxnum = num; } std::cout << "\nThe greatest value is " << maxnum << "\n\nContinue? (y/n) "; std::cin >> confirm; ++looped; } std::cout << "\nProgram ran " << looped << " time(s) and successfully exited"; }``````

Last edited on
I agree, I have learned a lot from this forum that trumps what I leaned in classes. I would like to think that these courses are trying to teach the students how to understand how the language works on a conceptual level. But there are a lot of things that they could teach us from the start, like how using namespace std; isn't good practice.
well, you learned something in the class as well. The trick is getting past outdated info and 'do it yourself' classroom stuff towards 'what can it do for me' and 'what is the current way'. Using namespace std was highly recommended for a while, until people realized the issues and reversed that.

sure, max element can do it for you, but you hopefully learned how to do it yourself, or at least how to approach it? Its a simple problem, good for a student to get a handle on looping through the data and finding something etc. What if you needed to find the second largest element? Or the median? Can you do those now? For every built in algorithm, there are dozens of nearly identical problems that are not in the toolset. You can either do something weird and usually inefficient to make the built in ones work (eg sort it and take the next to last works, but its slower than doing it yourself) or you can roll one out that does what you need. I consider the algorithms to be handy, ultra commonplace shortcuts to stuff that I really should be able to do myself, and recommend you take them with that mentality. Use them, absolutely, but know how to re-create them too.
Last edited on
Topic archived. No new replies allowed.