Task:
Write a C main program which reads in five integers (int) one after the other and numbers (int) and at the end outputs the smallest and the largest of the entered entered numbers. Furthermore it is to be output, at position of the entered numbers these two numbers were entered. were entered. If one of the final "extreme values" should occur more than once in the input the first occurrence is to be noted and output at the end. output. Your program should proceed in such a way that it reads in the input numbers one after the other and numbers one after the other and immediately stores the smallest and the largest of the numbers numbers entered so far in specially provided variables. In the input position of the "extreme values" that have been memorized so far. the input position of the "extreme values" noted so far. The just newly entered number is to be compared with the two "extreme values" stored so far. In this way, your program avoids having to compare all five entered numbers (each with each other) at the end. In my test next week i have to solve a similar task without loop and without arrays only with if and else. I've been sitting here for hours and just don't know how to do it. Please help me
This is how the output should look like:
Please enter the 1st number: ? 2
Please enter the 2nd number: ? 3
Please enter the 3rd number: ? 1
Please enter the 4th number: ? 5
Please enter the 5th number: ? 4
The 3rd number was the smallest of the numbers entered and is: 1
The 4th number was the largest of the numbers entered and is: 5
solution approach
int FirstNumber;
int SecondNumber;
int ThirdNumber;
int FourthNumber;
int FifthNumber;
cout << "Please enter the 1st:";
cin >> FirstNumber;
cout << endl;
cout << "Please enter the 2nd:";
cin >> SecondNumber;
cout << endl;
cout << "Please enter the 3rd:";
cin >> ThirdNumber;
cout << endl;
cout << "Please enter the 4th:";
cin >> fourthnumber;
cout << endl;
cout << "Please enter the 5th:";
cin >> FifthNumber;
cout << endl;
CodePudding user response:
You can add extra functionality in your input statements:
int FirstNumber;
int SecondNumber;
int ThirdNumber;
int FourthNumber;
int FifthNumber;
int max_number;
int min_number;
int position_max;
int position_min;
cout << "Please enter the 1st:";
cin >> FirstNumber;
cout << endl;
min_number = FirstNumber;
max_number = FirstNumber;
position_max = 1;
position_min = 1;
cout << "Please enter the 2nd:";
cin >> SecondNumber;
cout << endl;
if (SecondNumber > max_number)
{
max_number = SecondNumber;
position_max = 2;
}
if (SecondNumber < min_number)
{
min_number = SecondNumber;
position_min = 2;
}
CodePudding user response:
Perhaps you could create a helper function to reduce some of the duplication:
#include <iostream>
#include <climits>
#include <string>
void MaybeUpdateMinMaxBasedOnUserNumber(const std::string& pos,
int *min, std::string *min_pos,
int *max, std::string *max_pos) {
std::cout << "Please enter the " << pos << " number: ";
int num;
std::cin >> num;
if (num < *min) {
*min = num;
*min_pos = pos;
}
if (num > *max) {
*max = num;
*max_pos = pos;
}
}
int main() {
int min = INT_MAX, max = INT_MIN;
std::string min_pos, max_pos;
MaybeUpdateMinMaxBasedOnUserNumber("1st", &min, &min_pos, &max, &max_pos);
MaybeUpdateMinMaxBasedOnUserNumber("2nd", &min, &min_pos, &max, &max_pos);
MaybeUpdateMinMaxBasedOnUserNumber("3rd", &min, &min_pos, &max, &max_pos);
MaybeUpdateMinMaxBasedOnUserNumber("4th", &min, &min_pos, &max, &max_pos);
MaybeUpdateMinMaxBasedOnUserNumber("5th", &min, &min_pos, &max, &max_pos);
std::cout << "The " << min_pos << " number was the smallest of the numbers "
<< "entered and is: " << min << '\n';
std::cout << "The " << max_pos << " number was the largest of the numbers "
<< "entered and is: " << max << '\n';
return 0;
}
Example Usage:
Please enter the 1st number: 2
Please enter the 2nd number: 3
Please enter the 3rd number: 1
Please enter the 4th number: 5
Please enter the 5th number: 4
The 3rd number was the smallest of the numbers entered and is: 1
The 4th number was the largest of the numbers entered and is: 5
CodePudding user response:
#include <iostream>
#include <climits>
using namespace std;
std::string numPostString(int n)
{
std::string s;
if (n == 1) s = "st";
else if (n == 2) s = "nd";
else if (n == 3) s = "rd";
else if (n == 4) s = "th";
else if (n == 5) s = "th";
return s;
}
int main()
{
int FirstNumber;
int SecondNumber;
int ThirdNumber;
int FourthNumber;
int FifthNumber;
int s_num = INT_MAX;
int s_counter = 0;
int l_num = INT_MIN;
int l_counter = 0;
cout << "Please enter the 1st:";
cin >> FirstNumber;
cout << endl;
if (FirstNumber < s_num) {
s_num = FirstNumber;
s_counter = 1;
}
if (FirstNumber > l_num) {
l_num = FirstNumber;
l_counter = 1;
}
cout << "Please enter the 2nd:";
cin >> SecondNumber;
cout << endl;
if (SecondNumber < s_num) {
s_num = SecondNumber;
s_counter = 2;
}
if (SecondNumber > l_num) {
l_num = SecondNumber;
l_counter = 2;
}
cout << "Please enter the 3rd:";
cin >> ThirdNumber;
cout << endl;
if (ThirdNumber < s_num) {
s_num = ThirdNumber;
s_counter = 3;
}
if (ThirdNumber > l_num) {
l_num = ThirdNumber;
l_counter = 3;
}
cout << "Please enter the 4th:";
cin >> FourthNumber;
cout << endl;
if (FourthNumber < s_num) {
s_num = FourthNumber;
s_counter = 4;
}
if (FourthNumber > l_num) {
l_num = FourthNumber;
l_counter = 4;
}
cout << "Please enter the 5th:";
cin >> FifthNumber;
cout << endl;
if (FifthNumber < s_num) {
s_num = FifthNumber;
s_counter = 5;
}
if (FifthNumber > l_num) {
l_num = FifthNumber;
l_counter = 5;
}
std::cout << "the " << s_counter << numPostString(s_counter) << " is the smallest of the numbers you have entered : " << s_num << "\n";
std::cout << "the " << l_counter << numPostString(l_counter) << " is the largest of the numbers you have entered : " << l_num << "\n";
}