Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How to draw shapes in C++?

Without using built-in functions or string functions. Just using for and/or while loops, with very basic programming...an example of a Horizontal Line would be this (to give you an idea):

while (i < line + 1)

{

cout << shapesym;

i++;

}

Except I need to know how to draw a triangle like this

__#

_##

###

With _ as a blank line, and a diamond like this:

__#__

_###_

#####

_###_

__#__

Also with _ as a blank line.

Please, can anyone help? I am having a lot of trouble figuring out how to make these shapes, even though I understand looping...it's the drawing part with the loop that is throwinng me off, I can't get anything to turn out right...

Update:

Thanks so much guys - let me just add that that was just an example, the height of the triangle could be different every single time because it is input from a document that we haven't seen...so I can't just put the symbols and spaces in. I wish I could though! And as a beginning computer science student I have no idea what those complex terms mean :( If anyone has a simpler way of explaining it that would be much appreciated...thanks...

Update 2:

V Ronin - I tried your coding and it doesn't work, the output is:

__#

__#

__#

Which is a lovely vertical line - but not the triangle that I need.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    triangle: cout << " #\n ##\n###";

    diamond: cout << " #\n ###\n#####\n ###\n #";

    if you want to specify the number of lines for each shape:

    triangle:

    for (int i=1;i<=lines;i++){

    for (int j=lines;j>=1;j--){

    if (j>i){cout << " ";}

    else {cout << "#";}

    }

    cout << "\n";

    }

    Edit: I tried this code:

    #include <iostream>

    using namespace std;

    int main(){

    int lines=3;

    for (int i=1;i<=lines;i++){

    for (int j=lines;j>=1;j--){

    if (j>i){cout << " ";}

    else {cout << "#";}

    }

    cout << "\n";

    }

    cin >> lines;

    }

    and it works fine for the triangle! now I'll write you the code for the diamond

  • Bill
    Lv 4
    1 decade ago

    There are several ways to do this. I think the easiest way would be for you to create a 2 dimensional character matrix and use that as a basic euclidean coordinate map and graph the shape out. Then when you are finished just loop through your matrix and print it to the screen.

  • 1 decade ago

    hmm i think i am in the same class as you. i live in vegas and go to unlv. I havent started on the assignment yet, still need to do the first line. Things do on monday lol. Maybe i will be able to help when i get going, im just about to open up my book now.

Still have questions? Get your answers by asking now.