Thread: 'display' function

  1. #1
    Unregistered
    Guest

    'display' function

    This code displays a hollow rectangle from user supplied symbol and parameters. It works perfectly. How could I revise it to use the 'display' function instead.




    #include <stdio.h>

    int main(void)
    {
    int inner, outer;// loop control varibles
    int length, width;
    char icon;// icon is the character that is used for the display

    printf("This program draws a hollow rectangle using a character of your choice\n\n");
    printf("Choose character for display\n ");
    scanf("%c", &icon);
    printf("Enter length: ");
    scanf("%d",&length);
    printf("Enter width: ");
    scanf("%d",&width);


    for(inner = 0; inner < width; inner ++)// prints top row of icons

    printf("%c",icon);

    printf("\n");

    for(inner = 0; inner < length; inner ++)// prints both sides starting with left
    {
    printf("%c",icon);

    for(outer = 0; outer < (width-2); outer ++)// prints spaces to where the right column should be

    printf(" ");

    printf("%c",icon);// prints an icon in the right column

    printf("\n");
    }// end for
    for(inner = 0; inner < width; inner ++)// prints bottom row of icons

    printf("%c",icon);

    printf("\n\n");

    return 0;

    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You mean along the lines of

    void display ( char icon, int length, int width );

    Which you would call from main (after reading in the user input) with
    &nbsp; display( icon, length, width );

  3. #3
    Unregistered
    Guest
    Yes that is what I am talking about.
    I get two "unresolved external" faults when trying to run this code. I am not sure where i need to implement the display function. I am definitely missing something.









    #include <stdio.h>

    void display (char icon, int length, int width);

    int main()
    {


    //int inner, outer;// loop control varibles
    int length, width;
    char icon;// icon is the character that is used for the display

    printf("This program draws a hollow rectangle using a character of your choice\n\n");
    printf("Choose character for display\n ");
    scanf("%c", &icon);
    printf("Enter length: ");
    scanf("%d",&length);
    printf("Enter width: ");
    scanf("%d",&width);


    display ( icon, length, width);
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Yeah, you're meant to write the function as well

    Code:
    void display (char icon, int length, int width) {
        // your looping code here
    }

  5. #5
    Unregistered
    Guest
    Yeah, well I'm not very good with loops either.
    This is as close as I could get at this point. It is hard for me to learn this type of thing from a book and a professr who's second launguage is English.
    I am getting two, unresolved external faults after building.


    #include <stdio.h>

    int main(void)
    {
    int inner, outer;
    int length, width;
    char icon;// icon is the character that is used for the display

    printf("This program draws a hollow rectangle using a character of your choice\n\n");
    printf("Choose character for display\n ");
    scanf("%c", &icon);
    printf("Enter length: ");
    scanf("%d",&length);
    printf("Enter width: ");
    scanf("%d",&width);


    for(inner = 0; inner < width; inner ++)

    display (icon, width);

    printf("\n");

    for(inner = 0; inner < length; inner ++)

    {
    display (icon, length);

    for(outer = 0; outer < (width-2); outer ++)


    display (" ");

    display(icon);

    printf ("\n");
    }// end for
    for(inner = 0; inner < width; inner ++)


    display (icon, width);

    printf("\n\n");

    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're misunderstanding how to use functions.
    Let's say that my code has this line:

    a = b + c;

    Assuming I want to move it to a function wich does the same task, how could I do it fairly easy? What is needed? Well, in this case, since we're assigning a value to 'a', I can use a return to give me this. The function needs the values of B and C, so we can provide those via arguments. Thus, my function could look like:

    int myFunction( int b, int c ) { return b+c; }

    In your case, you want to do the same thing. Move your whole working loop from the original, into a function, and pass it the values it needs to know (size, and character to print).

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Unregistered
    Guest
    It still has syntax errors and wont compile. Is this any closer to being correct?


    #include <stdio.h>

    int main()
    {

    int length, width;
    char icon;// icon is the character that is used for the display
    const char blank =' ';

    printf("This program draws a hollow rectangle using a character of your choice\n\n");
    printf("Choose character for display\n ");
    scanf("%c", &icon);
    printf("Enter length: ");
    scanf("%d",&length);
    printf("Enter width: ");
    scanf("%d",&width);

    {
    void display( int width, char icon);

    display(width, icon);
    printf("\n");
    {
    }
    display(int width, int length, char icon, const char ' ');
    {
    for (length -2)
    printf("%c", icon);
    display(width -2 ,' ');
    printf("%c",icon);
    }

    display(width, icon);
    }
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Like this
    Code:
    #include <stdio.h> 
    
    void display( char icon, int length, int width );
    
    int main(void) { 
        int length, width; 
        char icon;// icon is the character that is used for the display 
    
        printf( "This program draws a hollow rectangle using a character of your choice\n\n" ); 
        printf( "Choose character for display\n " ); 
        scanf( "%c", &icon ); 
        printf( "Enter length: " ); 
        scanf( "%d",&length ); 
        printf( "Enter width: " ); 
        scanf( "%d",&width ); 
        display( icon, length, width );
        return 0;
    }
    
    void display( char icon, int length, int width ) {
        int inner, outer;// loop control varibles 
    
        for(inner = 0; inner < width; inner ++)// prints top row of icons 
            printf( "%c",icon ); 
    
        printf( "\n" ); 
    
        for(inner = 0; inner < length; inner ++)// prints both sides starting with left 
            { 
            printf( "%c",icon ); 
    
            for(outer = 0; outer < (width-2); outer ++)// prints spaces to where the right column should be 
    
                printf( " " ); 
    
            printf( "%c",icon );// prints an icon in the right column 
    
            printf( "\n" ); 
        }// end for 
    
        for(inner = 0; inner < width; inner ++)// prints bottom row of icons 
            printf( "%c",icon ); 
    
        printf( "\n\n" );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM