Rhythm notation converter (task #1) About a year ago, I was given a C++ assignment as a preliminary job-interview test.

The assignment included 4 different tasks, from which I had to choose one.

I actually chose and completed 3 out of 4 tasks, but I did not pass the test.

I was not given any specific rejections, so I would like to get an unbiased opinion.







My answer:

File main.cpp:

// --------------------------------
// Input (command-line):
// - Number of beats per bar
// - Number of notes per beat
// - A list of raw beat times
// --------------------------------
// Output (standard):
// - A list of formatted beat times
// --------------------------------

#include &lt;sstream&gt;
#include &lt;iostream&gt;
using namespace std;

static string Compute(int numOfBeatsPerBar,int numOfNotesPerBeat,double beatTime);

int main(int argc,char* argv[])
{
    if (argc &lt; 3)
    {
        cout&lt;&lt;"Invalid Input";
        return -1;
    }

    int numOfBeatsPerBar  = stoi(argv[1]);
    int numOfNotesPerBeat = stoi(argv[2]);

    for (int i=3; i&lt;argc; i++)
    {
        double beatTime = stod(argv[i]);
        string sampTime = Compute(numOfBeatsPerBar,numOfNotesPerBeat,beatTime);
        cout&lt;&lt;beatTime&lt;&lt;' '&lt;&lt;sampTime&lt;&lt;endl;
    }

    return 0;
}

static string Compute(int numOfBeatsPerBar,int numOfNotesPerBeat,double beatTime)
{
    int barId  = (int)beatTime;
    beatTime   = (beatTime-barId)*numOfBeatsPerBar;
    int beatId = (int)beatTime;
    beatTime   = (beatTime-beatId)*numOfNotesPerBeat;
    int noteId = (int)beatTime;

    ostringstream oss;
    oss&lt;&lt;barId+1&lt;&lt;'.'&lt;&lt;beatId+1&lt;&lt;'.'&lt;&lt;noteId+1;
    if (beatTime &gt; 0)
        oss&lt;&lt;'+';
    return oss.str();
}
I think your condition for putting the '+' isn't right: it should be beatTime-noteId &gt; 0 instead of beatTime &gt; 0.

For the given example there will be no difference in the output, but test it with  e.g. 4 4 1.125 as command line arguments: 1.125 matches a full 16th but your code prints 1.125 2.1.3+ instead of 1.125 2.1.3.