Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Program to print a pattern of hashtags: issues with spaces |
| Date | 2015-09-02 13:55 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <874mjdj7zx.fsf@bsb.me.uk> (permalink) |
| References | <2ba87e0d-d563-4890-9e8f-a8a64916f266@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
> Please, take a look at this simple
> program that is supposed to print out
> a pattern of the following kind:
>
> ####
> ###
> ##
> #
>
> Unfortunately, it doesn't produce
> the correct result due to the problem
> with spaces.
This won't help you directly because the solution I'm going to show is
not the way your book wants you to do it, but you might, nevertheless,
find it interesting to see another approach.
I look at that pattern and I ask myself what function can I imagine
having that would make it really easy to get that pattern? I know I
must make the patter line by line, so if I could call a function that
could generate the ith line all I'd need is
int main(void)
{
int pattern_size;
if (scanf("%d", &pattern_size) == 1)
for (int i = 0; i < pattern_size; i++)
output_line(i);
}
(I'm leaving off #include's for the moment and, since your book uses
C99, I'm using two features of C99 I like -- omitting the return from
main when it's just return 0, and declaring a loop counter in the loop
itself.)
Now I can forget about that part and ponder whether I can write
void output_line(int i)
{
....
}
So I ask myself, again, what function would making writing line number i
really easy? Well, line 0 has 0 spaces and pattern_size hashes, and
line 1 has 1 space and one fewer (pattern_size - 1) hashes.
Immediately there's a problem: output_line needs to know the pattern
size. But that's easy to fix: and you do that by passing it as another
argument:
int pattern_size;
if (scanf("%d", &pattern_size) == 1)
for (int i = 0; i < pattern_size; i++)
output_line(i, pattern_size);
Inside output_line I can choose to call this argument anything I like,
but, in order to make the point that it's a new variable in the
function, I'm going to call it 'length'. That makes sense because in
the context of a line, it's the length of the line that determines what
we have to output. So we need to write the function
void output_line(int i, int length)
{
....
}
This function must print length characters in total but i of them are
spaces. What function would be really helpful in writing output_line?
Well, a function to print some given number of a particular character
would let me write:
void output_line(int i, int length)
{
repeat(i, ' ');
repeat(length - i, '#');
putchar('\n');
}
(putchar may be new to you -- it prints one character).
So a new problem emerges. Can I write
void repeat(int n, char c)
{
}
Yes, I think I can and I don't think a new function will help much.
It's just a for loop:
void repeat(int n, char c)
{
for (int i = 0; i < n; i++)
putchar(c);
}
So, the whole thing is:
#include <stdio.h>
void repeat(int n, char c)
{
for (int i = 0; i < n; i++)
putchar(c);
}
void output_line(int i, int length)
{
repeat(i, ' ');
repeat(length - i, '#');
putchar('\n');
}
int main(void)
{
int pattern_size;
if (scanf("%d", &pattern_size) == 1)
for (int i = 0; i < pattern_size; i++)
output_line(i, pattern_size);
}
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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