473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

calendar problem

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.
Nov 13 '05 #1
4 2910
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.

--
Er*********@sun.com
Nov 13 '05 #2

"Rob Somers" <ke*****@gto.net> wrote in message
news:27*************************@posting.google.co m...
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
Nov 13 '05 #3

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:3p****************@newsread3.news.pas.earthli nk.net...

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
Nov 13 '05 #4
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<3p****************@newsread3.news.pas.earthl ink.net>...
"Rob Somers" <ke*****@gto.net> wrote in message
news:27*************************@posting.google.co m...
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
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: DFS | last post by:
If you're listening, I want the middle of the calendar (showing 1 month) to open below the cursor position. It currently opens just to the right and below the cursor position. I hunted through...
5
by: Miguel Dias Moura | last post by:
Hello, i am trying to create a .css file with several styles and apply them to the calendar control so i can change the look of: 1. Text Type and Format (Bold, Underline, etc) 2. Background...
0
by: maxrawson | last post by:
First, let me start by saying my asp.net experience is still in it's infancy so please bare with me as I try to explain my situation. I have created a single page that with the use of many...
2
by: Caesar Augustus | last post by:
First, let me start by saying my asp.net experience is still in it's infancy so please bare with me as I try to explain my situation. I have created a single page that with the use of many...
3
by: =?Utf-8?B?UGFycm90?= | last post by:
I applied the following Ajax code in my web page which has a calendar control to keep my page from completely reloading everytime something was changed. <atlas:ScriptManager ID="ScriptManager1"...
24
by: =?Utf-8?B?Tkg=?= | last post by:
I have a basic calendar and a SelectionChanged event (asp.net 2.0). How can I handle it when a user chooses the same date again, in this case the SelectionChanged event is not fired because,...
3
by: thorpk | last post by:
I posted this problem earlier in the month and some one decided it was better to change the subject and ask a completely different question. I am therefore reposting. I am hoping some one can...
0
by: mathewgk80 | last post by:
HI all, I am having popup calendar Javascript code. But i dont know how it is connecting to asp.net code.. I am using asp.net,c#.net and also using 3tier architecture with master page.... I...
4
by: gubbachchi | last post by:
Hi all, Please anybody help me solve this problem. I am stuck up with this from past 2 weeks. I am developing an application where, when the user selects date from javascript datepicker and enters...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.