Solved

Diamond

Posted on 1998-02-24
5
1,351 Views
Last Modified: 2006-11-17
how can I create a diamond shape using a "for" loop?
It should look like this (it should be a size of 9):

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

The faster I get the responce, the greater pts. u get
0
Comment
Question by:Dmitriy
  • 4
5 Comments
 
LVL 3

Author Comment

by:Dmitriy
Comment Utility
Edited text of question
0
 
LVL 3

Author Comment

by:Dmitriy
Comment Utility
Edited text of question
0
 
LVL 3

Author Comment

by:Dmitriy
Comment Utility
Edited text of question
0
 
LVL 3

Accepted Solution

by:
q2guo earned 0 total points
Comment Utility
int main()
{
    int i, j;
    const int SIZE=9;  // make sure SIZE is odd
    int k = SIZE / 2;

    for (int i=1; i <= SIZE; i++) {
        for (j = 0; j < k-i+1; j++) cout << " ";
        for (j = 0; j < i; j++) cout << "*";
        for (j = 0; j < k-i+1; j++) cout << " ";
      cout << endl;
    }
}
0
 
LVL 3

Author Comment

by:Dmitriy
Comment Utility
I know this addition is kind of late, but I FINALY got time to update this question.  The answer proposed was NOT correct.  Here is the correct code, albeit in java:

public class Diamond
{      

     public static void main(String [] args)
     {    
          boolean outer=true;
          int line, star, space;
        int SIZE=4;  
         
        //UPPER PART
       for (line = 1; line <= SIZE; line ++)
       {
            for (star = 0;star < line;star ++)
            {     if(outer)
                 {
                      for(space = SIZE - line;space >= 0;space --)
                      {     System.out.print(" ");
                           outer = false;
                      }
                 }
                 else
                 {     System.out.print(" ");
                 }
                 System.out.print("*");                
            }
           
            System.out.println();
            outer = true;
       }
       
       //LOWER PART
       
       for (line = SIZE - 1;line > 0;line --)
       {  
            for (star = 0;star < line;star ++)
            {     if(outer)
                 {
                      for(space = line - 1;space < SIZE;space ++)
                      {     System.out.print(" ");
                           outer = false;
                      }
                 }
                 else
                 {     System.out.print(" ");
                 }
                 System.out.print("*");
            }
           
            System.out.println();
            outer = true;
       }
     }
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now