need help with this code

This is a discussion on need help with this code within the C Programming forums, part of the General Programming Boards category; Hi I'm new to c programming and need some help with this code I'm ask to write a program that ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    need help with this code

    Hi

    I'm new to c programming and need some help with this code

    I'm ask to write a program that reads in the side of a square and the prints that square out of asterisks. The program should work for squares of all side sizes between 1 and 20. For example, if the program reads a size of 4, it should print

    Code:
    ****
    ****
    ****
    ****
    It then ask me to modify the program so that it prints a hollow square. For example if the program reads a size of 5, it should print

    Code:
    *****
    *   *
    *   *
    *   *
    *****
    I was able to do the first part but don't know how to do the second part can any one help me

    below is the code for the first part:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int inputInteger(int, int);
    
    #define MININPUT 1
    #define MAXINPUT 20
    #define TRUE 1
    #define FALSE 0
    
    int main (void)
    {
    
    int squareSize, column, row;
      
    
    squareSize = inputInteger(MININPUT, MAXINPUT);
    
    row = squareSize;
    
    while(row >= 1){
             column = 1;
             while(column <= squareSize){
                          printf("*");
                          column++;
                          }
             
             printf("\n");
             row--;
             }
    system("pause");
    }
    
    int inputInteger(int minInput,int maxInput){
       int reading;
       int validInput;
       do{
          printf("\nPlease enter the number of side of a square: ");
          fflush(stdin);
          validInput = scanf("%d", &reading);
          if(!validInput)
                         printf("\nThis is not a valid entry\n");
          if((validInput == TRUE)&&((reading >maxInput)||(reading < minInput))){
               printf("Error, the number entered needs to be between %d to %d\n", minInput, maxInput);
               validInput = FALSE;
               }
       }while(!validInput);
       return reading;
       }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at an empty box. What do you have? (Note: There's an exception for a size=1 box.)

    Consider:
    Code:
    ***** <-- a top line
    *   * <-- a number of "hollow" lines
    *   *
    *   *
    ***** <-- a bottom line
    What is a hollow line?
    Code:
    starting edge
    |
    v
    *   *
        ^
        |
        ending edge
    Between the start and end edge, there are a number of spaces. Notice that the number of spaces is the same as the number of hollow lines.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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