0

Print min and max elements of string!

https://code.sololearn.com/cJ2qYMaTIFPi/?ref=app Can anybody look at my code and correct it so it will work properly? I don't understand scope of elem array, how to properly inicialize it.

29th Apr 2021, 7:03 PM
TeaserCode
6 odpowiedzi
+ 1
std::string line("5 4 7 1 3 2"); std::stringstream lineStream(line); std::vector<int> numbers; while (!lineStream.eof()) { int x; lineStream >> x; numbers.push_back(x); }
29th Apr 2021, 8:58 PM
DRON
DRON - avatar
0
Намудрил. В строке в тебя хранятся не числа, а символы. Передай их в массив int-ов или вообще изначально его используй
29th Apr 2021, 7:11 PM
DRON
DRON - avatar
0
#include <iostream> #include <vector> int minNumber (std::vector<int> &numbers) { bool flag = false; int minNumber; int n = numbers.size(); for (int i = 0; i < n; i++) { if (numbers[i] < minNumber or !flag) { minNumber = numbers[i]; flag = true; } } return minNumber; } int maxNumber (std::vector<int> &numbers) { bool flag = false; int maxNumber; int n = numbers.size(); for (int i = 0; i < n; i++) { if (numbers[i] > maxNumber or !flag) { maxNumber = numbers[i]; flag = true; } } return maxNumber; } int main () { std::vector<int> numbers = {5, 4, 7, 1, 3, 2}; int minN = minNumber(numbers); int maxN = maxNumber(numbers); std::cout << "min number in array = " << minN << "\n"; std::cout << "max number in array = " << maxN << "\n"; return 0; }
29th Apr 2021, 8:18 PM
DRON
DRON - avatar