Re: calendar problem
From: Rob Somers (kermitt_at_gto.net)
Date: 10/17/03
- Next message: Dennis Ritchie: "Re: why still use C?"
- Previous message: Peter : "Re: Why are these different?"
- In reply to: Mike Wahler: "Re: calendar problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 16 Oct 2003 17:35:24 -0700
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message news:<3pjjb.2335$s93.307@newsread3.news.pas.earthlink.net>...
> "Rob Somers" <kermitt@gto.net> wrote in message
> news:275b1e05.0310151248.2c87b67@posting.google.com...
> > 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
- Next message: Dennis Ritchie: "Re: why still use C?"
- Previous message: Peter : "Re: Why are these different?"
- In reply to: Mike Wahler: "Re: calendar problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|