A
A
Artyom2018-05-29 11:51:52
C++ / C#
Artyom, 2018-05-29 11:51:52

Why if the last element is minimal, then another element becomes minimal?

This function counts the sum of array elements after the minimum, if the minimum is the last element of the array, then this function for some reason takes the previous minimum and calculate the sum after it, and if the function has two identical minimums, then it displays the sum after the first. Please help fix these two

{
            {
                int min = array[0], res = 0, count = 0;
                for (int i = 0; i < array.Length - 1; i++)
                {
                    if (array[i] < min)
                    {
                        min = array[i];                        
                        count = i;
                    }
                }
              
                    for (int j = count + 1; j < array.Length; j++)
                    {
                        res += array[j];
                    

                }
                return res;
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2018-05-29
@danyvasnafig

< array.Length - 1
-1 must be removed
and replaced with <=

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question