Thread: C program to draw a pyramid

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    7

    C program to draw a pyramid

    Program accepts 3 integer inputs and I want to draw a pyramid from the side view.
    Using the formula for a slope of a line, I end up with two formulas that give the coordinates for the outermost edge of the pyramid at each of its levels.
    A hash symbol needs to appear at the edges, top and base with dots everywhere else. My current program is trying to print the pyramid upside down (the loop is supposed to be easier to figure out this way) but eventually I need to make it upright.
    What happens now is that the base gets printed properly but then I get an infinite loop of dots starting at the next level after it prints an initial hash to mark the edge. Here is my code:

    Code:
    #include<stdio.h>
    #include<math.h>
    //upside down pyramid
    int main(void)
    {
        int x = 0, y = 0, z = 0;//dimensions
        scanf("%d %d %d", &x, &y, &z);
        char hash = '#';
        char dot = '.';
        int j = 0;
        for (j = 0; j < 1; j++)//print base first
        {
            for (int i = 0; i < x; i++)
                printf("%c", hash);
            printf("\n");
        }
        
        for (j = 1; j < z; j++)//second to z levels of pyramid
        {
            int right = ceil((x - 1) + (-j)*(x / (2.0*z)));//right line coordinates
            int left = (floor(j*(x / (2.0*z))));//left line coordinates
            int ledge = left;//left edge
            int redge = right;//right edge
            int mid = 0;//middle space
            
            for (ledge = ledge; ledge < (ledge + 1); ledge++);//first #
                printf("%c", hash);
            for (mid = (ledge+ 1); mid < redge; mid++)//dots in between
                printf("%c", dot);
            //for (redge = redge; redge <= redge; redge++)//right hand edge #
            printf("%c", hash);
            printf("\n");//new line to next level
        }
        return 0;
    }

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Do you mean to draw a triangle? A pyramid is a 3D structure, and cannot faithfully be rendered using this technique.

    You want to render something like this?

    Code:
                 #
                #.#
               #...#
              #.....#
             #.......#
            ###########
    Last edited by MacNilly; 05-20-2017 at 12:01 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A quick gdb lesson.
    Code:
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) list
    1	#include<stdio.h>
    2	#include<math.h>
    3	//upside down pyramid
    4	int main(void)
    5	{
    6	    int x = 0, y = 0, z = 0;//dimensions
    7	    scanf("%d %d %d", &x, &y, &z);
    8	    char hash = '#';
    9	    char dot = '.';
    10	    int j = 0;
    (gdb) 
    11	    for (j = 0; j < 1; j++)//print base first
    12	    {
    13	        for (int i = 0; i < x; i++)
    14	            printf("%c", hash);
    15	        printf("\n");
    16	    }
    17	    
    18	    for (j = 1; j < z; j++)//second to z levels of pyramid
    19	    {
    20	        int right = ceil((x - 1) + (-j)*(x / (2.0*z)));//right line coordinates
    (gdb) 
    21	        int left = (floor(j*(x / (2.0*z))));//left line coordinates
    22	        int ledge = left;//left edge
    23	        int redge = right;//right edge
    24	        int mid = 0;//middle space
    25	        
    26	        for (ledge = ledge; ledge < (ledge + 1); ledge++);//first #
    27	            printf("%c", hash);
    28	        for (mid = (ledge+ 1); mid < redge; mid++)//dots in between
    29	            printf("%c", dot);
    30	        //for (redge = redge; redge <= redge; redge++)//right hand edge #
    (gdb) b 28
    Breakpoint 1 at 0x4008b3: file foo.c, line 28.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    2 3 4
    ##
    
    Breakpoint 1, main () at foo.c:28
    28	        for (mid = (ledge+ 1); mid < redge; mid++)//dots in between
    (gdb) info locals
    ledge = 2147483647
    redge = 1
    right = 1
    left = 0
    mid = 0
    x = 2
    y = 3
    z = 4
    hash = 35 '#'
    dot = 46 '.'
    j = 1
    You might think about
    1. why ledge is so large.
    2. what ledge < (ledge + 1) actually means
    3. why there is a ; at the end of line 26
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Or consider why

    Code:
    for (j = 0; j < 1; j++)//print base first
    is a "loop" that loops only once?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ c++] Pyramid Shape By me
    By Ibraheem Salem in forum C++ Programming
    Replies: 0
    Last Post: 12-18-2012, 06:46 PM
  2. Draw Program Not Working
    By Benji Wiebe in forum Windows Programming
    Replies: 2
    Last Post: 06-11-2011, 08:15 PM
  3. how make this all pyramid in c++ 4.5
    By shah18 in forum C Programming
    Replies: 5
    Last Post: 10-04-2010, 11:34 AM

Tags for this Thread