I am a beginner in MPI and I have a homework. I am not asking for you to solve it, I only need a hint on why my program is malfunctioning.
Here is the problem
That is one approach. The problem you're encountering is related to output flushing. Using printf will send data to stdout, but stdout doesn't flush immediately. Since each rank will flush at its own time (in this case most likely at the end of execution), you'll get everything at the end, and both ranks will be grouped together. The easiest solution I know which will maintain the original structure of your program is to add fflush(stdout) after each printf call. This forces the buffer to flush which will display the output.
For anyone who might be looking for the answer, i managed to find out that you should let one process handle all the printing.