256,713 views
41 votes
41 votes
find the maximum value and minimum value in milestracker. assign the maximum value to maxmiles, and the minimum value to minmiles. ex: if the input is: -10 20 30 40 the output is: min miles: -10 max miles: 40

User Bbjay
by
2.9k points

1 Answer

16 votes
16 votes

#include <bits/stdc++.h>

std::vector<int> v;

int main(int argc, char* argv[]) {

std::cout << "How many items will be in Milestracker?: ";

int idx; std::cin>>idx;

assert(idx>0);

for(int i=0;i<idx;i++) {

int m; std::cin>>m;

v.push_back(m);

}

int maxmiles = *std::max_element(v.begin(),v.end());

int minmiles = *std::min_element(v.begin(),v.end());

if(maxmiles==minmiles) std::cout << "Elements in array are the same each other. So there is no maximum and minimum." << std::endl;

else std::cout << "Max miles: " << maxmiles

<< "\\Min miles: " << minmiles << std::endl;

return 0;

}

User ChristopheLec
by
3.3k points