Answer the question
In order to leave comments, you need to log in
Why are unintelligible numbers displayed?
Hello. Solving the problem, I came across the fact that instead of the numbers 1 and 2, the program displays some kind of garbage. What could be the problem?
#include <iostream>
#include <string>
#include <vector>
void change_status(int i, std::vector<bool>& people) {
if (i > people.size()) {
people.resize(i + 1);
}
if (i > 0) {
people[i] = true;
}
}
void come(int k, std::vector<bool>& people) {
people.resize(people.size() + k + 1);
}
void worry_count(const std::vector<std::string>& commands, const std::vector<int>& values, std::vector<bool>& people) {
int counter;
for (bool person : people) {
if (person) {
++counter;
}
}
std::cout << counter << std::endl;
}
int main(void) {
std::vector<std::string> commands = {"COME", "WORRY", "WORRY", "COME", "WORRY_COUNT", "COME", "WORRY", "WORRY_COUNT"};
std::vector<int> values = {5, 1, 4, -2, 0, 3, 3, 0};
std::vector<bool> people; // true - беспокоящийся человек, false - спокойный
int i = 0;
for (std::string command : commands) {
if (command == "WORRY" || command == "QUIET") {
change_status(i, people);
} else if (command == "COME") {
come(i, people);
} else {
worry_count(commands, values, people);
}
++i;
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
The error is here: int counter;
Initialize it: .int counter = 0;
#include <iostream>
#include <string>
#include <vector>
void change_status(int i, std::vector<bool>& people) {
if (i > people.size()) {
people.resize(i + 1);
}
if (i > 0) {
people[i] = true;
}
}
void come(int k, std::vector<bool>& people) {
people.resize(people.size() + k + 1);
}
void worry_count(const std::vector<std::string>& commands, const std::vector<int>& values, std::vector<bool>& people) {
int counter = 0;
for (bool person : people) {
if (person) {
++counter;
}
}
std::cout << counter << std::endl;
}
int main(void) {
std::vector<std::string> commands = {"COME", "WORRY", "WORRY", "COME", "WORRY_COUNT", "COME", "WORRY", "WORRY_COUNT"};
std::vector<int> values = {5, 1, 4, -2, 0, 3, 3, 0};
std::vector<bool> people; // true - беспокоящийся человек, false - спокойный
int i = 0;
for (std::string command : commands) {
if (command == "WORRY" || command == "QUIET") {
change_status(i, people);
} else if (command == "COME") {
come(i, people);
} else {
worry_count(commands, values, people);
}
++i;
}
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question