nie dawało mi spokoju co napisał @kq
jeśli użytkownik poda -1 ... -10, to min będzie -10, a max 0
jeśli użytkownik poda 1 ... 10, to min będzie 0, a max 10
faktyczni jest tak, jeżeli ustalimy rozmiar tablicy np. na 6 elementów, wprowadzimy tylko 2 elementy i przerwiemy pętle to będzie tak jak pisze @qk
ale, można zrobić żeby tak nie było
Listing 1
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
constexpr int sizeTab = 6;
int tab[sizeTab]{0}, minValue{0}, maxValue{0}, counter{0};
for(const auto & val: tab ) cout << val << " ";
cout << '\n';
for(auto &val:tab)
{
cout<<"Enter value: ";
cin>>val;
if(cin.good())
{
if(&val==tab) maxValue=minValue=val;
else if(tab[counter] > maxValue)maxValue = tab[counter];
else if(tab[counter] < minValue)minValue = tab[counter];
++counter;
}else break;
}
cout << '\n';
for(int i = 0; i < counter; ++i) cout << tab[i] << " ";
//wyniki jak najbardziej poprawne
cout << '\n';
cout << "minValue: " << minValue << " maxValue " << maxValue << '\n';
//to ponizej pokazuje to o czym pisze @qk
cout << "\n\n";
std::pair<int*, int*> minmax = std::minmax_element(std::begin(tab), std::end(tab));
std::cout << "The min element is " << *(minmax.first) << std::endl;
std::cout << "The max element is " << *(minmax.second) << std::endl;
cout << '\n';
return 0;
}
Listing 2
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int>v(6,0);
int counter{0};
//v.resize(6);
//int val = 0;
//fill(v.begin(), v.end(), val);
cout<<"v.size() = "<<v.size()<<"\n";
for(const auto &val: v)cout<<val<<" ";
cout<<"\n";
for(auto &val: v){
cout<<"Enter val: ";
cin>>val;
if(cin.good())++counter;else break;
}
cout<<"counter " << counter<<"\n";
v.erase(v.begin() + counter, v.end());
//v.shrink_to_fit();
cout<<"\nv.size() = "<<v.size()<<"\n";
cout<<"\n";
for(const auto &val: v)cout<<val<<" ";
cout<<"\n";
auto minmaxValue = std::minmax_element(v.begin(), v.end());
cout<<"minValue "<<*(minmaxValue.first)<<"\n";
cout<<"maxvalue "<<*(minmaxValue.second)<<"\n";
return 0;
}