Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #68988 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-09-02 04:38 -0700 |
| Last post | 2015-09-05 04:40 -0700 |
| Articles | 20 on this page of 36 — 8 participants |
Back to article view | Back to comp.lang.c
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
Page 1 of 2 [1] 2 Next page →
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-02 04:38 -0700 |
| Subject | Program to print a pattern of hashtags: issues with spaces |
| Message-ID | <2ba87e0d-d563-4890-9e8f-a8a64916f266@googlegroups.com> |
Hello!
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.
Please, see my comments within the program:
#include <stdio.h>
int main(void)
{
int row, column, hash;
int number, spaces;
int height = 9;
number = height;
spaces = height - number;
hash = height;
/** I introduced the variable number, and
spaces equal height - number, which
is decremented by one with each
iteration of the loop: 1 row has
zero spaces, second has one space
(spaces = 9 - 8), etc.
but somehow this algorithm doesn't
work.
**/
for (row = 1; row <= height; row++)
{
for (column = 1; column <= spaces; column++)
printf (" ");
number--;
for (column = 1; column <= hash; column++)
printf ("# ");
hash--;
printf ("\n");
}
return 0;
}
The result looks like this:
Please, enter the height
9
# # # # # # # # #
# # # # # # # #
# # # # # # #
# # # # # #
# # # # #
# # # #
# # #
# #
#
The program doesn't compute spaces in a
desired way.
Thank you!
[toc] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-02 13:12 +0100 |
| Message-ID | <GIBFx.283710$nx.263775@fx16.am4> |
| In reply to | #68988 |
On 02/09/2015 12:38, Alla _ wrote:
> Hello!
>
> Please, take a look at this simple
> program that is supposed to print out
> a pattern of the following kind:
>
> ####
> ###
> ##
> #
Okay, before we look at your program, let's start off by designing an
algorithm to achieve the desired output.
We'll start by defining the concept of "height", which will be the
number of rows we want. So we can think about a loop such as:
for(row = 0; row < height; row++)
Up until now, you have been using loops that run from 1 to N. That is
actually a very unnatural way for C to do loops. It is vastly more
common for C loops to run from 0 to N-1. This may seem unusual, but it's
amazing how often it turns out to be the Right Thing.
Okay, so in our loop we want to print out some number of spaces. 0 in
the 0th row, 1 in the 1th row, 2 in the 2th row, and so on. Well, that's
easy:
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 */
}
}
I deliberately chose "column" rather than "space". Why? Well, we'll see
in a moment.
Okay, so what's next? Well, the important thing to realise about this
problem is that what we're actually printing is a square. This becomes
more obvious if we use _ instead of space (just to make the spaces
visible!). What we want is:
#####
_####
__###
___##
____#
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');
}
Let's wrap this into a program and see what we get:
#include <stdio.h>
int main(void)
{
int row;
int column;
int height = 9;
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++) /* SEE NOTE BELOW */
{
putchar('#');
}
putchar('\n');
}
return 0;
}
NOTE: do you see, in the second of the inner loops, how I've left a
great big space where you'd expect to see somethingorother=0?
In C, all of the for() construct's three elements are *optional*. You
have to have the semicolon separators, but you don't actually need to
put anything else. for(;;) is perfectly legal (albeit not very helpful
in /my/ opinion, but others here will disagree and say it's really handy).
An empty first step means "don't bother doing this bit", an empty second
step is the same as if you'd written a true statement, and an empty
third step means "don't bother doing this bit".
In this case, when this loop starts, we want the column object to have
what value? Well, *exactly* the value that it already has - the number
of the column we're on. This loop then takes up the strain and moves
column along the rest of the way along the row.
I could have written it like this (and normally do):
for( ; column < height; column++)
Okay, here's the output:
#########
########
#######
######
#####
####
###
##
#
So we've solved the problem. Now let's take a look at your code and see
why it didn't work:
> Unfortunately, it doesn't produce
> the correct result due to the problem
> with spaces.
>
> Please, see my comments within the program:
>
>
> #include<stdio.h>
>
> int main(void)
> {
> int row, column, hash;
> int number, spaces;
> int height = 9;
Would you believe that's twice as many objects as you need? All you
really need is row, column, height. By the way, number is a lousy name -
it could mean anything. In programming, picking good descriptive names
is half the battle.
> number = height;
> spaces = height - number;
Well, hang on - that's no different to setting spaces to 0, is it? So
why not write: spaces = 0;
And, in fact, I think I see what's coming...
> hash = height;
>
> /** I introduced the variable number, and
> spaces equal height - number, which
> is decremented by one with each
> iteration of the loop:
Well, yes, but the value of 'spaces' is now set. Just because you
decrement number, it doesn't mean that spaces will automatically adjust
itself to the new value of height - number.
> 1 row has
> zero spaces, second has one space
> (spaces = 9 - 8), etc.
>
> but somehow this algorithm doesn't
> work.
> **/
>
> for (row = 1; row<= height; row++)
> {
> for (column = 1; column<= spaces; column++)
> printf (" ");
Since spaces has the value 0, this loop will never print any spaces at all.
>
> number--;
>
> for (column = 1; column<= hash; column++)
> printf ("# ");
Why "# " (hash, space) ? In your spec, above, you don't have any spaces
between the hashes, so why introduce one?
>
> hash--;
>
> printf ("\n");
> }
> return 0;
> }
>
> The result looks like this:
> Please, enter the height
> 9
No, it doesn't, because your program doesn't have any input. You are
posting the result of a previous test...
> # # # # # # # # #
> # # # # # # # #
> # # # # # # #
> # # # # # #
> # # # # #
> # # # #
> # # #
> # #
> #
...and in fact the program you posted doesn't print the above, but this:
# # # # # # # # #
# # # # # # # #
# # # # # # #
# # # # # #
# # # # #
# # # #
# # #
# #
#
Which is direct confirmation (if it were needed) that you haven't
incremented the spaces object's value on each row.
Take a look at my version, above, and think it through. Then try it out.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Gareth Owen <gwowen@gmail.com> |
|---|---|
| Date | 2015-09-02 20:25 +0100 |
| Message-ID | <87wpw8mxn0.fsf@gmail.com> |
| In reply to | #68990 |
Richard Heathfield <rjh@cpax.org.uk> writes: > Okay, before we look at your program, let's start off by designing an > algorithm to achieve the desired output. > [massive snip] That's a terrifically well written post, Richard.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-02 15:50 -0700 |
| Message-ID | <lna8t4sag6.fsf@kst-u.example.com> |
| In reply to | #68990 |
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"
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 02:07 -0700 |
| Message-ID | <7e4dd696-3ce9-48cc-8d6f-84ef2833b163@googlegroups.com> |
| In reply to | #69042 |
On Thursday, September 3, 2015 at 2:50:12 AM UTC+4, Keith Thompson wrote:
> 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"
Thank you very much. It does look neat,
and works :) Now I know a few options
for this little program.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 02:08 -0700 |
| Message-ID | <64619c8c-3ad5-4557-9adc-caa15f08c2f0@googlegroups.com> |
| In reply to | #69042 |
On Thursday, September 3, 2015 at 2:50:12 AM UTC+4, Keith Thompson wrote:
> 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"
Thank you very much. It does look neat,
and works :) Now I know a few options
for this little program.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 00:26 -0700 |
| Message-ID | <77071dee-21c3-4bf6-aa71-714fc0652c2a@googlegroups.com> |
| In reply to | #68990 |
On Wednesday, September 2, 2015 at 4:12:40 PM UTC+4, Richard Heathfield wrote:
> On 02/09/2015 12:38, Alla _ wrote:
> > Hello!
> >
> > Please, take a look at this simple
> > program that is supposed to print out
> > a pattern of the following kind:
> >
> > ####
> > ###
> > ##
> > #
>
> Okay, before we look at your program, let's start off by designing an
> algorithm to achieve the desired output.
>
> We'll start by defining the concept of "height", which will be the
> number of rows we want. So we can think about a loop such as:
>
> for(row = 0; row < height; row++)
>
> Up until now, you have been using loops that run from 1 to N. That is
> actually a very unnatural way for C to do loops. It is vastly more
> common for C loops to run from 0 to N-1. This may seem unusual, but it's
> amazing how often it turns out to be the Right Thing.
>
> Okay, so in our loop we want to print out some number of spaces. 0 in
> the 0th row, 1 in the 1th row, 2 in the 2th row, and so on. Well, that's
> easy:
>
> 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 */
> }
> }
>
> I deliberately chose "column" rather than "space". Why? Well, we'll see
> in a moment.
>
> Okay, so what's next? Well, the important thing to realise about this
> problem is that what we're actually printing is a square. This becomes
> more obvious if we use _ instead of space (just to make the spaces
> visible!). What we want is:
>
> #####
> _####
> __###
> ___##
> ____#
>
> 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');
> }
>
> Let's wrap this into a program and see what we get:
>
> #include <stdio.h>
>
> int main(void)
> {
> int row;
> int column;
> int height = 9;
>
> 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++) /* SEE NOTE BELOW */
> {
> putchar('#');
> }
> putchar('\n');
> }
> return 0;
> }
>
> NOTE: do you see, in the second of the inner loops, how I've left a
> great big space where you'd expect to see somethingorother=0?
>
> In C, all of the for() construct's three elements are *optional*. You
> have to have the semicolon separators, but you don't actually need to
> put anything else. for(;;) is perfectly legal (albeit not very helpful
> in /my/ opinion, but others here will disagree and say it's really handy).
>
> An empty first step means "don't bother doing this bit", an empty second
> step is the same as if you'd written a true statement, and an empty
> third step means "don't bother doing this bit".
>
> In this case, when this loop starts, we want the column object to have
> what value? Well, *exactly* the value that it already has - the number
> of the column we're on. This loop then takes up the strain and moves
> column along the rest of the way along the row.
>
> I could have written it like this (and normally do):
>
> for( ; column < height; column++)
>
> Okay, here's the output:
>
> #########
> ########
> #######
> ######
> #####
> ####
> ###
> ##
> #
This is a correct output I've tried to achieve;
only spaces are missed between hashes (that's
why I had printf ("# ") :)
>
> So we've solved the problem. Now let's take a look at your code and see
> why it didn't work:
>
> > Unfortunately, it doesn't produce
> > the correct result due to the problem
> > with spaces.
> >
> > Please, see my comments within the program:
> >
> >
> > #include<stdio.h>
> >
> > int main(void)
> > {
> > int row, column, hash;
> > int number, spaces;
> > int height = 9;
>
> Would you believe that's twice as many objects as you need? All you
> really need is row, column, height. By the way, number is a lousy name -
> it could mean anything. In programming, picking good descriptive names
> is half the battle.
>
> > number = height;
> > spaces = height - number;
>
> Well, hang on - that's no different to setting spaces to 0, is it? So
> why not write: spaces = 0;
>
> And, in fact, I think I see what's coming...
>
> > hash = height;
> >
> > /** I introduced the variable number, and
> > spaces equal height - number, which
> > is decremented by one with each
> > iteration of the loop:
>
> Well, yes, but the value of 'spaces' is now set. Just because you
> decrement number, it doesn't mean that spaces will automatically adjust
> itself to the new value of height - number.
>
> > 1 row has
> > zero spaces, second has one space
> > (spaces = 9 - 8), etc.
> >
> > but somehow this algorithm doesn't
> > work.
> > **/
> >
> > for (row = 1; row<= height; row++)
> > {
> > for (column = 1; column<= spaces; column++)
> > printf (" ");
>
> Since spaces has the value 0, this loop will never print any spaces at all.
>
> >
> > number--;
> >
> > for (column = 1; column<= hash; column++)
> > printf ("# ");
>
> Why "# " (hash, space) ? In your spec, above, you don't have any spaces
> between the hashes, so why introduce one?
>
> >
> > hash--;
> >
> > printf ("\n");
> > }
> > return 0;
> > }
> >
> > The result looks like this:
> > Please, enter the height
> > 9
>
> No, it doesn't, because your program doesn't have any input. You are
> posting the result of a previous test...
>
>
> > # # # # # # # # #
> > # # # # # # # #
> > # # # # # # #
> > # # # # # #
> > # # # # #
> > # # # #
> > # # #
> > # #
> > #
>
> ...and in fact the program you posted doesn't print the above, but this:
>
> # # # # # # # # #
> # # # # # # # #
> # # # # # # #
> # # # # # #
> # # # # #
> # # # #
> # # #
> # #
> #
>
> Which is direct confirmation (if it were needed) that you haven't
> incremented the spaces object's value on each row.
>
> Take a look at my version, above, and think it through. Then try it out.
>
> --
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within
Richard,
Thank you so much.
I am so sorry: only the wrong
output is somehow left in my post. I did
type in a correct one, but it is not there.
Therefore, what you have actually gone through,
and the output you got, is a wrong output which
I keep on getting with different transformations
I tried.
My original program in the initial post
does produce that wrong output:
# # # # # # # # #
# # # # # # # #
# # # # # # #
# # # # # #
# # # # #
# # # #
# # #
# #
#
But should produce the one you showed above
(please, see my comment above).
It should look like this:
# # # # #
# # # #
# # #
# #
#
I managed to get the right number of hashes,
but then I've struggled with spaces, and
tried a few options, finally arriving at
this bizarre looking number, which computes
the right number of spaces, but on paper :)))
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 09:16 +0100 |
| Message-ID | <KlTFx.420668$aP2.47872@fx38.am4> |
| In reply to | #69057 |
On 03/09/2015 08:26, Alla _ wrote:
> On Wednesday, September 2, 2015 at 4:12:40 PM UTC+4, Richard Heathfield wrote:
>> On 02/09/2015 12:38, Alla _ wrote:
>> > Hello!
>> >
>> > Please, take a look at this simple
>> > program that is supposed to print out
>> > a pattern of the following kind:
>> >
>> > ####
>> > ###
>> > ##
>> > #
>>
<snip>
>
> It should look like this:
> # # # # #
> # # # #
> # # #
> # #
> #
Well, that's not the same output that you asked for in your original
article. It's a different problem, and it has a different solution.
Take a look at this diagram, where _ means "a space that is part of a
space-hash digraph", and $ means "a leading space".
#_#_#_#_#
$_#_#_#_#
$$$_#_#_#
$$$$$_#_#
$$$$$$$_#
On row 0, we want 0 leading spaces.
On row 1, we want 1 leading space.
On row 2, we want 3 leading spaces.
On row 3, we want 5 leading spaces.
On row n (except for the first row), we want 2n-1 leading spaces.
Now, that's not very elegant, is it? We have an exception on the first
row. Of course, we could write it using "#_" rather than "_#", like this:
#_#_#_#_#_
$$#_#_#_#_
$$$$#_#_#_
$$$$$$#_#_
$$$$$$$$#_
This way, we want 2n leading spaces on every row, which is cleaner, but
it gives us the awkward problem that we're printing an extra space at
the end of every line. That will look fine with spaces, but it wouldn't
be right. (What if we had to use @ rather than a space? We'd have an
extra @ at the end of every line, which would be screamingly incorrect.)
And I'm not even going to mention what C's allowed to do with trailing
spaces on a text output line. (Oh, I did. Well, never mind - it'll wait.)
This second way is cleaner, but it does seem as if we need to introduce
a hack for the last #. (Can we avoid that?)
So we want 2n leading spaces, followed by height-n-1 sets of hash-space,
followed by a single hash and a newline.
It emerges that what we really want is to print a bunch of SPACE SPACE,
followed by a bunch of HASH SPACE, followed by HASH NEWLINE. So we
should be thinking not in terms of characters, but in terms of *pairs*
of characters (we call such pairs "digraphs"). Thinking of the problem
in these terms means that the "hack" for the last character just
disappears. We're pairing it off with the newline, which lends a
pleasing symmetry to the whole problem.
I'm going to build on Ben's suggestion that we start to use functions to
do some of the work for us.
Firstly, we're going to define a function to print a bunch of digraphs.
(To make it easy for you to try this program out, I'm going to add my
annotations in C comment form, so that you can just copy-paste the whole
shooting-match and shove it straight into your editor.)
#include <stdio.h>
void print_digraph(int first, int second, int count)
{
while(count--)
{
putchar(first);
putchar(second);
}
}
/*
So if we want to print SPACE SPACE (just once, say), we would call
print_digraph(' ', ' ', 1);
And if we want HASH SPACE, we have print_digraph('#', ' ', 1);
and if we want HASH NEWLINE, we have print_digraph('#', '\n');
So how do we print a whole line correctly? Well, let's write a function
to do that:
*/
void print_line(int current_line, int height)
{
print_digraph(' ', ' ', current_line);
print_digraph('#', ' ', (height - current_line) - 1);
print_digraph('#', '\n', 1);
}
/*
And now main() becomes trivial:
*/
int main(void)
{
int row;
int column;
int height = 9;
for(row = 0; row < height; row++)
{
print_line(row, height);
}
return 0;
}
When I run this, I get the output
# # # # # # # # #
# # # # # # # #
# # # # # # #
# # # # # #
# # # # #
# # # #
# # #
# #
#
which is, I think, what you now want.
Okay, so what should you take away from this?
Firstly, you *must* think the problem through in a logical way. Decide
*exactly* what you want, and then look for patterns in your required
output that you can exploit in your code. When you ask for help, ask for
the help with the problem you're trying to solve, not some
mildly-related problem.
Secondly, Ben is absolutely right that you need to start thinking in
terms of functions - sub-units of the program that do a small,
well-defined task. Programming can be thought of as the art of breaking
the problem down into sub-problems, solving those sub-problems using
functions, and then using those functions together in the right way to
solve the big problem.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-05 04:35 -0700 |
| Message-ID | <02c42eee-cb4b-4110-bcf5-97499a64b3a8@googlegroups.com> |
| In reply to | #69063 |
Hello Everyone!
Can't find enough thankful words for your
help! Although all of your contributions are
valuable and helpful to me, I would like to thank
especially Richard, who has invested so much time
and patience to help me with the material, and Ben.
I see my progress, and it is so amazing.
I still have to learn a lot about the basics we
have discussed in this and few other threads;
and I still have questions, some of which I will
post in the next reply.
But first, please, let me brag a little bit :)
Please, take a look at two options of a
bit revised (more complicated) version of
a program that should produce the following
output:
'_' stand for spaces
___#__#___
__##__##__
_###__###_
####__####
First version with functions (it was
so exciting to learn them); second version
with a for loop (I did two versions to
make sure I understand both of them, and to
test myself).
/**
*Program to create two half pyramids, separated by
*two spaces, of a height chosen by the user
**/
#include <stdio.h>
void print_digraph_1 (int symbol, int count)
{
while(count--)
{
putchar (symbol);
}
}
void print_digraph_2 (int symbol_1, int symbol_2, int symbol_3, int symbol_4, int count)
{
while(count--)
{
putchar (symbol_1);
putchar (symbol_2);
putchar (symbol_3);
putchar (symbol_4);
}
}
void print_line(int current_line, int height)
{
print_digraph_1 (' ', (height - current_line - 1));
print_digraph_1 ('#', current_line);
print_digraph_2 ('#', ' ', ' ', '#', 1);
print_digraph_1 ('#', current_line);
print_digraph_1 (' ', (height - current_line - 1));
print_digraph_1 ('\n', 1);
}
int main(void)
{
int row, height;
printf ("Please, enter the height: \n");
if (scanf("%d", &height) == 1)
{
for(row = 0; row < height; row++)
{
print_line(row, height);
}
}
return 0;
}
/**
*Program to create two half pyramids, separated by
*two spaces, of a height chosen by the user
**/
#include <stdio.h>
int main(void)
{
int row, column, height;
printf ("Please, enter the height\n");
if (scanf("%d", &height) == 1)
{
for (row = 0; row < height; row++)
{
for (column = 0; column <= (height - row - 1); column++)
putchar (' ');
for (column = 0; column <= row + 1; column++)
putchar ('#');
printf (" ");
for (column = 0; column <= row + 1; column++)
putchar ('#');
for (column = 0; column <= (height - row - 1); column++)
putchar (' ');
printf ("\n");
}
}
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-05 13:08 +0100 |
| Message-ID | <IWAGx.364961$tz.193702@fx02.am4> |
| In reply to | #69427 |
On 05/09/2015 12:35, Alla _ wrote:
> Hello Everyone!
>
> Can't find enough thankful words for your
> help! Although all of your contributions are
> valuable and helpful to me, I would like to thank
> especially Richard, who has invested so much time
> and patience to help me with the material, and Ben.
You're welcome.
I have a few nit-picks (see below).
> '_' stand for spaces
(Fair enough!)
>
> ___#__#___
> __##__##__
> _###__###_
> ####__####
>
> First version with functions (it was
> so exciting to learn them); second version
> with a for loop (I did two versions to
> make sure I understand both of them, and to
> test myself).
That's an excellent approach.
>
> /**
> *Program to create two half pyramids, separated by
> *two spaces, of a height chosen by the user
> **/
>
> #include<stdio.h>
>
> void print_digraph_1 (int symbol, int count)
> {
> while(count--)
> {
> putchar (symbol);
First nit: that's not a digraph! "di-" means "2". You are actually just
printing a character, "count" times. A better name for this function
would be something like looped_putchar or putchar_repeat or something
like that.
> }
> }
>
> void print_digraph_2 (int symbol_1, int symbol_2, int symbol_3, int symbol_4, int count)
> {
> while(count--)
> {
> putchar (symbol_1);
> putchar (symbol_2);
> putchar (symbol_3);
> putchar (symbol_4);
Here, you are actually printing tetragraphs, not digraphs. That's fine -
you print what you need to print, obviously - but it makes the name of
the function misleading. Choosing good names is half the programming battle.
> }
> }
>
> void print_line(int current_line, int height)
>
> {
> print_digraph_1 (' ', (height - current_line - 1));
> print_digraph_1 ('#', current_line);
> print_digraph_2 ('#', ' ', ' ', '#', 1);
> print_digraph_1 ('#', current_line);
> print_digraph_1 (' ', (height - current_line - 1));
> print_digraph_1 ('\n', 1);
> }
See how much easier it is when you split the problem into functions? :-)
<snip>
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-05 05:48 -0700 |
| Message-ID | <7eef806a-4b1f-4f0a-ab1e-9fcc053b29b0@googlegroups.com> |
| In reply to | #69433 |
On Saturday, September 5, 2015 at 4:08:25 PM UTC+4, Richard Heathfield wrote:
> On 05/09/2015 12:35, Alla _ wrote:
> > Hello Everyone!
> >
> > Can't find enough thankful words for your
> > help! Although all of your contributions are
> > valuable and helpful to me, I would like to thank
> > especially Richard, who has invested so much time
> > and patience to help me with the material, and Ben.
>
> You're welcome.
>
> I have a few nit-picks (see below).
>
> > '_' stand for spaces
>
> (Fair enough!)
>
> >
> > ___#__#___
> > __##__##__
> > _###__###_
> > ####__####
> >
> > First version with functions (it was
> > so exciting to learn them); second version
> > with a for loop (I did two versions to
> > make sure I understand both of them, and to
> > test myself).
>
> That's an excellent approach.
>
> >
> > /**
> > *Program to create two half pyramids, separated by
> > *two spaces, of a height chosen by the user
> > **/
> >
> > #include<stdio.h>
> >
> > void print_digraph_1 (int symbol, int count)
> > {
> > while(count--)
> > {
> > putchar (symbol);
>
> First nit: that's not a digraph! "di-" means "2". You are actually just
> printing a character, "count" times. A better name for this function
> would be something like looped_putchar or putchar_repeat or something
> like that.
>
> > }
> > }
> >
> > void print_digraph_2 (int symbol_1, int symbol_2, int symbol_3, int symbol_4, int count)
> > {
> > while(count--)
> > {
> > putchar (symbol_1);
> > putchar (symbol_2);
> > putchar (symbol_3);
> > putchar (symbol_4);
>
> Here, you are actually printing tetragraphs, not digraphs. That's fine -
> you print what you need to print, obviously - but it makes the name of
> the function misleading. Choosing good names is half the programming battle.
>
> > }
> > }
> >
> > void print_line(int current_line, int height)
> >
> > {
> > print_digraph_1 (' ', (height - current_line - 1));
> > print_digraph_1 ('#', current_line);
> > print_digraph_2 ('#', ' ', ' ', '#', 1);
> > print_digraph_1 ('#', current_line);
> > print_digraph_1 (' ', (height - current_line - 1));
> > print_digraph_1 ('\n', 1);
> > }
>
> See how much easier it is when you split the problem into functions? :-)
>
> <snip>
>
> --
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within
Agree with every word :) Being too excited,
I forgot to change function names :)
I can't say functions are more easy; too
early for me to treat anything as easy :)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-09-05 14:56 +0100 |
| Message-ID | <87y4glhswa.fsf@bsb.me.uk> |
| In reply to | #69427 |
Alla _ <modelling.data@gmail.com> writes:
> Hello Everyone!
>
> Can't find enough thankful words for your
> help! Although all of your contributions are
> valuable and helpful to me, I would like to thank
> especially Richard, who has invested so much time
> and patience to help me with the material, and Ben.
> I see my progress, and it is so amazing.
You're welcome. It's nice when people say thank you but you don't need
to do it
>
> I still have to learn a lot about the basics we
> have discussed in this and few other threads;
> and I still have questions, some of which I will
> post in the next reply.
>
> But first, please, let me brag a little bit :)
>
> Please, take a look at two options of a
> bit revised (more complicated) version of
> a program that should produce the following
> output:
>
> '_' stand for spaces
>
> ___#__#___
> __##__##__
> _###__###_
> ####__####
>
> First version with functions (it was
> so exciting to learn them); second version
> with a for loop (I did two versions to
> make sure I understand both of them, and to
> test myself).
<snip>
> void print_digraph_2 (int symbol_1, int symbol_2, int symbol_3, int symbol_4, int count)
> {
> while(count--)
> {
> putchar (symbol_1);
> putchar (symbol_2);
> putchar (symbol_3);
> putchar (symbol_4);
> }
> }
You don't need this function at all. You could just write:
> void print_line(int current_line, int height)
>
> {
> print_digraph_1 (' ', (height - current_line - 1));
> print_digraph_1 ('#', current_line + 1);
> print_digraph_1 (' ', 2);
> print_digraph_1 ('#', current_line + 1);
> print_digraph_1 (' ', (height - current_line - 1));
> print_digraph_1 ('\n', 1);
> }
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-05 05:46 -0700 |
| Message-ID | <2c714414-2b40-4f39-8e50-06cd2c03e4fb@googlegroups.com> |
| In reply to | #69063 |
On Thursday, September 3, 2015 at 12:16:53 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 08:26, Alla _ wrote:
> > On Wednesday, September 2, 2015 at 4:12:40 PM UTC+4, Richard Heathfield wrote:
> >> On 02/09/2015 12:38, Alla _ wrote:
> >> > Hello!
> >> >
> >> > Please, take a look at this simple
> >> > program that is supposed to print out
> >> > a pattern of the following kind:
> >> >
> >> > ####
> >> > ###
> >> > ##
> >> > #
> >>
> <snip>
> >
> > It should look like this:
> > # # # # #
> > # # # #
> > # # #
> > # #
> > #
>
> Well, that's not the same output that you asked for in your original
> article. It's a different problem, and it has a different solution.
>
> Take a look at this diagram, where _ means "a space that is part of a
> space-hash digraph", and $ means "a leading space".
> #_#_#_#_#
> $_#_#_#_#
> $$$_#_#_#
> $$$$$_#_#
> $$$$$$$_#
>
> On row 0, we want 0 leading spaces.
> On row 1, we want 1 leading space.
> On row 2, we want 3 leading spaces.
> On row 3, we want 5 leading spaces.
> On row n (except for the first row), we want 2n-1 leading spaces.
>
> Now, that's not very elegant, is it? We have an exception on the first
> row. Of course, we could write it using "#_" rather than "_#", like this:
>
> #_#_#_#_#_
> $$#_#_#_#_
> $$$$#_#_#_
> $$$$$$#_#_
> $$$$$$$$#_
>
> This way, we want 2n leading spaces on every row, which is cleaner, but
> it gives us the awkward problem that we're printing an extra space at
> the end of every line. That will look fine with spaces, but it wouldn't
> be right. (What if we had to use @ rather than a space? We'd have an
> extra @ at the end of every line, which would be screamingly incorrect.)
> And I'm not even going to mention what C's allowed to do with trailing
> spaces on a text output line. (Oh, I did. Well, never mind - it'll wait.)
>
> This second way is cleaner, but it does seem as if we need to introduce
> a hack for the last #. (Can we avoid that?)
>
> So we want 2n leading spaces, followed by height-n-1 sets of hash-space,
> followed by a single hash and a newline.
>
> It emerges that what we really want is to print a bunch of SPACE SPACE,
> followed by a bunch of HASH SPACE, followed by HASH NEWLINE. So we
> should be thinking not in terms of characters, but in terms of *pairs*
> of characters (we call such pairs "digraphs"). Thinking of the problem
> in these terms means that the "hack" for the last character just
> disappears. We're pairing it off with the newline, which lends a
> pleasing symmetry to the whole problem.
>
> I'm going to build on Ben's suggestion that we start to use functions to
> do some of the work for us.
>
> Firstly, we're going to define a function to print a bunch of digraphs.
> (To make it easy for you to try this program out, I'm going to add my
> annotations in C comment form, so that you can just copy-paste the whole
> shooting-match and shove it straight into your editor.)
>
> #include <stdio.h>
>
> void print_digraph(int first, int second, int count)
I do not understand why 'first' and 'second are declared
as ints, if in fact they are placeholders for characters?
Count is an int, because it is a number.
> {
> while(count--)
> {
> putchar(first);
> putchar(second);
> }
> }
>
> /*
> So if we want to print SPACE SPACE (just once, say), we would call
> print_digraph(' ', ' ', 1);
> And if we want HASH SPACE, we have print_digraph('#', ' ', 1);
> and if we want HASH NEWLINE, we have print_digraph('#', '\n');
>
> So how do we print a whole line correctly? Well, let's write a function
> to do that:
> */
>
> void print_line(int current_line, int height)
> {
> print_digraph(' ', ' ', current_line);
> print_digraph('#', ' ', (height - current_line) - 1);
> print_digraph('#', '\n', 1);
> }
>
> /*
> And now main() becomes trivial:
> */
>
> int main(void)
> {
> int row;
> int column;
> int height = 9;
>
> for(row = 0; row < height; row++)
> {
> print_line(row, height);
> }
> return 0;
> }
>
> When I run this, I get the output
>
> # # # # # # # # #
> # # # # # # # #
> # # # # # # #
> # # # # # #
> # # # # #
> # # # #
> # # #
> # #
> #
>
> which is, I think, what you now want.
>
> Okay, so what should you take away from this?
>
> Firstly, you *must* think the problem through in a logical way. Decide
> *exactly* what you want, and then look for patterns in your required
> output that you can exploit in your code. When you ask for help, ask for
> the help with the problem you're trying to solve, not some
> mildly-related problem.
>
> Secondly, Ben is absolutely right that you need to start thinking in
> terms of functions - sub-units of the program that do a small,
> well-defined task. Programming can be thought of as the art of breaking
> the problem down into sub-problems, solving those sub-problems using
> functions, and then using those functions together in the right way to
> solve the big problem.
>
> --
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-05 14:03 +0100 |
| Message-ID | <OKBGx.401291$pF1.326516@fx41.am4> |
| In reply to | #69450 |
On 05/09/2015 13:46, Alla _ wrote:
> On Thursday, September 3, 2015 at 12:16:53 PM UTC+4, Richard Heathfield wrote:
<snip>
>> void print_digraph(int first, int second, int count)
>
> I do not understand why 'first' and 'second are declared
> as ints, if in fact they are placeholders for characters?
> Count is an int, because it is a number.
Ah yes, I was afraid you might bring that up.
In C, the word "character" is fraught with peril. :-)
It would take an entire book to explain why, but in C (not in C++, if I
recall correctly):
* Character literals like ' ', '$', 'Z', '9', '!', and so on are known
as "integer character constants";
* An integer character constant has type int;
* Simple integer character constants (such as the ones I just showed
you) have code points that will fit into a single char without
overflowing it;
* When you use a char in a calculation or a function call, it is
"promoted" to type int.
char is quite handy for dealing with whole bunches of characters in a
row ("strings"), but that's about all. In general, if you find yourself
defining a single char, you have to ask yourself some hard questions
about what will happen when the integer promotion rules cut in. That
doesn't mean you should never do it, but it's not quite as obvious as
perhaps it ought to be.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-05 08:55 -0700 |
| Message-ID | <5a07fa3a-c38f-417f-b46f-cea4d63a515b@googlegroups.com> |
| In reply to | #69457 |
On Saturday, September 5, 2015 at 5:03:57 PM UTC+4, Richard Heathfield wrote:
> On 05/09/2015 13:46, Alla _ wrote:
> > On Thursday, September 3, 2015 at 12:16:53 PM UTC+4, Richard Heathfield wrote:
>
> <snip>
>
> >> void print_digraph(int first, int second, int count)
> >
> > I do not understand why 'first' and 'second are declared
> > as ints, if in fact they are placeholders for characters?
> > Count is an int, because it is a number.
>
> Ah yes, I was afraid you might bring that up.
:) I am, like a wonk, reading every single character you
write, trying to fathom out the meaning and learn.
>
> In C, the word "character" is fraught with peril. :-)
>
> It would take an entire book to explain why, but in C (not in C++, if I
> recall correctly):
>
> * Character literals like ' ', '$', 'Z', '9', '!', and so on are known
> as "integer character constants";
> * An integer character constant has type int;
> * Simple integer character constants (such as the ones I just showed
> you) have code points that will fit into a single char without
> overflowing it;
> * When you use a char in a calculation or a function call, it is
> "promoted" to type int.
>
> char is quite handy for dealing with whole bunches of characters in a
> row ("strings"), but that's about all. In general, if you find yourself
> defining a single char, you have to ask yourself some hard questions
> about what will happen when the integer promotion rules cut in. That
> doesn't mean you should never do it, but it's not quite as obvious as
> perhaps it ought to be.
>
> --
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-02 14:39 +0200 |
| Message-ID | <ms6qlc$meu$1@node1.news.atman.pl> |
| In reply to | #68988 |
On 02.09.2015 13:38, Alla _ wrote:
> Hello!
>
> 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.
>
> Please, see my comments within the program:
>
>
> #include <stdio.h>
>
> int main(void)
> {
> int row, column, hash;
> int number, spaces;
> int height = 9;
>
> number = height;
This
> spaces = height - number;
...is 0. And you newer change it.
"spaces" is not a formula, it it just a number,
it won't change when you change height or number.
> hash = height;
>
> /** I introduced the variable number, and
> spaces equal height - number, which
> is decremented by one with each
> iteration of the loop: 1 row has
> zero spaces, second has one space
> (spaces = 9 - 8), etc.
>
> but somehow this algorithm doesn't
> work.
> **/
>
> for (row = 1; row <= height; row++)
> {
So you want the same number of spaces , i.e. 0
every time? No , you want how many spaces?
spaces = row - 1;
Or, you can...
> for (column = 1; column <= spaces; column++)
> printf (" ");
>
> number--;
spaces++;
>
> for (column = 1; column <= hash; column++)
> printf ("# ");
Do you want spaces betwen "#"?
No. So print onlt "#"
>
> hash--;
>
> printf ("\n");
> }
> return 0;
> }
>
> The result looks like this:
> Please, enter the height
> 9
> # # # # # # # # #
> # # # # # # # #
> # # # # # # #
> # # # # # #
> # # # # #
> # # # #
> # # #
> # #
> #
This is not the output from program you have posted.
bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-09-02 13:55 +0100 |
| Message-ID | <874mjdj7zx.fsf@bsb.me.uk> |
| In reply to | #68988 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 01:51 -0700 |
| Message-ID | <fe38ea5f-8ad7-4546-bba2-d1f59fae6031@googlegroups.com> |
| In reply to | #68992 |
On Wednesday, September 2, 2015 at 4:55:43 PM UTC+4, Ben Bacarisse wrote:
> 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.
Thank you very much. This is a amazing.
I am trying to compile it (with little changes,
because Terminal doesn't accept for (int =..)),
but I receive a few error messages, which
I still don't know how to decipher.
Here is the program:
#include <stdio.h>
void repeat(int n, char c)
void output_line(int i, int length)
int main(void)
{
int pattern_size, i;
if (scanf("%d", &pattern_size) == 1)
{
for (i = 0; i < pattern_size; i++)
output_line(i, pattern_size);
}
}
void repeat(int n, char c)
{
int i;
for (i = 0; i < n; i++)
putchar(c);
}
void output_line(int i, int length)
{
repeat(i, ' ');
repeat(length - i, '#');
putchar('\n');
}
Error message (some error in the repeat function):
pattern5function.c: In function 'repeat':
pattern5function.c:7: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'int'
pattern5function.c:16: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
pattern5function.c:24: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
pattern5function.c:28: error: expected '{' at end of input
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 10:01 +0100 |
| Message-ID | <G%TFx.246511$i22.177762@fx05.am4> |
| In reply to | #69071 |
On 03/09/2015 09:51, Alla _ wrote: <snip> > Thank you very much. This is a amazing. > I am trying to compile it (with little changes, > because Terminal doesn't accept for (int =..)), So much for C99. :-) You won't have that problem with any code I show you. > but I receive a few error messages, which > I still don't know how to decipher. > Here is the program: > > #include<stdio.h> > > void repeat(int n, char c) This is a function prototype. If it doesn't have a function body, it's considered to be a forward declaration of the function, and it needs a semicolon on the end, like this: void repeat(int n, char c); > void output_line(int i, int length) Same here: void output_line(int i, int length); These forward declarations allow the compiler to generate code for any calls to these functions, without actually having seen the functions themselves. (Of course, the implementation has to be able to see the code /eventually/, or the program won't link.) A hint for dealing with diagnostic messages (errors and warnings): ALWAYS start with the very first one. Deal with that, and ONLY that, and then re-compile. I offer you this advice in the hope that it will keep you sane. Once a compiler gets its teeth into a syntax error, it seems to lose all sense of perspective, and can generate dozens or even hundreds of scary error messages - "ghosts", if you like - which vanish when you fix that one error and re-compile. -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 02:10 -0700 |
| Message-ID | <165387fe-5c35-433d-a583-d516a15f8d7a@googlegroups.com> |
| In reply to | #69073 |
On Thursday, September 3, 2015 at 1:01:38 PM UTC+4, Richard Heathfield wrote: > On 03/09/2015 09:51, Alla _ wrote: > > <snip> > > > Thank you very much. This is a amazing. > > I am trying to compile it (with little changes, > > because Terminal doesn't accept for (int =..)), > > So much for C99. :-) > > You won't have that problem with any code I show you. > > > but I receive a few error messages, which > > I still don't know how to decipher. > > Here is the program: > > > > #include<stdio.h> > > > > void repeat(int n, char c) > > This is a function prototype. If it doesn't have a function body, it's > considered to be a forward declaration of the function, and it needs a > semicolon on the end, like this: > > void repeat(int n, char c); > > > void output_line(int i, int length) > > Same here: > > void output_line(int i, int length); > > These forward declarations allow the compiler to generate code for any > calls to these functions, without actually having seen the functions > themselves. (Of course, the implementation has to be able to see the > code /eventually/, or the program won't link.) > > A hint for dealing with diagnostic messages (errors and warnings): > ALWAYS start with the very first one. Deal with that, and ONLY that, and > then re-compile. I offer you this advice in the hope that it will keep > you sane. Once a compiler gets its teeth into a syntax error, it seems > to lose all sense of perspective, and can generate dozens or even > hundreds of scary error messages - "ghosts", if you like - which vanish > when you fix that one error and re-compile. > > -- > Richard Heathfield > Email: rjh at cpax dot org dot uk > "Usenet is a strange place" - dmr 29 July 1999 > Sig line 4 vacant - apply within I see now. Thank you very much!
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web