how can I complete this code ?, diamond shape

This is a discussion on how can I complete this code ?, diamond shape within the C++ Programming forums, part of the General Programming Boards category; Hi, guys! I have to write a program that prompts the user to enter an integer number for to draw ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Exclamation how can I complete this code ?, diamond shape

    Hi, guys!
    I have to write a program that prompts the user to enter an integer number for to draw a diamond.
    The number would be longest part of the diamond.
    I did upper part of the diamond but I cant continue the bottom part of the diamond.
    could you please show me a way to complete the diamond ?
    thanks in advance.

    Code:
     
    
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
     b: int i,j,max,choose,min=1;
     char c;
                 a: cout<<"Please enter a positive number so that we can draw a triangle; ";
                  cin>>choose;
    
                  if(choose<0)
    {
                  cout<<"You have entered a negative number please try again."<<endl;
                  goto a;
    
    }
                  if(choose%2==0)
                  choose --;
                  max=choose;
                  while(min<=choose)
    {
                  for(j=1;j<max;j++)
                  cout<<" ";
                  for(i=0;i<min;i++)
                  cout<<"*";
                  min += 2;
                  max--;
                  cout<<"\n";
     }
     cout<<"Do you wish to continue Y/N? \n";
     cin>>c;
     if(c=='Y' || c=='y' )
    
     goto b;
    
     system("PAUSE");
          return 0;
    }

  2. #2
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39
    Can you not simply reverse the loop?

    reset the values of min and max
    Create another while loop
    Code:
    while ( min >= 0) { ... }
    Then work your way down
    Something like
    Code:
                  for(j=max;j > 1;j--)
                  cout<<" ";
                  for(i=min;i>0;i--)
                  cout<<"*";
    Also, I would work on indenting your code better as well as avoiding using goto statements as they are confusing and unnecessary.

    Regards,

    cs_student

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. A solid shape diamond
    By viryonia in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2003, 09:22 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread


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