Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating a Box in C

    Question:

    Create a program that creates a user defined square of X

    If the user specifies they would like square to be 5 in width and 5 in height then the following output would be displayed:

    XXXXX
    XXXXX
    XXXXX
    XXXXX
    XXXXX

    5 in width x 5 in height.

    I have to use a nested loop, and it has to be basic,

    printf
    scanf

    for

    etc.

    Anyone can help?

  • #2
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think Solution For your problem is here..
    solution removed
    Last edited by oracleguy; 10-31-2011 at 04:37 PM. Reason: removed homework answer

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by sumit1 View Post
    I think Solution For your problem is here..
    Please don't just give away the answers to homework questions. The poster might get the question done but they won't learn anything and it will come back to get them when they have to take a test. See our homework policy: http://www.codingforums.com/showthread.php?t=53446


    As to the original question, break the problem into pieces and tackle each one separately. You first need to ask the user for the width and then the height. Then you need to write the outer for loop and then the inner for loop. Then inside of the inner loop print an 'X'.
    OracleGuy

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorry can you let people post

    This is for my own knowledge, I have completed my tutorial but I want to add a little extra, my code so far is the following: But I cant figure out how to instead of just saying print X, how I can the user to pick a letter, so if they wanted to change X to I, it would do
    iiiiii
    iiiiii
    iiiii
    iiiii

    Please see my coding so far:

    #include <stdio.h>

    #include <stdlib.h>

    void main(void)
    {
    //setting int's

    int width=0;
    int height=0;

    //user input

    printf("What width would you like the box to be? \n", width);
    scanf("%i", &width);
    printf("What height would you like the box to be? \n", height);
    scanf("%i", &height);

    printf("Box Result: \n\n"); //output will be displayed under here.
    //loop

    for (int loop=0;loop < height; loop++)
    {
    for(int nestloop=0;nestloop < width ; nestloop++)
    {
    printf("X");
    }
    printf("\n");
    }

    system("pause");
    }

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Good work so far. In order to make the character of the user's choosing, you would need to add another prompt and read in a single character. Then instead of just printing the X you would print your char variable.
    OracleGuy

  • #6
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I understand the theory behind it, but can you check my coding to see if Im on the same wave lenght? The following doesnt work, and Im wondering why, I was taught that for a Char, you use %c, and set inizalize variable its char [] = name, I think.?

    #include <stdio.h>

    #include <stdlib.h>

    void main(void)
    {
    //setting int's

    int width=0;
    int height=0;
    char [1] box;

    //user input

    printf("What width would you like the box to be? \n", width);
    scanf("%i", &width);
    printf("What height would you like the box to be? \n", height);
    scanf("%i", &height);
    printf("What Letter would you like the box to be? \n", box);
    scanf("%c", &box);


    printf("Box Result: \n\n"); //output will be displayed under here.
    //loop

    for (int loop=0;loop < height; loop++)
    {
    for(int nestloop=0;nestloop < width ; nestloop++)
    {
    printf("%c");}

    printf("\n");
    }

    system("pause");
    }

  • #7
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    For future posts, if you wouldn't mind, please put your code in code tags. For example:
    Code:
    &#91;CODE&#93; Your Code Goes Between These Tags &#91;/CODE&#93;

    Quote Originally Posted by kinggrunt View Post
    The following doesnt work, and Im wondering why,[...]
    Usually the compiler will tell you why. What are the errors you are getting?

    Quote Originally Posted by kinggrunt View Post
    I was taught that for a Char, you use %c, and set inizalize variable its char [] = name
    Yes, the correct formating character to use for a value of type Char is %c. You may wish to read over this reference, it will help you fix a few errors in your printf functions.
    http://www.cplusplus.com/reference/c...cstdio/printf/

    The correct syntax for declaring an array in C is:
    Code:
    Type name[Length];
            OR
    Type name[] = {value1, value2, value3, (etc...)};
    So, to declare a character array you would use:
    Code:
    Char box[1];
    However, a character array is not the same as a character. For your program you are going to want to use a simple character variable.

    Variables in C are declared like this:
    Code:
    Type name;
        OR
    Type name = value;
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •