Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Creating a pyramid using C++

  1. Mar 7, 2006 #1
    Hello,
    I am trying to print an equilateral triangle, made up of * (stars), of height 8. Can someone please give me some hint. I am not getting the desired output. Thank you. Here is my code.

    Code (Text):

    #include<iostream.h>
    #include<iomanip.h>
    void main()
    {
      int n=8;


         for (int row=1; row<=8; row++)
         {
              for (int col=0; col<row; col++)
              {

                    cout<<setw(n=n-1)<< '*';

              }
              cout << endl;  // end the line.
         }

    }
     
     
    Last edited: Mar 7, 2006
  2. jcsd
  3. Mar 7, 2006 #2
    ew c++ io commands, can't remmeber what the setw command does so it may be best for you to post the errored output you got
    ...and just printing stars you won't get a real equilateral triangle so i'm guessing your looking for 8 stars each edge.

    also are you filling the triangle?

    and you can use n-- rather then n=n-1...doesn'treallly matter but n-- looks nicer.

    my best guess though is your forgetting to print spaces.
     
  4. Mar 7, 2006 #3

    jtbell

    User Avatar

    Staff: Mentor

    Please show us exactly what this triangle is supposed to look like, by literally typing it out inside a (code)(/code) block. (square brackets instead of parentheses, of course).
     
  5. Mar 7, 2006 #4
    Code (Text):
    #include<iostream.h>

    main()
    {
      int n=8;

         cout<<"45,90,45 degree triangle"<<endl;
         for (int y=1; y<=8; y++)
         {
            for(int space=8-y; space>0; space--)
            {
                cout<<" ";
            }  
            for (int x=0; x<y; x++)
            {
                cout<<"**";
            }        
            cout << endl;  // end the line.
        }

        cout<<endl<<"circle"<<endl;
        for (double y=-10; y<=10; y++)
        {
            for (double x=-10; x<=10; x++)
            {
                if(x*x+y*y<=100)
                    cout<<"*";
                else
                    cout<<" ";
            }      
            cout << endl;  // end the line.
        }

        cout<<endl<<"Equilateral Triangle"<<endl;
        for (double y=7*sqrt(3); y>=0; y--)
        {
            for (double x=-7; x<=7; x++)
            {
            if(x==0)
                cout<<"*";
            else if(x<0&&y/(x+7)<=sqrt(3))
                cout<<"*";
            else if(x>0&&y/(7-x)<=sqrt(3))
                cout<<"*";
            else
                cout<<" ";
            }      
            cout << endl;  // end the line.
        }
        system("PAUSE");
    }
     

    Attached Files:

    Last edited: Mar 7, 2006
  6. Mar 7, 2006 #5
    Thanks for the help
     
  7. Mar 8, 2006 #6
    Your Welcome. :smile:
     
  8. Mar 8, 2006 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Please don't do people's homework for them. :grumpy:
     
  9. Mar 16, 2006 #8
    something wrong

    when i tried to run the programm it had some problems.this is how i change it but it works

    #include<iostream.h>
    #include <cmath>//the first change
    int main()
    {
    int n=8;


    cout<<"45,90,45 degree triangle"<<endl;
    for (int y=1; y<=8; y++)
    {
    for(int space=8-y; space>0; space--)
    {
    cout<<" ";
    }
    for (int x=0; x<y; x++)
    {
    cout<<"**";
    }
    cout << endl; // end the line.
    }

    cout<<endl<<"circle"<<endl;
    for ( y=-10; y<=10; y++)//second change
    {
    for (double x=-10; x<=10; x++)
    {
    if(x*x+y*y<=100)
    cout<<"*";
    else
    cout<<" ";
    }
    cout << endl; // end the line.
    }

    cout<<endl<<"Equilateral Triangle"<<endl;
    for (y=7*sqrt(3); y>=0; y--)//third change
    {
    for (double x=-7; x<=7; x++)
    {
    if(x==0)
    cout<<"*";
    else if(x<0&&y/(x+7)<=sqrt(3))
    cout<<"*";
    else if(x>0&&y/(7-x)<=sqrt(3))
    cout<<"*";
    else
    cout<<" ";
    }
    cout << endl; // end the line.
    }

    return 0;//fourth change

    }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?