Solved

Diamond

Posted on 1998-02-24
5
1,360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 3

Author Comment

by:Dmitriy
ID: 1182779
Edited text of question
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 1182780
Edited text of question
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 1182781
Edited text of question
0
 
LVL 3

Accepted Solution

by:
q2guo earned 0 total points
ID: 1182782
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
ID: 7252281
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

691 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