# quick and simple

Discussion in 'C Programming' started by Bruce Wiggins, May 12, 2010.

1. ### Bruce WigginsGuest

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:

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

Bruce Wiggins, May 12, 2010

2. ### Heikki KallasjokiGuest

On 2010-05-12, Bruce Wiggins <> wrote:
> 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;
}

--
Heikki Kallasjoki
email: echo 'zfs+es_t_i@n_u.zf' | tr zen_muftis fuze_mints

Heikki Kallasjoki, May 12, 2010

3. ### August KarlstromGuest

On 2010-05-12 09:44, Bruce Wiggins wrote:
> 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

August Karlstrom, May 12, 2010
4. ### Guest

August Karlstrom <> wrote:
> On 2010-05-12 09:44, Bruce Wiggins wrote:
> > 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;
}
--
Larry Jones

I don't NEED to compromise my principles, because they don't have
the slightest bearing on what happens to me anyway. -- Calvin

, May 12, 2010
5. ### August KarlstromGuest

On 2010-05-12 18:20, wrote:
> #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

August Karlstrom, May 12, 2010
6. ### MarkGuest

August Karlstrom <> wrote:
> On 2010-05-12 18:20, wrote:
>> #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.

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

Mark, May 12, 2010
7. ### Guest

August Karlstrom <> wrote:
>
> 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.
--
Larry Jones

He just doesn't want to face up to the fact that I'll be
the life of every party. -- Calvin

, May 13, 2010
8. ### bart.cGuest

"Bruce Wiggins" <> wrote in message
news:...
>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(" *");
}

--
Bartc

bart.c, May 13, 2010