Hello I'm suppose to write a program using (for loop) that asks the user to enter any amount of numbers, so that it can display the smallest and largest. My program successfully finds the largest, but it is always displaying 0 for the smallest, I think Im doing something wrong with the internalization but I dont know what to change it to, would appreciate any help
this is what I have ....
#include <iostream>
using namespace std;
int main()
{
int amount;
int count;
int number = 0;
int smallest = 0;
int largest = 0;
cout << "Enter total numbers to process: ";
cin >> amount;
for(count = 1; count <= amount; count++)
{
cout << "Enter number: ";
cin >> number;
if(number > largest)
{
largest = number;
}
if(number < smallest)
{
smallest = number;
}
}
cout << "The largest integer is " << largest <<endl;
cout << "The smallest integer is " << smallest <<endl;
got it, thanks! I will use [code] tags from now on
I have switched
line 9: to int number = 999;
although it does the job at displaying the smallest as long as one of the numbers entered has 3 digits or less , I still dont think Im doing it correct because if all the numbers I enter are more than 3 digits it will display the 999 as the smallest. Is there any way that it can be for any number entered?
I was going to say "think about it", but it would be unfair because you don't know the largest number available.
It is provided in <limits>
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
int amount;
int count;
int number = 0;
int smallest = numeric_limits <int> ::max(); // start with largest value possible
int largest = numeric_limits <int> ::min(); // start with smallest value possible