calendar problem

R

Rob Somers

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.
 
E

Eric Sosman

Rob said:
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.
 
M

Mike Wahler

Rob Somers said:
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
 
M

Mike Wahler

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
 
R

Rob Somers

Mike Wahler said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top