C++ find smallest integer

chica2012
chica2012 used Ask the Experts™
on
Hi,
I have an assignment that accepts and stores 5 integers in an array. The numbers are output after being entered and then summed. Now I need to figure out how to get the program to return the minimum number. I cannot use min or max because we have to stick to concepts learned in class. Also cannot use a separate function. Only one function, main is used. If there is an easy way to accomplish this, I would appreciate any advice.
#include <math.h>
#include <iostream>	
using namespace std;
 
int main()
{
	
	int number [5];
	int count = 5;
	int sum = 0;
	int i = 0;
 
	for (i=0; i<count; i++) 
	{
		cout << " Enter Value " << i+1 << ": ";
		cin >> number[i];
		cout << endl;
	}
 
	cout << "You Entered: "; 
 
	for (i=0; i<count; i++) 
	{
		cout << " " << number[i];
	}
	
	cout << endl << endl;
 
	cout << "The total is: "; 
	for (int i=0; i<count; i++)
	{
		sum = number[i] + sum;
	}
 
	cout << sum << endl; 
return 0;
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Freelance programmer / Consultant
Commented:
Homework - see EE rules about that.

First set a variable (eg. int smallest) to the value of the first member of the array.  Now you need to loop through all the remaining values and see if any are smaller.  If the value is smaller then set store this in your 'smallest'.  Once the loop is finished you then the smallest value in 'smallest'

Author

Commented:
Thank you for your suggestions. I was able to get my program working. This was the direction I was headed, just didn't think to set the variable.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial