How to find the sum of array elements located between the maximum and minimum elements of an array, not including the maximum and minimum value in C ??
Closed due to the fact that the essence of the question is unclear by the participants of pavel , Vladimir Martyanov , cheops , Harry , Saidolim 4 Sep '16 at 12:27 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Find the minimum element, find the maximum element, summarize the elements between them. What is the problem? - s8am
- Learn to articulate your thoughts. The sum of the maximum and minimum elements of the array, or the sum of the elements between the maximum and minimum elements of the array (inclusive, no)? - Harry
- if I knew how to sum the elements between them I wouldn’t ask a question ( - moks____
- @Harry apologize for the inaccuracy in the question - moks____
2 answers
To find the minimum:
- Create a variable for the minimum value, enter the value of the first cell of the array into it - array [0].
- Create a variable for the index of the minimum value, put in it 0.
- In the cycle from the second to the last cell, perform a comparison of the value of the cell with the variable minimum. If the cell value (array [i], where i is a loop counter) is less than the value of the minimum variable, assign the value of the cell to the minimum, and assign an index to the variable of the current cell i.
Perform the same actions for the maximum with the new variables.
As a result, in the index variables we have the indices of the minimum and maximum values. It remains to set the variable for the sum, initialize to zero and in the loop from one index to another to calculate the sum of the cells: sum + = array [i], where i is the loop counter. Please note that the maximum element index may be less than the minimum index.
UPD: if the sum does not need to include the minimum and maximum values, then the sum should be looked up as follows: for (int i = index1 + 1; i <index2; i ++).
In the loop we iterate over the elements of the array and compare with each other, finding the minimum and maximum.
Walking through the array from the beginning to the end, on the hands we have the index of the minimum element and the index of the maximum.
Now we can start the loop on the new, that would sum up all the elements.
- The sum of the array elements between the minimum and maximum values, not including the max and min values, is moks____