Smallest number in an array

Posted on 2005-04-25
Last Modified: 2012-05-05
Hey everyone, just a small problem, im trying to find the smallest value in an array, but keep getting 0, can someone please help?

#include <iostream>
using namespace std;

void smallest (int x[], int size);

int main()

       int *array;
     int arraySize, i;
      cout << "Please input array size:" << endl;
     cin >> arraySize;

     array = new int[arraySize];

     cout << "Please input the array elements: " << endl;
     for (i = 0; i < arraySize; i++)
          cin >> array[i];

       return 0;

void smallest (int x[], int size)    
   int smallest = 999;  
   for (int i=0; i < size; i++) {
         if (x[i] < smallest ) {
         smallest = i;
   cout << smallest;


Question by:David_4321
    1 Comment
    LVL 8

    Accepted Solution


      The issue is that you're comparing to 9999 instead of the first element, and that you're assigning smallest with the index instead of the value in the array:

    void smallest (int x[], int size)    
       int smallest = x[0];  
       for (int i=0; i < size; i++) {
            if (x[i] < smallest ) {
             smallest = x[i];
       cout << smallest;

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now