Like Tree1Likes
  • 1 Post By stahta01

Entering Country Codes to display flags

This is a discussion on Entering Country Codes to display flags within the C Programming forums, part of the General Programming Boards category; I have a lab at Clemson that we have to construct a C program that produces the respective images of ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19

    Entering Country Codes to display flags

    I have a lab at Clemson that we have to construct a C program that produces the respective images of flags (France, North Rhine, Westphalia, Gibon). Basically the outcome is to have a program that when the country code is entered (0,1,2, or 3) it will display the flag of the country*. "If" statements cannot be in "While" loops, "While" loops cannot be in "While" loops, and "While" loops cannot be in "If" statements. I've been at it for a few hours now, looking through the book and google searches. I'm incredibly frustrated because I have no idea where to start, largely due to my frustration.

    This is the main () function that we are supposed to use.
    Code:
    #include <stdio.h>
    
    P6
    800 600 255\n
    
    int main ()
    {
        int width;
        int country_code;
        {
        //Read image dimensions and pixel color         ~~**
        }
        fscanf(stdin, "%d %d", &country_code, &width);
        fprintf(stderr, "Making Country %d width %d \n", country_code, width);
        {
        //write the image data..........     ~~***
        }
        make_ppm_image(country_code, width);
    
    return 0;
    }


    Questions--
    *How am I going to tell the program to separate the colors vertically as opposed to horizontally?
    **To determine the dimensions of each flag, I'm assuming based on the country code, is this going to be just one single four part if/else statement? such as if country_code = 1, then width = 400 and height = 300; else if country_code=2, then width = 400, height =250 ??
    ***I have no idea, we haven't touched on this in lecture. But the lab says that we are supposed to use the make_pixel() and make_ppm_header() and gives us this
    Code:
    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);
    }
    I really just don't know what else to ask. All I've come up with on my own is

    Code:
    France 
        fprintf(stdout, "%c%c%c", 0,0,255);
        fprintf(stdout, "%c%c%c", 255,255,255);    
        fprintf(stdout, "%c%c%c", 255,0,0);
    
    North Rhine
        fprintf(stdout, "%c%c%c", 0,255,0);
        fprintf(stdout, "%c%c%c", 255,255,255);
        fprintf(stdout, "%c%c%c", 255,0,0);
    
    Westphalia
        fprintf(stdout, "%c%c%c", 255,255,255);
        fprintf(stdout, "%c%c%c", 0,0,255);
    
    Gabon
        fprintf(stdout, "%c%c%c", 0,255,0);
        fprintf(stdout, "%c%c%c", 255,255,0);
        fprintf(stdout, "%c%c%c", 0,0,255);
    
        fscanf(stdin, "%c%c%c", &r,&g,&b);
    , not much of which has anything to do with anything. Either way, I'm not asking for code in as a response, maybe just a better explanation of something I can look up in the book to give me a better idea of what I'm supposed to do?? Everything I've tried to google comes back with programs that pull up existing images, not creating new ones. Usually I can look at code, read the book and between the two say "Oh, I see what you did there" but I can't here because I literally don't know what to ask.

    Not to mention I have no idea how I'm gonna construct this without "While" loops in if/else and vice verse -_-.

    I know that this is a really long, broad, unintelligible post. I apologize, it's just that I'm too lost to come up with a logical question and I've been at school all morning and work all day, My brain is obliterated. Thanks for any patience and responses.

    Robert

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your frustration is a natural reaction to the stupidity of the problem.

    One way to get around the "don't use while's (or for's) inside if's and vice versa" rule is to use function calls:
    Code:
    void make_ppm_image(int country_code, int width) {
        int height;
    
        // figure out the height somehow ???
    
        make_ppm_header(width, height);
    
        if (country_code == 0) {
            make_france_flag(width, height);
        }
        else if (country_code == 1) {
           make_north_rhine_flag(width, height);
        }
        // etc.
    }
    
    void make_france_flag(int width, int height) {
        int i;
        for (i = 0; i < height; i++)
            make_france_flag_row(width);
    }
    
    void make_france_flag_row(int width) {
        int i;
        for (i = 0; i < width / 3; i++)
            make_pixel(0, 0, 255);
        for (i = 0; i < width / 3; i++)
            make_pixel(255, 255, 255);
        for (i = 0; i < width / 3; i++)
            make_pixel(255, 0, 0);
    }
    P.S. I think your Westphalia flag should have red (not blue) on the bottom.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19
    Thanks for the reply. I'm between classes trying to figure this out and I'm still lost. I'll have a little more time later to get it done... But can someone just check over what I have right now and see where the major errors are? (there should be alot, I just compiled it and it's a nightmare.) All it came back with was

    Lab6a.c:4:1: error: expected '=', ',', ':', 'asm' or '_attribute_' before numerit constant
    Lab6a.c:4:1: error: stray '\' in program

    But here's my code so far... Can't say I'm too much further than before

    Code:
    #include <stdio.h>
    
    P6
    800 480 255\n
    
    int main ()
    {
        int width;
        int country_code;
        float height;
    {
        void make_ppm_image(int country_code, int width) {
                int height;
     
            if (width == 400){
            height == 300
         }
        else if (width == 800){
            height == 480
        }
        else (width ==900){
            height == 600
        }
            make_ppm_header(width, height);
     
            if (country_code == 0) {
                make_france_flag(width, height);
            }
            else if (country_code == 1) {
               make_north_rhine_flag(width, height);
            }
            else if (country code == 2) {
            make_gabon_flag(width, height);
        }
        else (country code ==3) {
            make_westphalia_flag(width, height);
        }
    }
        }
        fscanf(stdin, "%d %d", &country_code, &width);
        fprintf(stderr, "Making Country %d width %d \n", country_code, width);
        {
        
        void make_france_flag(int width, int height) {
            int i;
            for (i = 0; i < height; i++)
                make_france_flag_row(width);
        }
     
        void make_france_flag_row(int width) {
            int i;
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 0, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 255, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 0, 0);
        }
        void make_gabon_flag(int width, int height) {
            int i;
            for (i = 0; i < height; i++)
                make_gabon_flag_row(width);
        }
     
        void make_gabon_flag_row(int width) {
            int i;
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 255, 0);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 255, 0);
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 0, 255);
        }    
        void make_westphalia_flag(int width, int height) {
            int i;
            for (i = 0; i < height; i++)
                make_westphalia_flag_row(width);
        }
     
        void make_westphalia_flag_row(int width) {
            int i;
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 0, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 255, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 0, 255);
        }
        void make_north_rhine_flag(int width, int height) {
            int i;
            for (i = 0; i < height; i++)
                make_north_rhine_flag_row(width);
        }
     
        void make_north_rhine_flag_row(int width) {
            int i;
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 0, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 255, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 0, 255);
        }
        }
        make_ppm_image(country_code, width);
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Yikes! You have function definitions inside your main function?!

    And this:
    Code:
    P6
    
    800 480 255\n
    is the header in a Portable PixMap (PPM) file. It doesn't belong in your code there (you can put it inside a string to write it to the output file, though).

  5. #5
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19
    So lines 8-10 need to come outside the main function?

    I am so lost I don't know where to begin.. And this is due in 9 hours >.<

    I literally have no idea what's going on here. The output is supposed to be a PPM file, I just thought that it belonged up there.

    Jesus Christ.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Robert Harp View Post
    So lines 8-10 need to come outside the main function?
    Quite the contrary! Most of the other lines (where you define your functions) should be outside of main().
    Basically your program should look like:
    Code:
    #include <stdio.h>
    // if necessary include other headers
    
    // function prototypes
    void function1(int x);
    int function2(void);
    
    int main(void)
    {
        // body of main
        
        return 0;
    }
    
    // The function definitions follow here:
    void funtion1(int x)
    {
        // body of function1
    }
    
    int function2(void)
    {
        // body of function2
    }
    See also Functions in C - Cprogramming.com

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19
    Lab's over, thanks for all the help, everyone. I really appreciate it.

    I didn't get it working but i still need to know this stuff.

    This is as close as I got:

    Code:
    #include <stdio.h>
    
    void make_pixel(int, int, int);
    void make_ppm_header(int, int);
    void make_ppm_image(int, int);
    void make_france(int, int);
    void make_france_flag(int);
    void make_france_flag_row(int);
    void make_germany_flag(int);
    void make_germany_flag_row(int);
    void make_lituania_flag(int);
    void make_lituania_flag_row(int);
    
    
    int main ()
    {
        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_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_ppm_header(int width, int height);
     
           fprintf(stdout, "P6\n");
        fprintf(stdout, "%d %d %d\n", width, height, 255);
    
    }
    
    void make_france_flag(int width) {
        int i;
        int height=(width*2)/3;
        make_ppm_header(width, height);
            for (i = 0; i < height; i++)
    {
                make_france_flag_row(width);
    }
        }
    
    void make_france_flag_row(int width) {
        int i;
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 0, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 255, 255);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 0, 0);
        }
    void make_germany_flag(int width) {
        int i;
        int height=(width*3)/5;
        make_ppm_header(width, height);
            for (i = 0; i < height; i++)
                make_germany_flag_row(width);
        }
     
    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, 255, 0);
        }    
    void make_lithuania_flag(int width) {
        int i;
        int height=(width*3)/5;
        make_ppm_header(width, height);
            for (i = 0; i < height; i++)
                make_lithuania_flag_row( width);
        }
     
    void make_lithuania_flag_row(int width) {
        int i;
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 255, 0);
            for (i = 0; i < width / 3; i++)
                make_pixel(0, 255, 0);
            for (i = 0; i < width / 3; i++)
                make_pixel(255, 0, 0);
        }
    }
    most of my errors in the terminal included previous implicit declaration (I've never seen before and my lab guy didn't know, they're all students anyway.)

    Code:
    roberto@ubuntu-vm:~/Downloads$ gcc Lab6a.c
    Lab6a.c: In function ‘make_ppm_image’:
    Lab6a.c:51:39: error: ‘height’ undeclared (first use in this function)
    Lab6a.c:51:39: note: each undeclared identifier is reported only once for each function it appears in
    Lab6a.c: At top level:
    Lab6a.c:91:6: warning: conflicting types for ‘make_lithuania_flag’ [enabled by default]
    Lab6a.c:45:3: note: previous implicit declaration of ‘make_lithuania_flag’ was here
    Lab6a.c:99:6: warning: conflicting types for ‘make_lithuania_flag_row’ [enabled by default]
    Lab6a.c:96:10: note: previous implicit declaration of ‘make_lithuania_flag_row’ was here
    Lab6a.c:108:1: error: expected identifier or ‘(’ before ‘}’ token
    Any ideas? Do I need to start from scratch or is this program still salvageable?

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You've left out the 'h' in "lithuania" in your function prototypes.

    As for height being undeclared in make_ppm_image, you've apparently fixed that error.


    As for your algorithm, you're not thinking. It makes sense to make a row of the french flag by making one color for a third of the width, then the next color for the next third, and another color for the last third. But that's not how the other flags look, so they need a different algorithm.

    French:
    BBBWWWRRR
    BBBWWWRRR
    BBBWWWRRR

    Germany:
    KKKKKKKKK #whole row is black
    RRRRRRRRR #whole row is red
    YYYYYYYY #whole row is yellow

    You need to think about what the algorithm is doing.


    EDIT: Reading stahta's post and taking another look at your code, I see that make_ppm_image is missing it's ending brace. And make_ppm_header has a semicolon after the function header instead of a brace like it should have.
    Last edited by oogabooga; 10-11-2012 at 08:08 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,544
    Indent your code right.

    Your code looks to be having a function inside another function because of a missing brace to end the prior function.


    Code:
    #include <stdio.h>
    
    void make_pixel(int, int, int);
    void make_ppm_header(int, int);
    void make_ppm_image(int, int);
    void make_france(int, int);
    void make_france_flag(int);
    void make_france_flag_row(int);
    void make_germany_flag(int);
    void make_germany_flag_row(int);
    void make_lituania_flag(int);
    void make_lituania_flag_row(int);
    
    
    int main ()
    {
    	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_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_ppm_header(int width, int height);
    
    	fprintf(stdout, "P6\n");
    	fprintf(stdout, "%d %d %d\n", width, height, 255);
    
    }
    
    void make_france_flag(int width)
    {
    	int i;
    	int height=(width*2)/3;
    	make_ppm_header(width, height);
    	for (i = 0; i < height; i++)
    	{
    		make_france_flag_row(width);
    	}
    }
    
    void make_france_flag_row(int width)
    {
    	int i;
    	for (i = 0; i < width / 3; i++)
    		make_pixel(0, 0, 255);
    	for (i = 0; i < width / 3; i++)
    		make_pixel(255, 255, 255);
    	for (i = 0; i < width / 3; i++)
    		make_pixel(255, 0, 0);
    }
    void make_germany_flag(int width)
    {
    	int i;
    	int height=(width*3)/5;
    	make_ppm_header(width, height);
    	for (i = 0; i < height; i++)
    		make_germany_flag_row(width);
    }
    
    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, 255, 0);
    }
    void make_lithuania_flag(int width)
    {
    	int i;
    	int height=(width*3)/5;
    	make_ppm_header(width, height);
    	for (i = 0; i < height; i++)
    		make_lithuania_flag_row( width);
    }
    
    void make_lithuania_flag_row(int width)
    {
    	int i;
    	for (i = 0; i < width / 3; i++)
    		make_pixel(255, 255, 0);
    	for (i = 0; i < width / 3; i++)
    		make_pixel(0, 255, 0);
    	for (i = 0; i < width / 3; i++)
    		make_pixel(255, 0, 0);
    }
    }
    Code:
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c||In function 'make_ppm_image':|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|49|warning: implicit declaration of function 'make_lithuania_flag' [-Wimplicit-function-declaration]|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|55|error: 'height' undeclared (first use in this function)|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|55|note: each undeclared identifier is reported only once for each function it appears in|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|99|warning: conflicting types for 'make_lithuania_flag' [enabled by default]|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|49|note: previous implicit declaration of 'make_lithuania_flag' was here|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c||In function 'make_lithuania_flag':|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|105|warning: implicit declaration of function 'make_lithuania_flag_row' [-Wimplicit-function-declaration]|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|108|warning: conflicting types for 'make_lithuania_flag_row' [enabled by default]|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|105|note: previous implicit declaration of 'make_lithuania_flag_row' was here|
    H:\SourceCode\OpenSourceCode\Apps\IDEs\CodeBlocks\Projects\testc\main.c|118|error: expected identifier or '(' before '}' token|
    ||=== Build finished: 2 errors, 4 warnings (0 minutes, 0 seconds) ===|
    Tim S.
    Last edited by stahta01; 10-11-2012 at 08:09 AM.
    oogabooga likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,544
    The semi colon makes this a function prototype instead of a function.
    This is not likely what you wanted.

    NOTE: Braces are also missing for it to be a function body!

    Tim S.

    Code:
    	void make_ppm_header(int width, int height);
    
    	fprintf(stdout, "P6\n");
    	fprintf(stdout, "%d %d %d\n", width, height, 255);
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. country
    By crvenkapa in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-03-2008, 01:46 PM
  2. is greenland a country?
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-25-2002, 12:27 AM
  3. country stereotypes
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 06-08-2002, 09:17 AM
  4. New Country Project!
    By kidguru in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 03-01-2002, 03:47 PM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 03:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21