Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #69042

Re: Program to print a pattern of hashtags: issues with spaces

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Program to print a pattern of hashtags: issues with spaces
Date 2015-09-02 15:50 -0700
Organization None to speak of
Message-ID <lna8t4sag6.fsf@kst-u.example.com> (permalink)
References <2ba87e0d-d563-4890-9e8f-a8a64916f266@googlegroups.com> <GIBFx.283710$nx.263775@fx16.am4>

Show all headers | View raw


Richard Heathfield <rjh@cpax.org.uk> writes:
[good stuff snipped]
> So the width is the same as the height, yes? So what we can do is *carry 
> on counting along the columns*, like this:
>
> for(row = 0; row < height; row++)
> {
>    for(column = 0; column < row; column++)
>    {
>      putchar(' '); /* no need to call printf here - putchar places a 
> single, known character on the standard output stream */
>    }
>    for(          ; column < height; column++)
>    {
>      putchar('#');  /* by the way, this isn't a hashtag - it's just a 
> hash (or a pound, or an octothorpe) */
>    }
>    putchar('\n');
> }
[more good stuff snipped]

An alternative is to have a single inner loop that prints all the ' '
characters for a row followed by all the '#' characters for that row:

    for (column = 0; column < height; column ++) {
        if (column < row) {
            putchar(' ');
        }
        else {
            putchar('#');
        }
    }
    putchar('\n');

The inner if/else could also be written as:

    putchar(column < row ? ' ' : '#');

I personally find this clearer than the two inner loops.  YMMV.

Your solution has a possible performance advantage in that it doesn't
test the condition (column < row) on each column.  The difference
in run time, if any (optimizing compilers can do very clever things
with loops) is very unlikely to be relevant.  If it is, you should
actually measure the performance before deciding which approach
to use.  On the other hand, the fact that it doesn't do the test
repeatedly argues that your solution is *conceptually* cleaner
in some ways.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-02 04:38 -0700
  Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-02 13:12 +0100
    Re: Program to print a pattern of hashtags: issues with spaces Gareth Owen <gwowen@gmail.com> - 2015-09-02 20:25 +0100
    Re: Program to print a pattern of hashtags: issues with spaces Keith Thompson <kst-u@mib.org> - 2015-09-02 15:50 -0700
      Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 02:07 -0700
      Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 02:08 -0700
    Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 00:26 -0700
      Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 09:16 +0100
        Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-05 04:35 -0700
          Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-05 13:08 +0100
            Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-05 05:48 -0700
          Re: Program to print a pattern of hashtags: issues with spaces Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-05 14:56 +0100
        Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-05 05:46 -0700
          Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-05 14:03 +0100
            Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-05 08:55 -0700
  Re: Program to print a pattern of hashtags: issues with spaces bartekltg <bartekltg@gmail.com> - 2015-09-02 14:39 +0200
  Re: Program to print a pattern of hashtags: issues with spaces Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-02 13:55 +0100
    Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 01:51 -0700
      Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 10:01 +0100
        Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 02:10 -0700
      Re: Program to print a pattern of hashtags: issues with spaces Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-03 16:10 +0100
      Re: Program to print a pattern of hashtags: issues with spaces Keith Thompson <kst-u@mib.org> - 2015-09-03 09:12 -0700
  Re: Program to print a pattern of hashtags: issues with spaces Barry Schwarz <schwarzb@dqel.com> - 2015-09-02 11:52 -0700
    Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-02 20:24 +0100
      Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 02:06 -0700
        Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 10:36 +0100
          Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 03:10 -0700
      Re: Program to print a pattern of hashtags: issues with spaces Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 13:45 -0700
    Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 02:04 -0700
  Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 00:31 -0700
  Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 00:49 -0700
    Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 09:23 +0100
      Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 02:33 -0700
        Re: Program to print a pattern of hashtags: issues with spaces Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 10:39 +0100
          Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-03 03:11 -0700
  Re: Program to print a pattern of hashtags: issues with spaces Alla _ <modelling.data@gmail.com> - 2015-09-05 04:40 -0700

csiph-web