#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int number, large = 0, small = 0;
cout << "How many numbers do you want to enter" << endl;
cin >> number;
for (int i = 0; i < number; i++)
{
cout << "Enter a number" << endl;
cin >> large >> small;
}
cout << "The largest number was " << large << endl;
cout << "The smallest number was " << small << endl;
return 0;
}
***Edit**
The output program should look like this:
How many numbers do you want to enter
5
Enter a number
3
Enter a number
7
Enter a number
1
Enter a number
12
Enter a number
55
The largest number was 55
The smallest number was 1
Press any key to continue . . .
#include <iostream>
#include <limits>
int main()
{
int number = 0;
// lowest possible number a 32-bit signed int can hold
int max_number = -2147483648;
// largest possible it can hold
int min_number = 2147483647;
std::cout << "How many numbers to test?" << std::endl;
std::cin >> number;
// while the input is incorrect, we try again.
// if its correct the next part is skipped
while((!std::cin) || (number < 1))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid entry... try again." << std::endl;
std::cin >> number;
}
// clean up input stream errors
std::cin.clear();
std::cout << "Please enter " << number << " numbers, each one proceeded by the the enter key..." << std::endl;
// temporary variable to store input in until we can test it against
// min and max numbers
int tmp;
for(int i=0;i<number;++i)
{
std::cin >> tmp;
// error check like before
while(!std::cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid entry... try again." << std::endl;
std::cin >> tmp;
}
// we must test if input is greater than max_number
// max_number starts out at the lowest possible value it can hold
// so no matter what, any tested numbers will put it to a sane value
if(max_number < tmp)
max_number = tmp;
// same thing for min_number, just the other way around
if(min_number > tmp)
min_number = tmp;
}
std::cout << "Largest entered number was: " << max_number
<< "\nSmallest entered number was: " << min_number << std::endl;
return 0;
}
@Hengry How would I fix my code with variables you gave. Sorry, I'm pretty new and don't quite understand c++ yet.
@Hydranix Your code worked completely fine; however, I think that it's too complicated for me. I don't even understand some of those. I appreciate the work, though. Is there like a simpler way of doing it?
#include <iostream>
usingnamespace std;
int main()
{
int totalNumber, large = 0, small = 0, number;
cout << "How many numbers do you want to enter" << endl;
cin >> totalNumber;
for (int i = 0; i < totalNumber; i++)
{
cout << "Enter a number" << endl;
cin >> number;
if (small == 0)
{
small = number;
}
if (number < small)
{
small = number;
}
if (number > large)
{
large = number;
}
}
cout << "The largest number was " << large << endl;
cout << "The smallest number was " << small << endl;
return 0;
}
I just added a couple of extra lines to your code, changed the first variable to totalNumber (it made more sense to me) then added 'number' as the current input number. I added some If statements to check to see if the number was larger or smaller than the one before and replace if so.
It works, however I'm three days into my C++ journey, so I imagine there is a much better and more structured way of doing it.
I guess an array could have been set up, all the numbers put in then the array iterated to check for the largest and smallest but it seemed a bit cumbersome.
#include <iostream>
#include <string>
#include <limits> // special library that includes the numeric_limits functions you will see below
usingnamespace std;
int main()
{
int quantity = 0; // The quantity of numbers you want to enter is initialized with "0"
int number = 0; // number is initialized with "0"
int large = numeric_limits <int>::min(); // large gets the smallest integer value
int small = numeric_limits <int>::max(); // small gets the largest integer
cout << "How many numbers do you want to enter" << endl;
cin >> quantity;
for (int i = 0; i < quantity; i++)
{
cout << "Enter a number" << endl;
cin >> number;
if (number<=small) // if the number is smaller than the "smallest" number...
small = number;
if (number>=large)
large = number; // if the number is greater than the "largest" number...
}
cout << "The largest number was " << large << endl;
cout << "The smallest number was " << small << endl;
return 0;
}
@ufrnkiddo: I think your method is a little extreme. The first time you set small and large can be immediately after reading the first number and you can set them to that first number. How do you know it's the first number? Test for ( i == 0 ) inside the loop, immediately after reading the number.
Just an opinion, I'd consider ufrnkiddo's approach quite reasonable. Checking for the first loop iteration is also ok, I don't have a strong preference between the two, though adding an extra if statement, somehow seems less aesthetically satisfying, even if the effect on efficiency is unimportant.