Diamond asterisk

This is a discussion on Diamond asterisk within the C++ Programming forums, part of the General Programming Boards category; Can someone help me with this problem? write a program that prints the following diamond shape. you may use output ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    Diamond asterisk

    Can someone help me with this problem?

    write a program that prints the following diamond shape. you may use output statements that print either a single asterisk ( * ) or a single blank. maximize your use of repetition (with nested FOR structures) and minimize the number of output statements...
    Code:
          *
        * * *
      * * * * *
    * * * * * * *
      * * * * *
        * * *
          *
    Last edited by Dave_Sinkula; 07-22-2006 at 12:37 PM. Reason: Added [code][/code] tags to preserve whitespace -- and fixed spacing to boot.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    That's a triangle, not a diamond. You can pass on your homework assignment by answering just that.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    sorry
    ------*
    ----* * *
    --* * * * *
    * * * * * * *
    --* * * * *
    ----* * *
    ------*

    there u go!!

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    Ah... teacher almost fooled ya. Didn't they?

    The answer you can find here: http://cboard.cprogramming.com/annou...t.php?f=3&a=39
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    yeah my gf should read that...thanx man!!

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Quote Originally Posted by Mario F.
    That's a triangle, not a diamond. You can pass on your homework assignment by answering just that.
    I've never known a Triangle to have 4 sides, I am astounded by this new feat.

    I always thtought things like that were called Rhombi. (If that is the right plural?)

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    The original post was edited, bumfluff.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I've never known a Triangle to have 4 sides, I am astounded by this new feat.
    You missed the thread before a moderator added code tags to preserve formatting. This is how it was displayed originally:

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

    So, where are the four sides?
    My best code is written with the delete key.

  9. #9
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    Bumfluff obviously sees geometry in more than three dimentions, allowing him to conclude that a triangle has four sides.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You missed the thread before a moderator added code tags to preserve formatting.
    What does it mean? What tags? When moderators will add them?
    Last edited by siavoshkc; 07-22-2006 at 02:21 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    psychopath - what a stupid thing to say!! He sees in 4-D! The triangle obviously evolved over time to become the great and powerful diamond we see today!

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Ok, I'm going to copy the diamond and post it (the one from the first post):

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

    Now I'm going to do the same, but wrap CODE tags around it:

    Code:
          *
        * * *
      * * * * *
    * * * * * * *
      * * * * *
        * * *
          *
    Same again with Quote tags:

    *
    * * *
    * * * * *
    * * * * * * *
    * * * * *
    * * *
    *
    The code tags allows as many spaces as you desire, however, without them, the multiple spaces are deleted. Quote tags put the fancy grey background to it, but don't preserve the formatting as the code ones do.

  13. #13
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I hope this is what you were looking for and helps. Let me know if you don't understand my comments. I am sure it isn't the most elegant and efficient, but I am pretty sure that it works. Has variables that easily allow you to control the height, width, and starting spot.

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
        //rows for the height
        //cols for the width
        const int rows = 3;
        const int cols = 20;
        
        //the column you want to position the uppermost point on the screen
        int startingSpot = 5;
        //increment over every row
        for(int i = 0; i < rows +1; i++)
        {
            //increment over every column of every row
            for(int j = 0; j < cols; j++)
            {
                //if you are in the correct range
                //notice that the number of asterisks on any line is a function
                //of the line number you are on. Basically you print out an asterisk
                //at the startingSpot of hte first line. Then the next line you print
                //an asterisk out at the startingSpot and then an asterisk offset by
                //1 in front of and behind. This is the i variable that tells you 
                //which row you are one
                if(j > startingSpot - i and j < startingSpot +i) 
                    cout<<"*";
                else
                    cout<<" ";
                    
                cout<<" ";    
            }   
            //print out a blank line and start on the next row
            cout<<endl;
        }
     
     //this does the same thing as the above code, but it prints the rows in reverse
        for(int i = rows +1; i >0 ; i--)
        {
            for(int j = 0; j < cols; j++)
            {
                if(j > startingSpot - i and j < startingSpot +i) 
                    cout<<"* ";
                else
                    cout<<"  ";
            }   
            cout<<endl;
        }
        
        cin.ignore();
        cin.get();
    }

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    ... and this is why they say the internet is a good learning medium and every kid should have a computer to help with school.






    ... and why my daughter still learns from text books
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am sure it isn't the most elegant and efficient
    At least it's decent code. Most people who try to give free lunches can't even get hello world right.

    >but I am pretty sure that it works
    I'm torn between telling you it's broken and telling you to verify that it works. A good programmer has no doubts because a good programmer tests her code thoroughly before releasing it. On the other hand, if I tell you it's broken, the OP may not try to steal your code for an easy good grade. That's why we don't give out homework answers.
    My best code is written with the delete key.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  3. command line asterisk converted to list
    By linucksrox in forum C++ Programming
    Replies: 19
    Last Post: 07-14-2006, 03:22 PM
  4. making a diamond using functions
    By Nikisha in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2003, 08:05 PM
  5. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM

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