Drawing a Triangle

This is a discussion on Drawing a Triangle within the C++ Programming forums, part of the General Programming Boards category; I have a program that draws triangles however Ia mhavig trouble doing the part for case 3 a triangle that ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Drawing a Triangle

    I have a program that draws triangles however Ia mhavig trouble doing the part for case 3 a triangle that follows this example
    *
    **
    ***
    ****
    ***
    **
    *
    I understand that I to divide the height in half. However, integer division by 2 will result in a midpoint 1 less than the actual midpoint. For example: 5 / 2 = 2 (Midpoint = 2 + 1 = 3) but I am stumped on how to code this plz help
    Code:
    #include<iostream>usingnamespace std;
    int main()
    {
            int no_lines,// height of triangle.
                    width;// width of rectangle.
            short choice;
            // ADDITIONAL VARIABLES
      
            // ADD A do-while LOOP TO THE PROGRAM SO THAT THE USER CAN REPEATEDLY
            // DISPLAY THE MENU, MAKE A CHOICE, AND HAVE THE APPROPRIATE STEPS FOR
            // THAT CHOICE CARRIED OUT.
            // THE LOOP SHOULD CONTINUE ITERATING UNTIL THE USER ENTERS 3 FOR THE MENU CHOICE.
            // HAVE THE PROGRAM PRINT SEVERAL BLANK LINES AFTER EACH SHAPE IS DRAWN.
                    do
                    {
                    // Display menu.  
                    cout <<"THIS PROGRAM DRAWS TRIANGLES.\n"<< endl;
                    cout <<"1. Filled Triangle"<< endl;
                    cout <<"2. Filled Inverted Trianlge"<< endl;
                    cout <<"3. Exit\n"<< endl;
                    cin >> choice;
                    // ASK, READ AND VALIDATE CHOICE WITH while LOOP.
              
                    // Process choice.
                    switch(choice)
                    {
                               case1:// Filled Triangle
                                            // Ask, read and validate number of lines.
                                            cout <<"Enter the height for the filled triangle (2 - 25): ";
                                            cin >> no_lines;
                                            while(no_lines <2|| no_lines >25)
                                            {
                                                      cout <<"Number of lines has to be between 2 and 25.\n";
                                                      cout <<"Enter the height for the filled triangle (2 - 25): ";
                                                      cin >> no_lines;
                                            }
                                            cout << endl;
    // Draw shape.
    // An example of a filled triangle with height 5
    //                                              *
    //                                              **
    //                                              ***
    //                                              ****
    //                                              *****
                                            int i, j;
                                            for(i =1; i <= no_lines; i++)
                                            {
                                                    for(j =1; j <= i; j++)
                                                            cout <<"*";
                                                    cout << endl;
                                            }
                                            break;
                                            case2:// Inverted filled triangle
                                            cout <<"Enter the height for the triangle (2 - 25): ";
                                            cin >> no_lines;
                                            while(no_lines <2|| no_lines >25)
                                            {
                                                      cout <<"Number of lines has to be between 2 and 25.\n";
                                                      cout <<"Enter the height for the triangle (2 - 25): ";
                                                      cin >> no_lines;
                                            }
                                            cout << endl;
    // ASK, READ AND VALIDATE HEIGHT.
    // DRAW INVERTED TRIANGLE
    // AN EXAMPLE OF AN INVERTED TRIANGLE OF HEIGHT 5
    //                                              *****
    //                                              ****
    //                                              ***
    //                                              **
    //                                              *
    
                                             for(i = no_lines; i >=1; i--)
                                             {
                                             for(j =1; j <= i; j++)
                                             cout <<"*";
                                             cout << endl;
                                             }
                                            break;
                                            case3:
    // ASK, READ AND VALIDATE HEIGHT.
    // DRAW THE THIRD TRIANGLE
    //                                              *
    //                                              **
    //                                              ***
    //                                              **
    //                                              *
    
                                            cout <<"Exiting ...\n";
                                            return0;
                                            break;
                                            }
                                            }
    while(choice >=1&& choice <=3);
            system("pause");
                   return0;
    }
      
    

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    You cant work out how to add 1 to the result of h / 2?? Maybe this is a start, for what you are looking for - filling in the blanks should not be too hard after already accomplishing the other cases.
    Code:
    void DrawTriangle(int h)
    {	
    	int mid= (h/2) + 1;
    	int len=0
    	
    	for (int i = 0; i < h; i++)
    	{
    		//calculate len here
    		
    		// loop to print "*" here { }
    		
    		cout << "\n";
    	}		
    }
    Last edited by rogster001; 04-24-2012 at 07:05 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Triangle
    By BB18 in forum C Programming
    Replies: 14
    Last Post: 10-16-2004, 10:59 PM
  2. Drawing a triangle with hatch marks?
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2003, 05:42 AM
  3. OpenGL Question...simple (triangle drawing stuff)
    By incognito in forum Game Programming
    Replies: 7
    Last Post: 03-15-2003, 07:47 PM
  4. triangle
    By volk in forum C Programming
    Replies: 2
    Last Post: 01-06-2003, 04:05 PM
  5. triangle of *
    By GMK in forum C++ Programming
    Replies: 16
    Last Post: 11-12-2002, 09:40 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21