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


Groups > comp.lang.c > #68988 > unrolled thread

Program to print a pattern of hashtags: issues with spaces

Started byAlla _ <modelling.data@gmail.com>
First post2015-09-02 04:38 -0700
Last post2015-09-05 04:40 -0700
Articles 16 on this page of 36 — 8 participants

Back to article view | Back to comp.lang.c


Contents

  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 2 of 2 — ← Prev page 1 [2]


#69136

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-09-03 16:10 +0100
Message-ID<87egifh737.fsf@bsb.me.uk>
In reply to#69071
Alla _ <modelling.data@gmail.com> writes:
<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 =..)),

Because your book uses C99 I thought you'd know how to compile a C99
program!  You probably just need to use gcc -std=c99 ... but you might
like to know that I use

  gcc -std=c99 -D_ANSI_SOURCE -pedantic -Wall -Wextra
      -Wformat-nonliteral -Wcast-align -Wbad-function-cast
      -Wstrict-prototypes -Winline -Wundef -Wcast-qual -Wshadow
      -Wconversion -ffloat-store -fno-diagnostics-show-option

in order to get lots of helpful warnings (some of which I ignore once I
know that they are OK).

> but I receive a few error messages, which 
> I still don't know how to decipher.
> Here is the program:

You changed the order of the code and forgot the semi-colons after the
function declarations.  That's all.  It's a shame that a simple missing
';' gives such a confusing error but that is sadly very common.

> #include <stdio.h>
>
> void repeat(int n, char c)
>
> void output_line(int i, int length)

You need ; after each of these.  You book puts declarations like this
inside the function that use them.  Doing that would be fun just to see
the responses you'd get here!

> int main(void)
> {
>     int pattern_size, i;
>     if (scanf("%d", &pattern_size) == 1)
>     {

Yo could put "int i;" here.  I would if I were forced to use old C.

>         for (i = 0; i < pattern_size; i++)
>             output_line(i, pattern_size);
>     }

You should be consistent with your chosen brace style (the placement of
{}s).  I don't like to use redundant ones, but if you add some, you
should add them all -- i.e put {}s round the body of the for statement
as well.

> }
> void repeat(int n, char c)
> {
>     int i;
>     
>     for (i = 0; i < n; i++)
>         putchar(c);
> }

And again here.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#69154

FromKeith Thompson <kst-u@mib.org>
Date2015-09-03 09:12 -0700
Message-ID<ln613rscr6.fsf@kst-u.example.com>
In reply to#69071
Alla _ <modelling.data@gmail.com> writes:
[...]
> 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.
[...]

Your use of the word "Terminal" with a capital T suggests, I think,
that you're using Mac OSX.  Is that correct?

"Terminal" is just a terminal emulator.  Normally you'd have a shell
(perhaps bash) running inside a terminal window; you'd then invoke
a compiler from the shell by typing a command something like

    gcc [options] filename.c

The terminal has nothing to do with compiling C code.

The "for (int i = ...;;)" syntax is a feature did not exist in the
1989/1990 version of the C language.  It was introduced by the 1999
ISO C standard.

Until very recently, gcc did not support the 1999 standard *by
default*.  (When you type "gcc" on OSX, you might be invoking
the clang compiler rather than gcc; it behaves very similarly.)
You can ask gcc or clang to support C99, and therefore to permit
the new style of for loop, using the "-std=c99" option (if you
don't want gcc-specific extensions) or "-std=gnu99" (if you do).
There's also a "-std=c11" or "-std=gnu11" option to tell gcc to
(attempt to) conform to the later C11 standard.  You can probably
safely ignore the newer C11 standard for now.

-- 
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]


#69020

FromBarry Schwarz <schwarzb@dqel.com>
Date2015-09-02 11:52 -0700
Message-ID<bofeuapflcrpfm18suaka5raubh3pqb2fs@4ax.com>
In reply to#68988
On Wed, 2 Sep 2015 04:38:56 -0700 (PDT), Alla _
<modelling.data@gmail.com> 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.

When asking for help with a problem, simply telling us your solution
doesn't work is not very informative.  We need to know in what way it
doesn't work.  Tell us how the result differs from what you intended. 
Eventually I saw your output at the bottom (partly my fault for
looking at details before reading the entire message) but if you put
that data up here it would give us a clue what to look for as we
examined your code.  In either case, you need to specify what you
don't like about the output.

>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. 
>    **/

