calendar problem

Discussion in 'C Programming' started by Rob Somers, Oct 15, 2003.

  1. Rob Somers

    Rob Somers Guest

    Hey people

    I came across this calendar problem on another board, and so I tried
    solving it myself, but to no avail. If you run the program as it is
    now, you should see th problem of the spaces being in the wrong spot
    on the first line of output after the 'days of the week' line.

    here is the code:

    #include <stdio.h>

    int main(void)
    {

    int i, n, day;

    printf( "Enter number of days in the month:" );
    scanf( "%d",&n );
    printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
    scanf( "%d",&day );

    printf( "\n S M T W T F S \n\n" );

    for( i = 1; i <= n ; i++ ){
    printf( "%3d", i );

    if( ( i + day ) % 7 == 0 ){
    printf( "\n" );
    }
    }

    printf( "\n\n" );
    return 0;
    }

    I had wanted to solve it just for my own curiosity, but I think it is
    beyond me.
     
    Rob Somers, Oct 15, 2003
    #1
    1. Advertising

  2. Rob Somers

    Eric Sosman Guest

    Rob Somers wrote:
    >
    > Hey people
    >
    > I came across this calendar problem on another board, and so I tried
    > solving it myself, but to no avail. If you run the program as it is
    > now, you should see th problem of the spaces being in the wrong spot
    > on the first line of output after the 'days of the week' line.
    >
    > here is the code:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    >
    > int i, n, day;
    >
    > printf( "Enter number of days in the month:" );


    See Question 12.4 in the comp.lang.c Frequently
    Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html

    for an explanation of why this might not always work
    and of how you should fix it.

    > scanf( "%d",&n );


    scanf() can fail, and you should check whether it
    succeeded before proceeding. See Questions 12.19 and
    12.20 for some ideas of how to handle the obstreperous
    or fat-fingered user who enters "e^H31". You should
    probably also check for legitimate numeric input with
    bogus values: a month of minus forty-two days, for
    example.

    > printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
    > scanf( "%d",&day );


    Same remarks as above.

    > printf( "\n S M T W T F S \n\n" );
    >
    > for( i = 1; i <= n ; i++ ){
    > printf( "%3d", i );


    Your problem occurs the first time you arrive at
    this printf() call. The preceding printf() ended with
    a pair of newlines, so the "output position" is at the
    start of a fresh output line. Thus, the very first
    number you print will occupy the first three spaces on
    that line -- which is exactly where you want it for a
    month beginning on Sunday, but is too far to the left
    for any other weekday. (This is a new twist on the
    old phrase about "a month of Sundays.")

    There are at least two ways to fix this. The simplest
    is to decide how many blank days begin the first week, and
    to printf() enough space for them before entering the loop.
    The tricky way (mostly useful for proving to yourself how
    very clever you are) is to expand the width of the first
    output field, changing it from a constant 3 to a variable
    amount depending on how much offset is needed; a format
    specifier like "%*d" and *two* argument values would do
    the trick. Personally, I'd stick with the simple method.

    > if( ( i + day ) % 7 == 0 ){
    > printf( "\n" );
    > }
    > }
    >
    > printf( "\n\n" );
    > return 0;
    > }
    >
    > I had wanted to solve it just for my own curiosity, but I think it is
    > beyond me.


    Not so very far beyond, I'd say; you're fairly close.
    Two glaring problems and one subtle error is a better-than-
    average outcome in these parts.

    For extra credit, modify the corrected program so it
    allows the user to specify that the week runs from Sunday
    through Saturday, as above, or from Monday through Sunday.

    --
     
    Eric Sosman, Oct 15, 2003
    #2
    1. Advertising

  3. Rob Somers

    Mike Wahler Guest

    "Rob Somers" <> wrote in message
    news:...
    > Hey people
    >
    > I came across this calendar problem on another board, and so I tried
    > solving it myself, but to no avail. If you run the program as it is
    > now, you should see th problem of the spaces being in the wrong spot
    > on the first line of output after the 'days of the week' line.
    >
    > here is the code:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    >
    > int i, n, day;
    >
    > printf( "Enter number of days in the month:" );
    > scanf( "%d",&n );
    > printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
    > scanf( "%d",&day );
    >
    > printf( "\n S M T W T F S \n\n" );
    >
    > for( i = 1; i <= n ; i++ ){
    > printf( "%3d", i );
    >
    > if( ( i + day ) % 7 == 0 ){
    > printf( "\n" );
    > }
    > }
    >
    > printf( "\n\n" );
    > return 0;
    > }
    >
    > I had wanted to solve it just for my own curiosity, but I think it is
    > beyond me.


    But it's not. :) When you can't figure out the behavior of
    a program, watch it. This can be done with a debugger, or
    in simple cases like this, just output variables' values at
    strategic points.

    I have made no changes to original other than
    adding the 'debug' output:


    #include <stdio.h>

    /* MKW these three functions will be used for watching the code */
    void eat(void) /* MKW */
    { /* MKW */
    int c = 0; /* MKW */
    while((c = getchar()) != '\n' && c !=EOF) /* MKW */
    ; /* MKW */
    } /* MKW */

    void show(const char *what, int arg) /* MKW */
    { /* MKW */
    printf(" %s = %2d", what, arg); /* MKW */
    } /* MKW */

    void pause(void) /* MKW */
    { /* MKW */
    printf("%s", " Enter to continue"); /* MKW */
    getchar(); /* MKW */
    } /* MKW */

    int main(void)
    {
    int i, n, day;
    printf( "Enter number of days in the month:" );
    scanf( "%d",&n );
    printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
    scanf( "%d",&day );

    eat(); /* MKW 'eat' any chars left over from scanf() */
    printf( "\n S M T W T F S \n\n" );

    for( i = 1; i <= n ; i++ ){
    /* printf( "%3d", i ); */ /* MKW removed so won't interfere */
    /* with our 'debug' output */

    show("i", i); /* MKW */
    putchar(' '); /* MKW */
    show("i + day", i + day); /* MKW */
    putchar(' '); /* MKW */
    show("i + day % 7", i + day % 7); /* MKW */
    pause(); /* MKW */

    if( ( i + day ) % 7 == 0 ){
    printf( "\n" );
    }
    }
    printf( "\n\n" );
    return 0;
    }

    -Mike
     
    Mike Wahler, Oct 15, 2003
    #3
  4. Rob Somers

    Mike Wahler Guest

    Re: (corr) calendar problem

    "Mike Wahler" <> wrote in message
    news:3pjjb.2335$...

    correction:

    > void pause(void) /* MKW */
    > { /* MKW */
    > printf("%s", " Enter to continue"); /* MKW */
    > getchar(); /* MKW */


    Change to:
    eat(); /* MKW */

    > }


    This will prevent corruption of the output if any
    characters are typed before the newline, e.g.

    Enter to continue ABC<enter>


    -Mike
     
    Mike Wahler, Oct 15, 2003
    #4
  5. Rob Somers

    Rob Somers Guest

    "Mike Wahler" <> wrote in message news:<3pjjb.2335$>...
    > "Rob Somers" <> wrote in message
    > news:...
    > > Hey people
    > >
    > > I came across this calendar problem on another board, and so I tried
    > > solving it myself, but to no avail. If you run the program as it is
    > > now, you should see th problem of the spaces being in the wrong spot
    > > on the first line of output after the 'days of the week' line.
    > >
    > > here is the code:
    > >
    > > #include <stdio.h>
    > >
    > > int main(void)
    > > {
    > >
    > > int i, n, day;
    > >
    > > printf( "Enter number of days in the month:" );
    > > scanf( "%d",&n );
    > > printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
    > > scanf( "%d",&day );
    > >
    > > printf( "\n S M T W T F S \n\n" );
    > >
    > > for( i = 1; i <= n ; i++ ){
    > > printf( "%3d", i );
    > >
    > > if( ( i + day ) % 7 == 0 ){
    > > printf( "\n" );
    > > }
    > > }
    > >
    > > printf( "\n\n" );
    > > return 0;
    > > }
    > >
    > > I had wanted to solve it just for my own curiosity, but I think it is
    > > beyond me.

    >
    > But it's not. :) When you can't figure out the behavior of
    > a program, watch it. This can be done with a debugger, or
    > in simple cases like this, just output variables' values at
    > strategic points.
    >
    > I have made no changes to original other than
    > adding the 'debug' output:
    >
    >
    > #include <stdio.h>
    >
    > /* MKW these three functions will be used for watching the code */
    > void eat(void) /* MKW */
    > { /* MKW */
    > int c = 0; /* MKW */
    > while((c = getchar()) != '\n' && c !=EOF) /* MKW */
    > ; /* MKW */
    > } /* MKW */
    >
    > void show(const char *what, int arg) /* MKW */
    > { /* MKW */
    > printf(" %s = %2d", what, arg); /* MKW */
    > } /* MKW */
    >
    > void pause(void) /* MKW */
    > { /* MKW */
    > printf("%s", " Enter to continue"); /* MKW */
    > getchar(); /* MKW */
    > } /* MKW */
    >
    > int main(void)
    > {
    > int i, n, day;
    > printf( "Enter number of days in the month:" );
    > scanf( "%d",&n );
    > printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
    > scanf( "%d",&day );
    >
    > eat(); /* MKW 'eat' any chars left over from scanf() */
    > printf( "\n S M T W T F S \n\n" );
    >
    > for( i = 1; i <= n ; i++ ){
    > /* printf( "%3d", i ); */ /* MKW removed so won't interfere */
    > /* with our 'debug' output */
    >
    > show("i", i); /* MKW */
    > putchar(' '); /* MKW */
    > show("i + day", i + day); /* MKW */
    > putchar(' '); /* MKW */
    > show("i + day % 7", i + day % 7); /* MKW */
    > pause(); /* MKW */
    >
    > if( ( i + day ) % 7 == 0 ){
    > printf( "\n" );
    > }
    > }
    > printf( "\n\n" );
    > return 0;
    > }
    >
    > -Mike



    Hey people

    Thanks for your help. As it turned out, someone on the board, that I
    initially found this code on, gave a little hint as to what direction
    to take that got me started on about 3 different 'solutions' though I
    did not get an answer that worked properly until it came to me while I
    was working today - heh - my job is completely unrelated to computers
    so I had no chance to try my code. Your answers have indeed shown me
    that there is a lot more that I need to learn about programming. What
    I mean is that sometimes you cannot learn if you don't know what you
    don't know.. :) For example, the whole debugging thing is not
    something I am up to speed on at all.

    So thanks again for your help.

    Rob
     
    Rob Somers, Oct 17, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael England
    Replies:
    0
    Views:
    609
    Michael England
    Nov 26, 2003
  2. Alessandro

    Problem with object calendar

    Alessandro, Jan 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    289
    Alessandro
    Jan 13, 2004
  3. Shevek
    Replies:
    3
    Views:
    6,051
    Shevek
    Jun 23, 2004
  4. Shevek
    Replies:
    0
    Views:
    395
    Shevek
    Jun 23, 2004
  5. Parthiv Joshi
    Replies:
    1
    Views:
    709
    Samuel L Matzen
    Jul 6, 2004
Loading...

Share This Page