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.
3 Answers
+ 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);
}
0
Намудрил. В строке в тебя хранятся не числа, а символы. Передай их в массив int-ов или вообще изначально его используй
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;
}