Skip to content
Snippets Groups Projects
Commit 3f347d26 authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

removing the erase hotspot from the vector

parent 1c72e219
Branches jwr-solutions
No related tags found
No related merge requests found
......@@ -25,6 +25,8 @@ std::vector<int> readNumbers(const char* filename) {
// read the numbers in (first line = number of inputs)
std::fscanf( fp, "%d", &nCount);
numbers.reserve(nCount);
for (int i=0; i<nCount; ++i) {
int nextNum = 0;
std::fscanf( fp, "%d", &nextNum);
......@@ -40,13 +42,13 @@ std::vector<int> readNumbers(const char* filename) {
* @param numbers the numbers to calculate
* @return the average of the input values
*/
float calcAverage(std::vector<int> numbers) {
float calcAverage(std::vector<int>& numbers) {
int total = 0;
int size = 0;
while ( !numbers.empty() ) {
total += numbers[0];
numbers.erase( numbers.begin() );
total += numbers.back();
numbers.pop_back();
size++;
}
......@@ -84,7 +86,7 @@ int main(int argc, const char* argv[]) {
}
// calculate and print the average
auto avg = calcAverageFast(numbers);
auto avg = calcAverage(numbers);
std::cout << "the average is: " << avg << std::endl;
return EXIT_SUCCESS;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment