Solved

draw a diamond

Posted on 2002-05-07
8
6,883 Views
Last Modified: 2012-08-14
Thanks Snoegler.

Reply:
I was hoping someone could provide some insight as to how I can solve this, not write the code for me. This is my code, but the final result is a half diamond, not a full diamond.

#include <iostream>
#include <stdlib.h>
int main()
{
int size;
int i;
int asterix;
cout << "Enter size: 1-80" << endl;
cin >> size;

if (size > 0 && size < 80)
{

     for (int i = 0; i < size; i++)
     {
          int asterix = ++i;
          for (; asterix != 0; asterix--)
          {
               cout << '*';
          }
          cout << endl;
     }
     
     for (i = size; i > 0; i--)
     {
          int asterix = --i;
          for (; asterix != 0; asterix--)
          {
               cout << '*';
          }
          cout << endl;
     }
}


      system("PAUSE");
      return 0;
}


i want to Write a C++ program that will print a diamond(filled with asterisks--see below) using for while and do-while loops in each case(loop)with the size of the diamond should be detemined by the user when the program starts . The size may be any positive odd integer between 1 and 79.


              *
             ***
            *****
           *******
            *****
             ***
              *
             
0
Comment
Question by:blacksteel4life
8 Comments
 
LVL 2

Expert Comment

by:jonnin
ID: 6993780
int main()
{
cout << "just give me an 'F', I cheated on this homework but now I feel bad about it\n" << endl;
}



0
 
LVL 11

Expert Comment

by:griessh
ID: 6993827
Somebody should build a "diamond class library". This question comes over and over again <LOL>. I am just so mad they asked me to program that in C++.

blacksteel4life
Don't expect too much. Many experts here in EE are not willing to write code for your homework. We are all wiliing to help you if you have specific questions about your problem, but you have to show some effort, too.

======
Werner
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6993954
If you've got a half diamond, then ... determine if the number of total lines of the diamond is odd or even. If even, simply use the existing code in reverse direction. If odd, do the same but skip the first printed row.
0
 

Author Comment

by:blacksteel4life
ID: 6993966
Will try a different approach
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Accepted Solution

by:
snoegler earned 50 total points
ID: 6994028
#include <stdio.h>
#include <iostream>
using namespace std;

#define c(x) (cout << x)
#define     l(s,w) {for(int z=0;z<s;z++) c(' ');for(z=0;z<w;z++) c('*');c(endl);}

void     diamond(int w)
{
     for(int i=2-(w&1);i<=w-((w&1)?2:0);i+=2) l((w-i)/2,i);
     for(i-=(w&1)?0:2;i>=2-(w&1);i-=2) l((w-i)/2,i);
}
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6994042
This was more a joke than a solution (you won't present this to your teacher, will you?).
BTW: Please post comments instead of editing the question.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6994101
Yes, please post comments instead of editing!

You need 2 nested loops.

The outer loop for the lines ... how many lines do you need to print a diamond with size *s?
The inner loop for the chars ... you have to print ' ' and '*' ... how many ' '/'*' do you need for every line?

The ' ' is the part that's missing in your code.

FInd these numbers and then come back ...

Good luck!

======
Werner

0
 
LVL 11

Expert Comment

by:griessh
ID: 7178620
Dear blacksteel4life

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to split betwen

     "griessh and snoegler"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now