Another project on arrays. I need to get the smallest number in an array. Enclosed is code. Your input highly needed!

#include<iostream>
using namespace std;
int smallestnumberindex(const int number[int index])
int main()
{
  int number[10];
  int index;
  int smallestnumber;

  //initialize the array
  for (index = 0; index < 10; index++)
    number[index] = 0;

  //input data
  cout << "Enter 10 integers " << endl;
  for (index = 0; index < 10; index++)
    cin >> number[index];
  // print array
  for (index = 0; index < 10; index++)
    cout << number[index] << "  ";

cout << "The smallest index is " << smallestnumberindex << endl;
}
  int smallestnumberindex(const int number[index])  
    int smallestnumberindex = 0;
    int index;
    for (index = 1; index < 10; index++)
    {
      if number[smallestnumberindex] < number[index]
      smallestnumberindex = index;
      return = smallestnumberindex;
    }
        
  return 0;
}

Recommended Answers

All 2 Replies

in c++,
a. you cannot pass an array to a function, only the address of it's first element. the number of elements has to be passed seperately.
b. the name of a function can not be used as a variable
c. return is a statement, not a variable. you need to use return result ; not return = result ;
d. the expression you are testing in an if statement needs to be within paranthesis ( )
are you a pascal programmer?

#include<iostream>
using namespace std;

int smallestnumberindex( const int number[], int index ) ;

int main()
{
  const int N = 10 ;
  int number[N];
  int index;

  //input data
  cout << "Enter " << N << " integers " << endl;
  for (index = 0; index < N; index++)
    cin >> number[index];
  // print array
  for (index = 0; index < N; index++)
    cout << number[index] << "  ";

  cout << "The smallest index is " << smallestnumberindex( number, N )  << endl;
}

int smallestnumberindex( const int number[], int N )
{
    int smallest = 0;
    int index;
    for (index = 1; index < N; index++ )
    {
      if( number[index] < number[smallest] )
        smallest = index;
    }
    return smallest ;
}

When I input integers, without the 0, the output for the smallest is still zero. I have been going back and forth on this and cannot get it to get the smallest input. The smallest was initialized to zero and that is what is outputting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.