Thread: Printing a Pattern of Stars

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Printing a Pattern of Stars

    Hey guys, I'm having trouble with this source code..I have most of it, but I can't seem to get the desired display.

    Objective: Input an Integer and Character from the keyboard. The output should be the outline of a square composed of the character and extending the width specified by the integer. For example, if the integer is 5 and the character is *, the output should be:

    *****
    * *
    * *
    * *
    *****
    Note: the input NUMBER must be greater than or equal to 3.
    What the hell can I do to make this thing work? I might be too inexperienced..but here's the code I've come up with so far..

    #include<iomanip.h>
    main()
    {
    int Num, i, j; char Letter;

    cout<<"\nEnter an Integer:"; cin>>Num;
    cout<<"\nEnter a Character:";cin>>Letter;

    if(Num>=3)
    {
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=5;j++)
    cout<<setw(Num)<<Letter;
    cout<<endl;
    }//for
    }//if
    else
    {cout<<"Invalid Number-Must be greater than or equal to 3";
    }//else

    return 0;
    }

    Thanks for taking a look.

  2. #2
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Correction

    The pattern of stars that are in the Previous Post came out wrong..
    I would do it again, but it'll probably come out wrong again..so I'll Explain

    The Second, third and forth line should have only one * at the beginning and one * at the end .. The first and Last line should have *****. I hope this clears it up.. it should look like the outline of a rectangle.

    Thanks

  3. #3
    Unregistered
    Guest
    look at the pattern.

    The number of lines equals the number that was input which may or may not be 5.

    The first and last line has as many characters as the number that was input which may or may not be 5.

    The other lines have only the first and last char in the line as the char that was input, and the other char are spaces.

    you can start the loop counting variable at any value you want.


    if(Num>=3)
    {
    for(i=1;i<= Num;i++)
    {
    if(i == 1 || i == Num)
    {
    for(j=1;j<= Num;j++)
    {
    cout<< Letter;
    }
    cout << endl;
    }
    else
    //I'll leave the rest up to you

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Set a maximum value for the height and width borders, loop until you reach height or width and if the current value of the loop iterator is equal to height or width, print an *, otherwise print a space.
    Code:
    #include <iostream>
    using namespace std;
    
    int main ( void ) {
      int height = 5, width = 8;
      for ( int i = 0; i <= height; i++ ) {
        for ( int j = 0; j <= width; j++ )
          if ( i == 0 || j == 0 || i == height || j == width )
            cout<<"*";
          else
            cout<<" ";
          cout<<"\n";
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    I'm not getting it..I think I've been working on this too long and everything seems blurry.. could someone please explain it a little further?

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Nice solution Prelude

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Okay, you print the box line by line. On the first line your i variable will contain the value 0 which after the test will print an *. When you move to the next line i has the value 1, so unless j has the value 0 or width a space will be printed, otherwise an * will be printed.
    The i variable handles the vertical lines while the j variable handles everything horizontally.
    Code:
    If i has the value 0 or 5, print an *. If j has the value 0 or 8, print an *. Anything else print a space.
    i = 0, j = 1 - 8
    ********
    i = 1, j = 1 - 8
    *      *
    i = 2, j = 1 - 8
    *      *
    i = 3, j = 1 - 8
    *      *
    i = 4, j = 1 - 8
    *      *
    i = 5, j = 1 - 8
    ********
    
    Another way to view it would be with line numbers
    Line 0: 012345678
    Line 1: 0       8
    Line 2: 0       8
    Line 3: 0       8
    Line 4: 0       8
    Line 5: 012345678
    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    if you want to print 5 * across (say width is 5) then you need to do this:

    j = 0; j < width; j++


    or this:

    j = 1; j <= width; j++

    but not this:

    j = 0; j <= width; j++

    which will print 6 * across.

    Same for height.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    if you want to print 5 * across (say width is 5) then you need to do this:
    j = 0; j < width; j++
    or this:
    j = 1; j <= width; j++
    but not this:
    j = 0; j <= width; j++
    which will print 6 * across.
    Same for height.
    Really, have you tried it? Before correcting someone make sure that you're right. I made it <= for two reasons: First, I didn't really care how many *'s were printed because it was irrelevant to solving the problem. Second, if you change it to < with the code I posted the tests will fail for the right and bottom borders of the box and the output will look like this:
    Code:
    ********
    *
    *
    *
    *
    Changing the loop condition would require a different algorithm or a hackish fix and I saw no need to do so since I'm not getting graded for how many *'s my program actually prints as opposed to my test variables. I was describing a solution to a problem, not writing a program based on set requirements.

    But, for the nitpickers, here's the hacked fix
    Code:
    #include <iostream>
    using namespace std;
    
    int main ( void ) {
      int height = 5, width = 8;
      for ( int i = 0; i < height; i++ ) {
        for ( int j = 0; j < width; j++ )
          if ( i == 0 || j == 0 || i == height - 1 || j == width - 1 )
            cout<<"*";
          else
            cout<<" ";
          cout<<"\n";
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    Last edited by Prelude; 02-28-2002 at 01:22 PM.
    My best code is written with the delete key.

  10. #10
    Unregistered
    Guest
    I didn't intend to offend. However, attention to details when writing code is important. I agree the code originally posted by Prelude does produce the requirements drawing the correct design, but if the prupose of the input is to specify the size of the drawing (admittedly not a stated requisite of the problem but highly implied by the lines:

    if the integer is 5 and the character is *, the output should be:

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

    in the firt post) then Prelude's original version will be off by 1 in the width and height of the drawing. I can't count the number of times I and others have introduced errors into programs by not paying attention to use of < vs <= etc when using 0 vs 1 as the starting point in a loop.

    Again, no offense intended. I accept criticism of being nitpicky in effort to be clear about what is happening.

  11. #11
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Still in Trouble

    Hey..Prelude and all you guys, I appreciate your help, but it seems the more I work on this damn code, I get more and more aggrivated..like a girlfriend who gets outta line!

    what I've done so far is at the bottom, and I'm trying to make it print this out:

    *****
    * `` * I just put the " ` " in the middle so it won't collapse
    * `` * when I post it..this is not in the program!!
    * `` *
    *****

    __________________________________________________ __

    #include <iomanip.h>
    #include <stdlib.h>

    main()
    {


    int Num, i, j; char Letter;
    cout<<"\n Enter an Integer: "; cin>>Num;
    cout<<"\n Enter a Character: "; cin>>Letter;

    for ( i =0; i < Num; i++ ) {
    for ( j =0; j < Letter; j++ )
    if ( i == 0 || j == 0 || i == Letter - 1 || j == Num - 1 )
    cout<<"*";
    else
    cout<<" ";
    cout<<"\n";
    }


    system("PAUSE");
    return 0;
    }

    when i do this, the output is:
    __________________________________________________ __

    Output:

    Enter an Integer: 5

    Enter a Character: *

    ******************************************
    * `*
    * `*
    * `*
    * `*

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Re: Printing a Pattern of Stars

    Originally posted by ProgrammingDlux


    *****
    **
    **
    **
    *****
    go:

    PHP Code:
    *****
    *   *
    *   *
    *   *
    ***** 
    OR

    Code:
    *****
    *   *
    *   *
    *   *
    *****

  13. #13
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    okay i got this much, but it's still ........in' me off

    #include <iomanip.h>
    #include <stdlib.h>

    main()
    {


    int Num, i, j; char Letter;
    cout<<"\n Enter an Integer: "; cin>>Num;
    cout<<"\n Enter a Character: "; cin>>Letter;

    for (i =0; i<=Num; i++ ) {
    for (j =0; j<=Num; j++ )
    if (i == 0 || j == 0 || i == Letter - 1 || j == Num - 1 )
    cout<<Letter;
    else
    cout<<" ";
    cout<<"\n";
    }


    system("PAUSE");
    return 0;
    }


    /* OUTPUT


    Enter an Integer: 5 <- user input

    Enter a Character: * <- user input

    ******
    * ``*
    * ``* once again the " ` " is only for space..not part
    * ``* of the program
    * ``*
    * ``*

    and that's all... but what I need to Output is

    *****
    *```*
    *```*
    *```*
    *****

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    However, attention to details when writing code is important
    So is learning how to modify existing code to suit your needs
    Code:
    #include <iomanip.h> 
    #include <stdlib.h> 
    
    int main() 
    { 
      int Num, i, j; char Letter; 
      cout<<"\n Enter an Integer: "; cin>>Num; 
      cout<<"\n Enter a Character: "; cin>>Letter;
      /* Place a newline here or the box will print oddly */
      cout<<endl;
    
      /* Be consistent with your tests, if you start at 0
      ** then test for < Num, not <= Num. Otherwise 
      ** the number of Letters will be off.
      */
      for (i =1; i<=Num; i++ ) { 
        for (j =1; j<=Num; j++ ) 
         /* The test for i's maximum boundary was checking
         ** against Letter, a value which i would never have.
         ** This has been changed to Num and the check
         ** against Num - 1 has been removed to fix the off
         ** by one error.
         */
         if (i == 1 || j == 1 || i == Num || j == Num ) 
           cout<<Letter; 
         else 
           cout<<" "; 
        cout<<"\n"; 
      }
      /* main returns a value */
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Patterns
    By ferniture in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 10:00 PM
  3. need help printing this pattern
    By Blimpy325 in forum C Programming
    Replies: 4
    Last Post: 03-04-2006, 06:40 AM
  4. Printing a pattern correctly???
    By Basia in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 04:34 PM
  5. text pattern recognition
    By mtsmox in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 08:38 AM