#include <iostream>
#include <cassert>
// Additional functions:
//void test();
bool isOdd(int numCheck);
int main() {
while (true) {
// Asks for user input
int numCheck;
std::cin >> numCheck;
// Checks if requirements are met.
if ((numCheck > 0) && (numCheck <= 20)) {
// Automatically tests program.
//test();
// Checks if odd. If it IS odd, it will output true. Else false.
// Then will print info accordingly.
if (isOdd(numCheck) == true)
std::cout << numCheck << " is odd\n";
else
std::cout << numCheck << " is even\n";
}
}
return 0;
}
// Checks if user input is even or odd and returns answer
bool isOdd(int numCheck) {
// If odd, return true
if (numCheck % 2 != 0)
returntrue;
// If even, return false
elsereturnfalse;
}
// Automatic Tests:
/*
void test() {
assert(isOdd(4) == false);
assert(isOdd(5) == true);
assert(isOdd(111) == true);
assert(isOdd(666) == false);
//std::cout << "All test cases passed...\n";
}
*/
Help would be greatly appreciated. Thanks!
UPDATE:
I tried running even more simplified code (no additional functions or assert() tests) but it still isn't fast enough. Here is my 2nd version:
/*
Kattis Problem: Oddities
Version: 2
*/
#include <iostream>
int main() {
while (true) {
// Asks for user input
int numCheck;
std::cin >> numCheck;
// Checks if requirements are met.
if ((numCheck > 0) && (numCheck <= 20)) {
// Checks if odd or even then outputs accordingly.
if (numCheck % 2 != 0)
std::cout << numCheck << " is odd\n";
else
std::cout << numCheck << " is even\n";
}
}
return 0;
}
The problem is not that your program is too slow. The problem is that it's incorrect. More specifically, it doesn't follow the specifications for input handling from the problem statement and it doesn't terminate.
Read the problem statement again. I'll reformat it so it's clearer:
Input
* begins with an integer 1 < n < 20 on a line by itself, indicating the number of test cases that follow.
* Each of the following n lines contain a test case consisting of a single integer -10 < x < 10.
Another thing: when these kinds of problems specify that an input will be in a certain range, that doesn't mean that you have to check that the input is actually in that range. It means that you can assume that the input will be in that range.