Most of the people who respond to your question can read code.
Comments that simply repeat the code do not provide any additional
information.  We know spaces is the difference between two variables.
Comments that describe something the code does not do are misleading
(and probably affect you more than us).  In this case, you indicate
that spaces is recalculated in the loop but in fact it never is.

>    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

What program did these two lines come from?  Nowhere in the code you
show us do you request input from the user.  Is any part of this
output actually from the above code?

You should always use cut and paste for both your code and output.
Details can make all the difference.

># # # # # # # # # 
> # # # # # # # # 
>  # # # # # # # 
>   # # # # # # 
>    # # # # # 
>     # # # # 
>      # # # 
>       # # 
>        # 

Your desired output at the top of the message does not have spaces
between the hashtags.  Your actual output (if that is what this is)
shows spaces between the hashtags.  Was this change intentional?

>The program doesn't compute spaces in a 
>desired way.  

Why do you say that?  Both the desired output and the purported actual
output look like inverted triangles.  Each row has one less hashtag
than the previous row.  What don't you like?

Of course, when I ran your program each row was left justified, not
centered.  So where did this output come from?

We can't help you if you don't show us what you are actually doing.

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#69021

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-09-02 20:24 +0100
Message-ID<p1IFx.306768$Ch1.151161@fx40.am4>
In reply to#69020
On 02/09/2015 19:52, Barry Schwarz wrote:
> On Wed, 2 Sep 2015 04:38:56 -0700 (PDT), Alla _
> <modelling.data@gmail.com>  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.
>
> When asking for help with a problem, simply telling us your solution
> doesn't work is not very informative.  We need to know in what way it
> doesn't work.

He made that very clear.

> Tell us how the result differs from what you intended.

He did.

> Eventually I saw your output at the bottom (partly my fault for
> looking at details before reading the entire message) but if you put
> that data up here it would give us a clue what to look for as we
> examined your code.  In either case, you need to specify what you
> don't like about the output.

I eventually got around to reading this part of your reply. Put it 
higher up next time, okay? :-)

>>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.
>>     **/
>
> Most of the people who respond to your question can read code.
> Comments that simply repeat the code do not provide any additional
> information.

Seriously, I disagree here. They offer us an insight into the mind of 
the programmer. In this case, it was this comment that led me to realise 
that the OP was interpreting his "spaces = height - number;" statement 
as defining some kind of self-updating relationship (like a spreadsheet 
formula).

> We know spaces is the difference between two variables.

But the comment (taken along with the code) showed that he didn't know that.

> Comments that describe something the code does not do are misleading
> (and probably affect you more than us).  In this case, you indicate
> that spaces is recalculated in the loop but in fact it never is.

That was precisely why I found the comment valuable.

<snip>

>>The result looks like this:
>>Please, enter the height
>>9
>
> What program did these two lines come from?  Nowhere in the code you
> show us do you request input from the user.  Is any part of this
> output actually from the above code?

Valid complaint. I expect he just got a bit fingers-and-thumbs, writing 
the Usenet article while simultaneously fiddling about with the code in 
a desperate attempt to make it work, and lost track of which code went 
with which output.

<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]


#69075

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 02:06 -0700
Message-ID<e4700e02-d199-4ae1-b236-243901503690@googlegroups.com>
In reply to#69021
On Wednesday, September 2, 2015 at 11:24:20 PM UTC+4, Richard Heathfield wrote:
> On 02/09/2015 19:52, Barry Schwarz wrote:
> > On Wed, 2 Sep 2015 04:38:56 -0700 (PDT), Alla _
> > <modelling.data@gmail.com>  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.
> >
> > When asking for help with a problem, simply telling us your solution
> > doesn't work is not very informative.  We need to know in what way it
> > doesn't work.
> 
> He made that very clear.
> 
> > Tell us how the result differs from what you intended.
> 
> He did.
> 
> > Eventually I saw your output at the bottom (partly my fault for
> > looking at details before reading the entire message) but if you put
> > that data up here it would give us a clue what to look for as we
> > examined your code.  In either case, you need to specify what you
> > don't like about the output.
> 
> I eventually got around to reading this part of your reply. Put it 
> higher up next time, okay? :-)
> 
> >>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.
> >>     **/
> >
> > Most of the people who respond to your question can read code.
> > Comments that simply repeat the code do not provide any additional
> > information.
> 
> Seriously, I disagree here. They offer us an insight into the mind of 
> the programmer. In this case, it was this comment that led me to realise 
> that the OP was interpreting his "spaces = height - number;" statement 
> as defining some kind of self-updating relationship (like a spreadsheet 
> formula).
> 
> > We know spaces is the difference between two variables.
> 
> But the comment (taken along with the code) showed that he didn't know that.
> 
> > Comments that describe something the code does not do are misleading
> > (and probably affect you more than us).  In this case, you indicate
> > that spaces is recalculated in the loop but in fact it never is.
> 
> That was precisely why I found the comment valuable.
> 
> <snip>
> 
> >>The result looks like this:
> >>Please, enter the height
> >>9
> >
> > What program did these two lines come from?  Nowhere in the code you
> > show us do you request input from the user.  Is any part of this
> > output actually from the above code?
> 
> Valid complaint. I expect he just got a bit fingers-and-thumbs, writing 
> the Usenet article while simultaneously fiddling about with the code in 
> a desperate attempt to make it work, and lost track of which code went 
> with which output.
> 
> <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

Richard, 
thank you very much for understanding my reasoning.

Offtopic: I am a girl :) Well, they say at this age
girls are called women; anyway, a girl )))) 

[toc] | [prev] | [next] | [standalone]


#69081

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-09-03 10:36 +0100
Message-ID<nwUFx.391819$Dx3.75860@fx39.am4>
In reply to#69075
On 03/09/2015 10:06, Alla _ wrote:

<snip>

> Richard,
> thank you very much for understanding my reasoning.

You're most welcome.

> Offtopic: I am a girl :) Well, they say at this age
> girls are called women; anyway, a girl ))))

I apologise, then, for my institutionally sexist use of masculine 
pronouns. I'll try to remember.

-- 
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]


#69085

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 03:10 -0700
Message-ID<f2ea002f-906d-468e-8d08-92abd213fe87@googlegroups.com>
In reply to#69081
On Thursday, September 3, 2015 at 1:36:34 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 10:06, Alla _ wrote:
> 
> <snip>
> 
> > Richard,
> > thank you very much for understanding my reasoning.
> 
> You're most welcome.
> 
> > Offtopic: I am a girl :) Well, they say at this age
> > girls are called women; anyway, a girl ))))
> 
> I apologise, then, for my institutionally sexist use of masculine 
> pronouns. I'll try to remember.
> 
No need to apologize; it's absolutely
fine with me.
Actually, when I read professional 
textbooks, I am so confused by the way
authors now use 'she' instead of 'he' 
due to overblown issues around women; 
it sounds so pretentious.
 
> -- 
> 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]


#69667

FromTim Rentsch <txr@alumni.caltech.edu>
Date2015-09-06 13:45 -0700
Message-ID<kfn1tebs2dw.fsf@x-alumni2.alumni.caltech.edu>
In reply to#69021
Richard Heathfield <rjh@cpax.org.uk> writes:

> On 02/09/2015 19:52, Barry Schwarz wrote:
>> On Wed, 2 Sep 2015 04:38:56 -0700 (PDT), Alla _
>> <modelling.data@gmail.com>  wrote:
>>
>>> 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.
>>>     **/
>>
>> Most of the people who respond to your question can read code.
>> Comments that simply repeat the code do not provide any additional
>> information.
>
> Seriously, I disagree here.  [snip elaboration]

I agree with your reaction for leaving this in.  However I think
the comment (or explanation) is much better placed /after/ the
function body, not in the middle of it.

[toc] | [prev] | [next] | [standalone]


#69074

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 02:04 -0700
Message-ID<a53c0e6a-9989-4944-adef-10891d597272@googlegroups.com>
In reply to#69020
On Wednesday, September 2, 2015 at 10:52:15 PM UTC+4, Barry Schwarz wrote:
> On Wed, 2 Sep 2015 04:38:56 -0700 (PDT), Alla _
> <modelling.data@gmail.com> 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.
> 
> When asking for help with a problem, simply telling us your solution
> doesn't work is not very informative.  We need to know in what way it
> doesn't work.  Tell us how the result differs from what you intended. 
> Eventually I saw your output at the bottom (partly my fault for
> looking at details before reading the entire message) but if you put
> that data up here it would give us a clue what to look for as we
> examined your code.  In either case, you need to specify what you
> don't like about the output.
> 
> >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. 
> >    **/
> 
> Most of the people who respond to your question can read code.
> Comments that simply repeat the code do not provide any additional
> information.  We know spaces is the difference between two variables.
> Comments that describe something the code does not do are misleading
> (and probably affect you more than us).  In this case, you indicate
> that spaces is recalculated in the loop but in fact it never is.
> 
> >    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
> 
> What program did these two lines come from?  Nowhere in the code you
> show us do you request input from the user.  Is any part of this
> output actually from the above code?
> 
> You should always use cut and paste for both your code and output.
> Details can make all the difference.
> 
> ># # # # # # # # # 
> > # # # # # # # # 
> >  # # # # # # # 
> >   # # # # # # 
> >    # # # # # 
> >     # # # # 
> >      # # # 
> >       # # 
> >        # 
> 
> Your desired output at the top of the message does not have spaces
> between the hashtags.  Your actual output (if that is what this is)
> shows spaces between the hashtags.  Was this change intentional?
> 
> >The program doesn't compute spaces in a 
> >desired way.  
> 
> Why do you say that?  Both the desired output and the purported actual
> output look like inverted triangles.  Each row has one less hashtag
> than the previous row.  What don't you like?
> 
> Of course, when I ran your program each row was left justified, not
> centered.  So where did this output come from?
> 
> We can't help you if you don't show us what you are actually doing.
> 
> -- 
> Remove del for email

Barry, I agree with all you said,
and I did try to write a post that would
follow these rules. I am sorry that it 
didn't work out as I intended. 

As to comments, as Richard pointed out below,
my intention was to explain why I used 
this or that variable and approach. 

Please, bear with me: I will get better :)

[toc] | [prev] | [next] | [standalone]


#69058

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 00:31 -0700
Message-ID<0e007d82-0810-48c2-a97a-a2b786dbf77f@googlegroups.com>
In reply to#68988
On Wednesday, September 2, 2015 at 3:39:06 PM UTC+4, 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;
>     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!

Thank you Everyone for you input!
I am truly sorry for the confusion
I caused by somehow wrong output
which I accidentally published 
as a correct one: I typed it in 
manually, thus, maybe, due to the 
technical issues it didn't come out
correctly. 

Still I again learned a lot from you.
Now, try to implement all suggestions.

[toc] | [prev] | [next] | [standalone]


#69062

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 00:49 -0700
Message-ID<b8d34866-0e1e-4834-a020-9b2d16dc3d5d@googlegroups.com>
In reply to#68988
On Wednesday, September 2, 2015 at 3:39:06 PM UTC+4, 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;
>     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!

Unbelievable! I have just figured out
what problem is; I didn't have to invent
any additional arithmetic, just printf ("  ")
 - two spaces :)))) I am so sorry that I 
had to bother the community with such 
issue. 
still I am happy, because I, even this tiny
mistake, helped me to learn a lot from you.
Thank you!

#include <stdio.h>

int main(void)
{
    int row, column;
    int height = 4;
    int number = height;
    
    for (row = 0; row < height; row++)
    {
        for (column = 0 ; column <= row; column++)
            printf ("  ");
                
        for (column = 0; column < number; column++)
            printf ("# ");
        
        number--;
        
        printf ("\n");
    }
    return 0;
}
  
The correct output is:
  # # # # 
    # # # 
      # # 
        # 

[toc] | [prev] | [next] | [standalone]


#69066

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-09-03 09:23 +0100
Message-ID<psTFx.449947$z21.66838@fx18.am4>
In reply to#69062
On 03/09/2015 08:49, Alla _ wrote:

<snip>

> Unbelievable! I have just figured out
> what problem is;

That's excellent news.

> I didn't have to invent
> any additional arithmetic, just printf ("  ")
>   - two spaces :)))) I am so sorry that I
> had to bother the community with such
> issue.
> still I am happy, because I, even this tiny
> mistake, helped me to learn a lot from you.
> Thank you!

You can still squeeze a bit more learning out of this one:

