1

I have to create country flags displayed in a ppm file. I have the program below working except when I display the flags the stripes are diagonal instead of vertical for the French flag or horizontal for the other two. I know that it is something with the i < width in the make_flag_row functions, but I really have no idea what would make the stripes horizontal or vertical.

int main(void) {
    int width;
    int country_code;

    fscanf(stdin, "%d %d", &country_code, &width);
    fprintf(stderr, "Making country %d width %d \n", country_code, width);

    make_ppm_image(country_code, width);

    return(0);
}

void make_pixel(int r,int g,int b) {
    fprintf(stdout, "%c%c%c", r, g, b);
}

void make_ppm_header(int width, int height) {
    fprintf(stdout, "P6\n");
    fprintf(stdout, "%d %d %d\n", width, height, 255);
}

void make_ppm_image(int country_code, int width) {
    if(country_code==0) {
        make_france_flag(width);
    }
    else if(country_code==1) {
        make_germany_flag(width);
    }
    else {
        make_lithuania_flag(width);
    }
}   

void make_france_flag(int width) {
    int i;
    int height;
    height = width * 2/3;
    make_ppm_header(width, height);
    for(i = 0; i < height; i++)
        make_france_flag_row(width);
}

void make_germany_flag(int width) {
    int i;
    int height;
    height = width * 3/5;
    make_ppm_header(width, height);   
    for(i = 0; i < height; i++)
        make_germany_flag_row(width);
}

void make_lithuania_flag(int width) {
    int i;
    int height;
    height = width * 3/5;
    make_ppm_header(width, height);
    for(i = 0; i < height; i++)
        make_lithuania_flag_row(width);
}

void make_france_flag_row(int width) {
    int i;
    for (i = 0; i < width / 3; i++)
        make_pixel(0, 85, 164);
    for (i = 0; i < width / 3; i++)
        make_pixel(255, 255, 255);
    for (i = 0; i < width / 3; i++)
        make_pixel(250, 60, 50);
}

void make_germany_flag_row(int width) {
    int i;
    for (i = 0; i <= width / 3; i++)
        make_pixel(0, 0, 0);
    for (i = 0; i <= width / 3; i++)
        make_pixel(255, 0, 0);
    for (i = 0; i <= width / 3; i++)
        make_pixel(255, 204, 0);
}

void make_lithuania_flag_row(int width) {
    int i;
    for (i = 0; i < width / 3; i++)
        make_pixel(253, 185, 19);
    for (i = 0; i < width / 3; i++)   
        make_pixel(0, 106 , 68 );
    for (i = 0; i < width / 3; i++) 
        make_pixel(193, 39, 45);
}
sampathsris
  • 21,564
  • 12
  • 71
  • 98
  • You should show your results and explain what is not wrong/not expected if you want help. Do you know what you want the answer to be? – Dwayne Towell Oct 07 '14 at 02:52
  • 4
    Hint: If your width is not a multiple of 3, how many pixels are you writing out per row? Are you writing out exactly `width` pixels in all cases? – Adam Rosenfield Oct 07 '14 at 03:15
  • Yes it is the exact width taken from the user input for each row. My problem is I can't get an entire column or row to be the same color. – Chase Koger Oct 07 '14 at 15:55
  • Another hint: The answer to @AdamRosenfield's question is "no". :-) Fix this issue, and see if that helps. – Harald K Oct 14 '14 at 11:09

0 Answers0