?
Solved

draw a diamond

Posted on 2002-05-07
8
Medium Priority
?
6,898 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
Independent Software Vendors: 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!

 

Author Comment

by:blacksteel4life
ID: 6993966
Will try a different approach
0
 
LVL 6

Accepted Solution

by:
snoegler earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

757 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