A diamond problem

This is a discussion on A diamond problem within the C++ Programming forums, part of the General Programming Boards category; Hello everybody, I need some help here How am I gonna display a diamond shape as shown below by only ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Unhappy A diamond problem

    Hello everybody, I need some help here

    How am I gonna display a diamond shape as shown below by only using an output statements that prints a single asterisk(*) and using nested for loop?

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

  2. #2
    Unregistered
    Guest
    you must be in Dr. Li class

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    look for patterns

    it isn't that hard

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421

    Thumbs down

    i have to say that you hav chosen the perfect name for this board

    1) use your head
    2) think about it
    3) try coding it
    4) post if you have problems with your code.

    Don't try and get people to do your homework for you!

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    that doesnt look like a diamond...

    this looks like a diamond:

    Code:
           * 
         *** 
       ***** 
     ******* 
       ***** 
         *** 
           *
    well how hard could it be?

    wait, the code for one of my programs has a diamond...

    hmm

    well you could start by setting a variable for the amount of space between the beginning and your first asterisk and do a looping statement that takes away one space each time so it would be like:

    y = 5; // could be anything doesnt matter

    cout<<setw(y)<<""<<"*"<<endl;
    cout<<setw(y-1)<<""<<"***"<<endl;
    cout<<setw(y-2)<<""<<"*****"<<endl;
    cout<<setw(y-1)<<""<<"***"<<endl;
    cout<<setw(y)<<""<<"*"<<endl;

    that is the easy way to do it, i used for statements in my program...but it would be more code and i dont want to show you

    :::EDIT:::

    truly sorry, for some reason the diamond doesnt look right, well you know what im talking about...and my code should work
    Paro

  6. #6
    Unregistered
    Guest
    becoming a good programmer does require you to be a good problem solver, and looking for patterns is certainly the key to solving this problem. However, problem solving doesn't teach you the syntax of the language. Personally, I feel there should be a problem solving course separate from syntax instructions, but I am only a lowly peon, so what does my opinion matter. Therefore, I will share my approach to solving this problem, although there are several different possible approach's to use.

    Notice that there is symetry around the middle line of the diamond as well as around the middle column of the diamond. If you substitute the char 'a' for spaces it is easier to see this:

    aa*aa a*a
    ..........*a ***
    ***** a*a
    ..........*a
    aa*aa

    Notice also the that number of rows equals the number of columns and that to make a diamond you need an odd number of rows and columns (unless you can figure out how to do half spaces, but that is beyond me). Notice that the number of lines where the number of * increases is one half the maximum number of * plus one (if you use integer math). Notice that in each line the number of a's decreases by one before the * startand by one after the * end, and that the number of * increases or decreases by 2 each line. Note that the number of a's before the * start in the top section of the diamond is the number of columns minus the * divided by 2. Note that if you say line 1 is row 0 then the number of * in the top part of the diamond is one plus the quantity row number times two. And you can figure out similar relationships for the lower part of the diamond.

    Based on these observations, I would try coding for the top part of the diamond separately from the bottom. I would use a loop to print out the a's one at a time and a separate loop to print out the * one a at time. I would use the row/line information and maximum number of * per line to do the calculations necessary to determine how many a's and how many * to print on each line.

    At this point I would try to put all this into a coherent set of instructions in english (pseudocode) before writing actual program code.

    Given differences in opinion as to how much information/help to give, etc. ; it's probably best to let you work on it from here, posting code/pseudocode/error statements/etc. with specific questions, etc. as you go along.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Well, this isn't exactly what you're looking for, but it should help you to figure out what you need.

    #include <iostream.h>
    #include <math.h>

    void diamond(int num)
    {
    for (int lines = 1; lines <= (num); lines++)
    {
    for (int spaces = 0; spaces < (abs(num - lines)); spaces++)
    {
    cout<<" ";
    }

    for (int stars = 0; stars < (lines + lines - 1); stars++)
    {
    if (stars % 2 == 0)
    cout<<"+";
    else
    cout<<"*";
    }
    cout<<endl;
    }

    int temp = 0;

    for (int bottom_lines = (num-1); bottom_lines >= 0; bottom_lines--)
    {
    for (int bottom_spaces = 0; bottom_spaces <= temp; bottom_spaces++)
    {
    cout<<" ";
    }

    for (int bottom_stars = (bottom_lines + bottom_lines - 1); bottom_stars > 0; bottom_stars--)
    {
    if (bottom_stars % 2 == 0)
    cout<< "*";
    else
    cout<<"+";
    }

    cout<<endl;
    temp++;
    }


    }

    int main()
    {
    int number;

    cout << "Please enter the number for the drawing" << endl << " of the diamond." << endl;
    cin >> number;

    if ((number < 3) || (number > 10))
    {
    cout << "Sorry, incorrect number. Try again." << endl;
    return 0;
    }

    diamond(number);

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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