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;
}