I got an assignment on the pros, I don’t understand them at all, but, unfortunately, I don’t have time to figure it out. Can anyone help with this task?

Find the sum of the array elements located after the maximum element.

Closed due to the fact that off-topic participants are Vladimir Martyanov , αλεχολυτ , Kromster , Regent , VAndrJ 19 Jan '17 at 20:00 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Why should someone do tasks for you? - Sublihim
  • I didn’t write that someone would necessarily make it to me or someone MUST do it. I asked for help, maybe someone has free minutes to help me do it. I think these resources work the same way, someone doesn't know something, asks a question, and they help him .... - tweeker
  • four
    This question should be closed, because homework should be done independently. - αλεχολυτ

3 answers 3

Well, let's say, this is the basis of the algorithm.

const int a[] = ...; // Исходный массив. int max = a[0]; // Максимальный элемент. int sum = 0; // Сумма. for (size_t i = 1; i < sizeof(a)/sizeof(a[0]); ++i) { if (a[i] > max) { max = a[i]; sum = 0; } else { sum += a[i]; } } 
  • Thank you for your reply. I did everything as in your example, just added the output of the array and the amount, pause at the end. But swears at the identifier cout What could be the problem? #include <iostream> #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <conio.h> using namespace std; - tweeker
  • @tweeker HZ. With such header files and using should not swear. Maybe a semicolon is missing somewhere or a bracket or something. - user194374
  • Oka, I will look for a mistake - tweeker
  • @tweeker is actually stdfax first line always. - pavel
  • @pavel Oh, right, thank - tweeker

If you write in C ++, you should use the standard algorithms defined in the C ++ language.

In this case, the program may look like this.

 #include <iostream> #include <algorithm> #include <numeric> #include <iterator> #include <cstdlib> #include <ctime> int main() { const size_t N = 20; int a[N]; std::srand((unsigned int)std::time(nullptr)); std::generate(std::begin(a), std::end(a), [=] { return std::rand() % N; }); for (int x : a) std::cout << x << ' '; std::cout << std::endl; int long long sum = std::accumulate( std::next(std::max_element(std::begin(a), std::end(a))), std::end(a), 0ll); std::cout << "sum = " << sum << std::endl; return 0; } 

The output of the program to the console may look like

 8 17 16 11 9 10 5 1 3 17 6 17 3 6 4 12 10 11 1 3 sum = 145 

In this program, the array is filled with random values ​​from 0 to 20, and then it is written in one line how to find the sum of the elements after the maximum element.

If you are not familiar with standard C ++ algorithms, you can use regular cycles instead. For example,

 #include <iostream> #include <cstdlib> #include <ctime> int main() { const size_t N = 20; int a[N]; std::srand((unsigned int)std::time(nullptr)); for ( int &x : a ) x = std::rand() % N; for (int x : a) std::cout << x << ' '; std::cout << std::endl; size_t max_i = 0; for ( size_t i = 1; i < N; i++ ) { if ( a[max_i] < a[i] ) max_i = i; } long long int sum = 0; for ( size_t i = max_i + 1; i < N; i++ ) sum += a[i]; std::cout << "sum = " << sum << std::endl; return 0; } 

In fact, this program is equivalent to the one shown above.

There is one subtlety to which attention should be paid. In your statement of the assignment it does not say what the maximum element should be the sum of the elements: after the first maximum element or after the last maximum element, since there can be several maximum elements in the sequence of numbers.

If you need to find the sum of elements after the last maximum element, then in this case, for example, in the last program with cycles, you should replace the condition in the sentence

 if ( a[max_i] < a[i] ) max_i = i; ^^^ 

on

 if ( a[max_i] <= a[i] ) max_i = i; ^^^^^ 
  • Thank you for your reply. I used your example for review. I am trying to make the console not close immediately after execution. I getch() function. But I swear that the "ID is not found." What is the problem? - tweeker
  • @tweeker It is necessary to include the header where this function is defined. This feature is not standard. You can replace it with the std :: cin.get () clause; - Vlad from Moscow
  • Thank you very much for such a detailed answer. One of these days I will figure out your example :) - tweeker

For example:

 vector<int> z = {1,2,3,10,3,4,5}; cout << accumulate(max_element(z.begin(),z.end())+1,z.end(),0);