cppm

Is this not that way to make an Italian flag .ppm file?


This is my code to create an Italian flag .ppm file:

#include <stdio.h>

int main() {
   int width = 800, height = 600, i, j;

   printf("P6\n");
   printf("%d %d\n", width, height);
   printf("255\n");

   for (i = 0; i < height; i++) {
        for (j = 0; j < width/3; j++) {
            printf("%c%c%c", 0,146,70);
        }
        for (j = 0; j < width/3; j++) {
            printf("%c%c%c", 255, 255, 255);
        }
        for (j = 0; j < width/3; j++) {
            printf("%c%c%c", 206,43,55);
        }
   }
   return 0;
}

I've already made a Poland and Netherlands flag and it worked well. I don't know if it's right?


Solution

  • Your problem is that your width = 800 is not divisible by 3. So you don't fill the whole row with one iteration of the outer loop.

    for (j = 0; j < width/3; j++)
    

    This line stops once j is greater than a third of the width. Let's consider a simple example with width=4. width/3 is somewhere between 1 and 2. So you would color one pixel green, one pixel white, one pixel red. And the fourth? That has to be filled by the next iteration.

    Set your width = 900 for example, and you should get the expected output.