Reputation: 25
OK. So I'm just trying to do some practice with simple algorithms and asterisks in C. This is not a homework problem. In my book there is a project that asks you to create a program that makes "these shapes" out of asterisks. One of them is a pyramid. While I can find numerous examples of how to make pyramid on the web, I can't find one that helps me figure out the algorithm for just the border of one. And I have not seen one on here either.
Now here is the code I have already.
#include <stdio.h>
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 1; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
for( j = 1; j <= 2*i-1; j++)
{
printf("*");
}
printf("\n");
}
for ( i = 6; i < 10; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
for( j = 1; j <= i+1-3*h; j++)
{
printf("*");
}
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
When you run the program it should, since I've ran it numerous times with no errors, give you this shape:
....*
...***
..*****
.*******
*********
.*******
..*****
...***
....*
I keep thinking that the loop that has the asterisk would probably need more loops to determine the location of the spacing between the asterisks. But I can't really begin to think of how to do this. What I want to end up with is this:
....*
...*.*
..*...*
.*.....*
*.......*
.*.....*
..*...*
...*.*
....*
Any help with this would be greatly appreciated. I am a beginner programmer. In my first University level programming class. We're only learning C in this class. We've learned up to Multidimensional Arrays, and started learning functions on Thursday.
Upvotes: 1
Views: 2172
Reputation: 46415
Here is a hint. You want the "first and last *" - so look at the loop
for( j = 1; j <= 2*i-1; j++)
{
printf("*");
}
And change it to something like:
printf("*"); // first *
for( j = 2; j<= 2*i-2; j++) printf(" "); // spaces until...
printf("*"); // last *
See how you get on with that… this is not the only place where you need to make this change.
EDIT in response to the request by @KalaJ I provide complete code - once with the first and last star, and once without. You can pick which one you want to use by changing the condition in the #if 0
preprocessor directive. If you set it to #if 1
you get the original pattern.
#include <stdio.h>
#if 0
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 1; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
printf("*");
for( j = 2; j <= 2*i-2; j++)
{
printf(" ");
}
if(i>1) printf("*");
printf("\n");
}
for ( i = 6; i < 10; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
printf("*");
for( j = 1; j <= i-3*h-1; j++)
{
printf(" ");
}
if(i<9) printf("*");
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
#else
int main(void)
{
int i,j,k,x,h=0;
printf("\n\n");
for( i = 2; i <= 5; i++)
{
for( k = 1; k <=5-i; k++)
{
printf(" ");
}
printf("*");
for( j = 2; j <= 2*i-2; j++)
{
printf(" ");
}
if(i>1) printf("*");
printf("\n");
}
for ( i = 6; i < 9; i++)
{
for( k = 1; k <= i-5; k++)
{
printf(" ");
}
printf("*");
for( j = 1; j <= i-3*h-1; j++)
{
printf(" ");
}
if(i<9) printf("*");
printf("\n");
h++;
}
printf("\n\n");
return 0;
}
#endif
This produces either
* *
* *
* *
* *
* *
* *
* *
or
*
* *
* *
* *
* *
* *
* *
* *
*
Upvotes: 2
Reputation: 1970
Upvotes: 0