How do I find the smallest number in an array

Searching for smallest number in an array

Page 1 of 1

2 Replies - 931 Views - Last Post: 30 October 2013 - 09:21 PM Rate Topic: -----

#1 cathysd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-May 13

How do I find the smallest number in an array

Posted 30 October 2013 - 08:09 PM

I need help trying to find the lowest number in array ?? but I cant figure out what I doing wrong???? or is there a more simple way to go around this without an array?


#include <iostream>
using namespace std;
int lowestNum(int arr[], int size);
int main ()
{
int howmany[100];
int inpnum;
int lowest_Num;
int sum = 0;
cout << "how many numbers would you like to know the sum of? "<< endl;
cin >> howmany[100];
for (int i =0; i < howmany[100]; i++)
{
cout << "Enter an integer: " <<endl;
cin >> inpnum;
lowest_Num = lowestNum(howmany, 100);
sum = sum + inpnum;
}
cout << "The sum is " << sum<< endl;
cout << "the lowest integer entered is " << lowest_Num << endl;



system ("Pause");
return 0;
}

int lowestNum(int arr[], int size)
{
int  lowestNum =arr[size];
for (int i =0; i < arr[size]; i++)
{
if (arr[i] <lowestNum)
{
lowestNum = arr[i];

}
return lowestNum;
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How do I find the smallest number in an array

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: How do I find the smallest number in an array

Posted 30 October 2013 - 08:21 PM

Please use a more descriptive topic title than "Help!". Imagine everyone created topics titled help, it would be impossible for someone to pick a thread to help with, or for someone to search the boards for a similar problem. Topic renamed....
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: How do I find the smallest number in an array

Posted 30 October 2013 - 09:21 PM

Line 11:
cin >> howmany[100];


That is not correct. Maybe something like this?
int howmany = 0;
cin >> howmany;


And what is lowestNum()? If you have sequential (one-after-the-other) input, you can keep track of the lowest value and update it when you come across a lower value. Rough pseudo code.
int lowest = <first value>

while reading input
  calculate new sum

  if input < lowest
    update lowest


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1