Problem: i cant find the minimum, it's always zero!why?
#include <iostream>
using namespace std;
int main(){ // input n numbers & show max & min
int n, x, max = x, min = x;
cin>>n;
for(int i = 1; i <= n; i ){
cin>>x;
if (x>max){
max = x;
}else if (x<min){
min = x;
}
}
cout<<"max: "<<max<<endl<<"min: "<<min;
return 0;
}
I try to give n inputs and find the maximum & minimum between them with my codes.
CodePudding user response:
You need to choose an initial value for min
and max
. You choose x
but x
is not initialized, it has an indeterminate value. Using x
to initialize min
and max
leads to undefined behavior.
You can use the first element as initial value for min
and max
. However, rather than fixing your mistake you can avoid it completely: Let someone else find min
and max
for you.
There is std::minmax_element
that given two iterators to being and end of a range returns a pair of iterator to the minimum element and iterator to maximum element. The iterators you pass to std::minmax_element
do not have to be iterators to a container, they can be std::istream_iterator
s that read from std::cin
on the fly.
#include <iostream>
#include <algorithm>
#include <iterator>
int main() {
auto res = std::minmax_element(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>());
std::cout << *(res.first) << " " << *(res.second) ;
}
It takes a bit to understand the code. I hope the links above help with that. But once you know what the code does it is as simple as it can get. There is almost no possibility to get something wrong. Well of course there are some, but for example it is just not possible to have wrong initial values for min
or max
in this code.
CodePudding user response:
Using std::vector, std::sort/std::minmax_element your code would look like this (also no index based loops):
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
std::size_t number_of_numbers;
std::cout << "How many numbers : ";
std::cin >> number_of_numbers;
// TODO check in put != 0 or 1;
std::vector<int> values(number_of_numbers);
// range based for will not go out of bounds
for (auto& value : values) // reference to value in array so it can be changed
{
std::cout << "Input number : ";
std::cin >> value;
}
// using min element
auto pair_t = std::minmax_element(values.begin(), values.end());
auto min = *pair_t.first;
auto max = *pair_t.second;
std::cout << "min element = " << min << "\n";
std::cout << "max element = " << max << "\n";
// using sort
std::sort(values.begin(), values.end());
std::cout << "first element in sorted vector : " << values.front() << "\n";
std::cout << "last element in sorted vector : " << values.back() << "\n";
return 0;
}
CodePudding user response:
correction:
#include <iostream>
using namespace std;
int main(){ // input n numbers & show max & min
int n, x, f;
cin>>n;
cin>>x;
f = x;
int max = f, min = f;
for(int i = 1; i < n; i ){
cin>>x;
if (x>max){
max = x;
}else if (x<min){
min = x;
}
}
cout<<"max: "<<max<<endl<<"min: "<<min;
return 0;
}