Creating a diamond pattern

This is a discussion on Creating a diamond pattern within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am attempting to create a diamond pattern, and failing miserably. I am looking for some info on ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Creating a diamond pattern

    Hello everyone,

    I am attempting to create a diamond pattern, and failing miserably. I am looking for some info on getting my proggie running

    Each dash represents a space. It is supposed to be monospace. Notice it goes 1-3-5-3-1. Here is an example of it working:
    ----x
    --xxx
    xxxxx
    --xxx
    ----x

    I know I need a few "for" loops. Outer for counting spaces and inner couple for counting and displaying spaces and numbers.
    Here's what I have so far...

    NEW CODE ADDED.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        int num;
      char character;
      cout << "Enter Number: ";
      cin >> num;
      
    int half=(num/2);
    cout <<half<<endl;
    
      while (num>0){ 
      cout << "Enter character: "<<endl;
      cin >> character;
      cout <<endl;
    
      for (int row=0;row<num;++row) {
          int numplus1=num++;
    
          for (int space=((numplus1/2)-row);space<=row;++space){  
              cout <<" ";
              cout <<endl;
          for (int display=num-((numplus1)-(2*row));display<=num;display++){
              cout <<character;
              cout <<endl;
          }}
          for (int space=((numplus1/2)-row);space<=row;++space){ 
              cout <<" ";
              cout <<endl;
          for (int display=num-((numplus1)-(2*row));display<=num;display++){
               cout <<character;
               cout <<endl;
          }}
        cout << "\nEnter Number: ";
      cin >> num;
    }
      cout <<endl;
      cout << "Bye"<<endl;
      system("PAUSE");
      return 0;}
    }
    Last edited by ___________; 07-15-2008 at 03:30 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm supposed to read that when all the lines start at the left? Please.

    And you forgot to say what your question was.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    1. Pick an indent style - http://en.wikipedia.org/wiki/Indent_style

    2. Do something like
    for ( row = 0 ; row < 5 ; row++ )

    The first thing inside that loop would be
    int numSpaces = /* an expression based on the value of row */
    int numStars = /* another expression based on the value of row */

    Use those two results to print a number of spaces, then a number of stars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Code:
    class A {};
    class B : public A {};
    class C : public A {};
    class D : public B, C {};
    Done.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    I added new code. I am too newb to know what classes are yet medievalelks. Now my outputs are getting horrible.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Where do you output spaces?

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    What do you mean? Spaces are supposed to go before the characters.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ___________ View Post
    What do you mean? Spaces are supposed to go before the characters.
    Yes they are. Why don't you ever print any out?

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Hmm. Good point. new code added.

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, 04:19 PM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 05:07 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  4. Creating Patterns
    By incognito in forum Game Programming
    Replies: 5
    Last Post: 03-16-2003, 09:02 AM
  5. text pattern recognition
    By mtsmox in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 08:38 AM

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