Compile-Time Timer Reload

#include <iostream>
using namespace std;

constexpr int timerClockHz = 1000000;

// Define constexpr computeReload here
constexpr int computeReload(int clockHz, int intervalMs) {
    return ( (clockHz * intervalMs + 500) / 1000 );
}
// Define constexpr RELOAD_TABLE here
constexpr int RELOAD_TABLE[] = {computeReload(timerClockHz, 1), computeReload(timerClockHz, 10), computeReload(timerClockHz, 100)};


int main() {
    int idx;
    cin >> idx;

    // Print RELOAD_TABLE[idx]
    cout << RELOAD_TABLE[idx];

    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Expected Output

1000