1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming question

  1. Apr 12, 2013 #1
    I have to write a program that will take user input to make certain shapes. If the user inputs 's', its a solid square, 'b', its a hollow box, 't' its a triangle. They also choose how many rows it is and what the shape is made of. Example

    ****
    ****
    ****

    this would be a user input of 's', 3 rows, and the * symbol.
    Now the part i cant figure out is how to specify the rows. I would have just used if statements and make a shape out a bunch of %c but i cant figure out how to make it more or less rows by user input. This is my code so far:

    #include <stdio.h>
    #include <stdlib.h>
    void main()
    {
    char a,c;
    int b;

    printf("Enter \"s\" for a square\nEnter \"b\" for a box\nEnter \"t\" for a triangle\n");
    scanf("%c",&a);
    system("cls");
    if (a=='s')
    printf("Square\n--------------------…
    if (a=='b')
    printf("Box\n--------------------\n"…
    if (a=='t')
    printf("Triangle\n------------------…

    printf("Enter the number of rows\n");
    scanf("%d",&b);
    system("cls");
    if (a=='s')
    printf("Square, %d rows\n--------------------\n",b);
    if (a=='b')
    printf("Box, %d rows\n--------------------\n",b);
    if (a=='t')
    printf("Triangle, %d rows\n--------------------\n",b);

    printf("Enter the character to use for drawing\n");
    scanf(" %c",&c);
    system("cls");
    if (a=='s')
    printf("Square, %d rows, made of \"%c\" characters\n--------------------\n",b,c)…
    if (a=='b')
    printf("Box, %d rows, made of \"%c\" characters\n--------------------\n",b,c)…
    if (a=='t')
    printf("Triangle, %d rows, made of \"%c\" characters\n--------------------\n",b,c)…

    /*Making the shape*/




    }

    And obviously i don't have anything under "making the shape". So far, my code asks for the 2 characters and 1 variable i need from the user but i don't know how to go about the rest.

    Any help? thanks in advance
     
  2. jcsd
  3. Apr 12, 2013 #2

    rcgldr

    User Avatar
    Homework Helper

    As an option to iteratively producing one line of a shape at a time, you could allocate a matrix of characters, and fill in the matrix with spaces and the user selected character, then output the matrix one line at a time.
     
  4. Apr 12, 2013 #3
    nvm, I got it after a little more tinkering. Just thought I'd post cause i had been stuck for a while. Thanks anyways!
     
  5. Apr 13, 2013 #4

    Mark44

    Staff: Mentor

    Would you show us what you have? We might be able to offer you some tips on improving your code.
     
  6. Apr 13, 2013 #5
    This is what i ended up with:

    #include <stdio.h>
    #include <stdlib.h>
    void main()
    {
    char a,c;
    int b,i,d,space,k=0;

    printf("Enter \"s\" for a square\nEnter \"b\" for a box\nEnter \"t\" for a triangle\n");
    scanf("%c",&a);
    system("cls");
    if (a=='s')
    printf("Square\n--------------------\n");
    if (a=='b')
    printf("Box\n--------------------\n");
    if (a=='t')
    printf("Triangle\n--------------------\n");

    printf("Enter the number of rows\n");
    scanf("%d",&b);
    system("cls");
    if (a=='s')
    printf("Square, %d rows\n--------------------\n",b);
    if (a=='b')
    printf("Box, %d rows\n--------------------\n",b);
    if (a=='t')
    printf("Triangle, %d rows\n--------------------\n",b);

    printf("Enter the character to use for drawing\n");
    scanf(" %c",&c);
    system("cls");
    if (a=='s')
    printf("Square, %d rows, made of \"%c\" characters\n--------------------\n",b,c);
    if (a=='b')
    printf("Box, %d rows, made of \"%c\" characters\n--------------------\n",b,c);
    if (a=='t')
    printf("Triangle, %d rows, made of \"%c\" characters\n--------------------\n",b,c);

    /*Making the shapes*/

    /*Square*/
    if (a=='s')
    for (i=0;i<b;i++)
    printf("%c%c%c%c%c\n",c,c,c,c,c);

    /*Box*/
    d=b-2;
    if (a=='b')
    printf("%c%c%c%c%c\n",c,c,c,c,c);
    if (a=='b')
    for (i=0;i<d;i++)
    printf("%c %c\n",c,c);
    if (a=='b')
    printf("%c%c%c%c%c\n",c,c,c,c,c);

    /*Triangle*/
    if (a=='t')
    for(i=1;i<=b;++i)
    {
    for(space=1;space<=b-i;++space)
    {
    printf(" ");
    }
    while(k!=2*i-1)
    {
    printf("%c ",c);
    ++k;
    }
    k=0;
    printf("\n");
    }
    }

    If you have any tips on making it simpler, feel free to post em cause if feel like that code is way long for what it needs to do (although it does do it). One question i do have is when i have something like printf("%c%c%c%c",c,c,c,c), is there a way to just have one ,c rather than one to match each %c in the string?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C programming question
  1. C programming question (Replies: 7)

  2. C programming question (Replies: 22)

  3. C programming question (Replies: 1)

  4. C programming question (Replies: 2)

  5. C programming question (Replies: 2)

Loading...