Reducing CPU Time For My Program

I'm practicing C++ programs and I can't figure out how to make my program run quickly enough (1 sec or less).

Problem: https://open.kattis.com/problems/oddities
My Attempted Submission: https://open.kattis.com/submissions/3735426

Note: I tried commenting out my assert() related stuff but it didn't make it run in 1 second or under.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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)
        return true;
    // If even, return false
    else
        return false;
}


// 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
  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;
}
Last edited on
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.
Ahhh okay. Thank you! That really helps.
Topic archived. No new replies allowed.