> #include<stdio.h>
>
> int main(void)
> {
>      int row, column;
>      int height = 4;
>      int number = height;
>
>      for (row = 0; row<  height; row++)
>      {
>          for (column = 0 ; column<= row; column++)
>              printf ("  ");
>
>          for (column = 0; column<  number; column++)
>              printf ("# ");
>

What if you'd been asked to print this instead:

#U#U#U#U#
UU#U#U#U#
UUUU#U#U#
UUUUUU#U#

Could you just substitute U for space and be left with a working 
program? You'd have thought so, wouldn't you? But try it and see.

A few moments ago, I posted an article in which this issue is addressed. 
I recommend it to you.

-- 
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]


#69079

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 02:33 -0700
Message-ID<a48a3667-fa87-4056-bab7-b7061139d08b@googlegroups.com>
In reply to#69066
On Thursday, September 3, 2015 at 12:23:57 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 08:49, Alla _ wrote:
> 
> <snip>
> 
> > Unbelievable! I have just figured out
> > what problem is;
> 
> That's excellent news.
> 
> > I didn't have to invent
> > any additional arithmetic, just printf ("  ")
> >   - two spaces :)))) I am so sorry that I
> > had to bother the community with such
> > issue.
> > still I am happy, because I, even this tiny
> > mistake, helped me to learn a lot from you.
> > Thank you!
> 
> You can still squeeze a bit more learning out of this one:
> 
> > #include<stdio.h>
> >
> > int main(void)
> > {
> >      int row, column;
> >      int height = 4;
> >      int number = height;
> >
> >      for (row = 0; row<  height; row++)
> >      {
> >          for (column = 0 ; column<= row; column++)
> >              printf ("  ");
> >
> >          for (column = 0; column<  number; column++)
> >              printf ("# ");
> >
> 
> What if you'd been asked to print this instead:
> 
> #U#U#U#U#
> UU#U#U#U#
> UUUU#U#U#
> UUUUUU#U#
> 
> Could you just substitute U for space and be left with a working 
> program? You'd have thought so, wouldn't you? But try it and see.
> 
> A few moments ago, I posted an article in which this issue is addressed. 
> I recommend it to you.
> 
> -- 
> 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 would be happy to read the article.
Have you posted it here, or elsewhere?

[toc] | [prev] | [next] | [standalone]


#69082

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-09-03 10:39 +0100
Message-ID<rzUFx.391906$Dx3.193540@fx39.am4>
In reply to#69079
On 03/09/2015 10:33, Alla _ wrote:
> On Thursday, September 3, 2015 at 12:23:57 PM UTC+4, Richard Heathfield wrote:

<snip>

>>  What if you'd been asked to print this instead:
>>
>>  #U#U#U#U#
>>  UU#U#U#U#
>>  UUUU#U#U#
>>  UUUUUU#U#
>>
>>  Could you just substitute U for space and be left with a working
>>  program? You'd have thought so, wouldn't you? But try it and see.
>>
>>  A few moments ago, I posted an article in which this issue is addressed.
>>  I recommend it to you.
>
> I would be happy to read the article.
> Have you posted it here, or elsewhere?

In this very thread, about an hour and a quarter ago (09:16 according to 
my newsreader).

Message-ID: <KlTFx.420668$aP2.47872@fx38.am4> if that helps.

-- 
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]


#69086

FromAlla _ <modelling.data@gmail.com>
Date2015-09-03 03:11 -0700
Message-ID<c204f915-d162-4543-83f5-9d5f31e4dd86@googlegroups.com>
In reply to#69082
On Thursday, September 3, 2015 at 1:39:44 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 10:33, Alla _ wrote:
> > On Thursday, September 3, 2015 at 12:23:57 PM UTC+4, Richard Heathfield wrote:
> 
> <snip>
> 
> >>  What if you'd been asked to print this instead:
> >>
> >>  #U#U#U#U#
> >>  UU#U#U#U#
> >>  UUUU#U#U#
> >>  UUUUUU#U#
> >>
> >>  Could you just substitute U for space and be left with a working
> >>  program? You'd have thought so, wouldn't you? But try it and see.
> >>
> >>  A few moments ago, I posted an article in which this issue is addressed.
> >>  I recommend it to you.
> >
> > I would be happy to read the article.
> > Have you posted it here, or elsewhere?
> 
> In this very thread, about an hour and a quarter ago (09:16 according to 
> my newsreader).
> 
> Message-ID: <KlTFx.420668$aP2.47872@fx38.am4> if that helps.
> 
> -- 
> 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, then it is already part of my gold mine
of options.

[toc] | [prev] | [next] | [standalone]


#69429

FromAlla _ <modelling.data@gmail.com>
Date2015-09-05 04:40 -0700
Message-ID<a09fc14a-4277-47e4-8ed6-081bf4b51adb@googlegroups.com>
In reply to#68988
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, Ben and 
Keith (I am so happy I know now how to use
a flag to add C99). 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] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c


csiph-web