Solved

draw a diamond

Posted on 2002-05-07
8
6,882 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

760 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

22 Experts available now in Live!

Get 1:1 Help Now