quick and simple

B

Bruce Wiggins

I did not put much thought into this but it works.

int i;
int x;
int j=4;

for(i=0; i < 9; i+=2)
{
for(x=0; x < j; x++) printf(" ");
j--;
for(x=0; x <= i; x++)
printf("*");
printf("\n");
}

j=0;
for(i=9; i > 0; i-=2)
{
for(x=0; x < j; x++) printf(" ");
j++;
for(x=0; x < i; x++)
printf("*");
printf("\n");
}


output:

*
***
*****
*******
*********
*********
*******
*****
***
*
 
H

Heikki Kallasjoki

int i;
int x;
int j=4;

for(i=0; i < 9; i+=2)
{
for(x=0; x < j; x++) printf(" ");
j--;
for(x=0; x <= i; x++)
printf("*");
printf("\n");
}

j=0;
for(i=9; i > 0; i-=2)
{
for(x=0; x < j; x++) printf(" ");
j++;
for(x=0; x < i; x++)
printf("*");
printf("\n");
}

I can't quite see the point of this (a reference to some other thread?),
but for some inexplicable reason couldn't resist applying some recursion
to it:

#include <stdio.h>

void d1(int s, int n) {
if (s) {
putchar(' ');
d1(s-1, n);
putchar(' ');
} else if (n) {
putchar('*');
d1(s, n-1);
}
}

void d2(int s, int n) {
d1(s, n); putchar('\n');
if (s) d2(s-1, n+2);
d1(s, n); putchar('\n');
}

int main(void) {
d2(4, 1);
return 0;
}
 
A

August Karlstrom

I did not put much thought into this but it works.
[...]

Here is a shorter solution:

#include <stdio.h>

int main(void)
{
const int n = 9;
int i, j, inside;

for (i = 0; i < n + 1; i++) {
for (j = 0; j < n; j++) {
inside = (i + j >= n / 2) && (i + j < 3 * n / 2 + 1)
&& (j - i >= -n / 2 - 1) && (j - i < n / 2 + 1);
printf("%c ", inside? '*': ' ');
}
printf("\n");
}
return 0;
}


August
 
L

lawrence.jones

August Karlstrom said:
I did not put much thought into this but it works.
[...]

Here is a shorter solution:
[...]

#include <stdio.h>

#define ABS(i) ((i) < 0 ? -(i) : (i))

int main()
{
int i;

for (i = -4; i <= 4; i++)
printf("%.*s\n", 9 - ABS(i), " *********" + 4 - ABS(i));
return 0;
}
 
A

August Karlstrom

#include<stdio.h>

#define ABS(i) ((i)< 0 ? -(i) : (i))

int main()
{
int i;

for (i = -4; i<= 4; i++)
printf("%.*s\n", 9 - ABS(i), " *********" + 4 - ABS(i));
return 0;
}

Nice! Didn't know printf had these formatting capabilities. But you
don't get the same output as the OP with this program.


August
 
M

Mark

August Karlstrom said:
Nice! Didn't know printf had these formatting capabilities. But you
don't get the same output as the OP with this program.

for (i=-5; i<=5; i++)
if(i)
printf("%.*s\n", 10 - ABS(i), " **********" + 5 - ABS(i));
 
L

lawrence.jones

August Karlstrom said:
Nice! Didn't know printf had these formatting capabilities.

That was only the half of it...this version goes all the way:

#include <stdio.h>

#define ABS(i) ((i)< 0 ? -(i) : (i))

int main()
{
int i;

for (i = -4; i<= 4; i++)
printf("%*.*s\n", 9 - ABS(i), 9 - 2 * ABS(i), "*********");
return 0;
}
But you don't get the same output as the OP with this program.

I agree with the other poster who said that it looks better without the
duplicated line, but it's an easy change if you want it.
 
B

bart.c

Bruce Wiggins said:
I did not put much thought into this but it works.

int i;
int x;
int j=4;

for(i=0; i < 9; i+=2) ....

for(i=9; i > 0; i-=2) ....

output:

*
***
*****
*******
*********
*********
*******
*****
***
*

This version doesn't use any loops or variables, and avoids that duplicated
middle line:

#include <stdio.h>

int main(void)
{
puts(" *");
puts(" ***");
puts(" *****");
puts(" *******");
puts("*********");
puts(" *******");
puts(" *****");
puts(" ***");
puts(" *");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top