-3

I am trying to write a function to find the smallest value in an array, but i cannot figure out the error i have made. Can someone take a look at it? thanks!!!

void smallest(int array[],int size)
    {
        int smallest=array[0];
            for (int i=1;i<size-1;i++)
                {
                if (array[i]<smallest)

                 smallest=array[i];

                }
        cout<<smallest<<'\n';

     }

the code are not executed because there is breakpoint, and I cannot see where is the problem.

Bruce Cui
  • 35
  • 1
  • 2
  • 2
    Use a debugger to locate the "breakpoint" in your code. Check the values of all involved variables, and make sure they are valid. And *please* try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us, that show us how you use and call this function and with what values. – Some programmer dude Oct 28 '16 at 09:37
  • 1
    `< size-1` surely that should just be `< size` – George Oct 28 '16 at 09:38
  • do you have stack trace – Naresh Hawk Oct 28 '16 at 09:38
  • 1
    also, if the array is zero size, your program will crash – Sven Nilsson Oct 28 '16 at 09:39
  • @SvenNilsson I only know about nullptr but I haven't heard about array of zero size, is it the new feature? – Danh Oct 28 '16 at 09:56
  • @Danh: It's not new feature. By standard those are not allowed, but there is a special case when it is useful and therefore it can be implemented (but definetely shouldn't except for this one case). There is plenty information about that [here](http://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size-array-in-c-c) – Tomasz Plaskota Oct 28 '16 at 10:24
  • One liner -- `#include ... std::cout << *std::min_element(array, array + size) << '\n'`; – PaulMcKenzie Oct 28 '16 at 10:25
  • @TomaszPlaskota Even if it's a flexible array member, its actual size will not be zero, from standard: `If the expression is a constant expression, it shall have a value greater than zero.` and for VLA or FAM: `the behavior is undefined if the expression's value is less than or equal to zero`. In this question, the function parameter is indeed a pointer, not an array. – Danh Oct 28 '16 at 10:29
  • Possible duplicate of [Why is it faster to process a sorted array than an unsorted array?](http://stackoverflow.com/questions/11227809/why-is-it-faster-to-process-a-sorted-array-than-an-unsorted-array) – Zeev Katz Oct 28 '16 at 11:07

2 Answers2

0

You can find a more efficient solution here:
Finding smallest value in an array most efficiently.

Code snippet:

int smallest = array[0];
for (int i = 0; i < array_length; i++) {
    if (array[i] < smallest) {
        smallest = array[i];
    }
}
Community
  • 1
  • 1
0

Your loop is not going to the last element of your array. You should remove the minus 1 of the condition. And do not hesitate to the test the value of the received arguments:

void smallest(int array[], int size)
{
    if (size <= 0 || !array)
        return;
    int smallest = array[0];
    for (int i = 1; i < size ; i++)
        if (array[i] < smallest)
            smallest = array[i];
    cout << smallest << '\n';
}
Bruno V.
  • 19
  • 5