By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,460 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,460 IT Pros & Developers. It's quick & easy.

Calendar Program

P: n/a
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0

*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
Nov 15 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
"tigrfire" writes:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0

*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
.
.
.

(output continues for all 12 months)

I'm not asking for source code necessarily, not that it wouldn't help,
but I'd just like some suggesstions on how to either modify or change
my current code in order to achieve a program that's not just a bunch
of wasted, repetitive code, like it currently is.

Here's what I've written, the problem is, it will only work, obviously,
for when there's no leap year, and January 1 comes on a Sunday. I don't
have a lot of programming experience and pretty much everything you see
here is all I can utilize.

<snip>

For the "meaty" part of the program, the part that displays the actual
numbers, you want a function that can print 28, 39, 30 or 31 days with a gap
at the beginning of from 0 to 6 days. So I suggest trying to write a
function suggested by the following prototype.

void print_month(int skip, int tot_days); /* 0..6, 28..31*/

This may suggest subsidiary functions, it may not. It kind of depends on
how your mind works.

The header that goes Sun Mon ,,, Sat could be called as the first line of
the above function, it's always the same.

The line that prints the name of the month could be in a two dimensional
array ( a constant) so you merely provide the month number to cause it to be
printed. You could modify print_month so it could print the name of the
month as well. Two dimensional arrays are a bit nasty so do this last.
Nov 15 '05 #2

P: n/a
thanks for the response but i cant use any functions besides main nor
can i use arrays. pretty much everything i have written already is the
total number of things i can utilize to do this.

Nov 15 '05 #3

P: n/a
"tigrfire" <bb******@gmail.com> writes:
thanks for the response but i cant use any functions besides main nor
can i use arrays. pretty much everything i have written already is the
total number of things i can utilize to do this.


Please don't assume that everyone can see the article to which you're
replying. You need to provide some context. See 99% of the followup
articles in this newsgroup for examples of how to quote enough text to
make each article make sense on its own. Google makes it gratuitously
difficult to do this properly.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Having said that, *why* can't you use any functions besides main or
arrays? I can't think of any good reason for such restrictions.

Unless this is homework. If so, you should have said so in the first
place -- and based on what you've said so far, it's a ridiculous
assignment.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Having said that, *why* can't you use any functions besides main or
arrays? I can't think of any good reason for such restrictions. Unless this is homework. If so, you should have said so in the first
place -- and based on what you've said so far, it's a ridiculous
assignment.


The first comment of the OP's code was:

/* Homework 1
Calendar Program */

--
If you lie to the compiler, it will get its revenge. -- Eric Sosman
Nov 15 '05 #5

P: n/a
It is a homework program, I'm not trying to lie to you. I didn't ask
for the answer, I asked for suggestions, please don't flame me without
understanding what I asked for. I didn't say source code couldn't be
posted here, not that I would copy it, just that it would still be of
aid to me. I merely asked for suggestions or alternative ways of doing
the program, not that it be written for me and I don't interpret that
as cheating in any way.

Nov 15 '05 #6

P: n/a
In article <11**********************@g47g2000cwa.googlegroups .com>,
tigrfire <bb******@gmail.com> wrote:
thanks for the response but i cant use any functions besides main nor
can i use arrays. pretty much everything i have written already is the
total number of things i can utilize to do this.


Are you allowed to use macros?
Are you allowed to use the question-colon operator ?: ?
Are you allowed to use pointers?
Are you allowed to use string literals such as "January" ?
Are you allowed to use the 'for' statement?
Are you allowed to use the % operator? The bitwise and operator?
Multiplication and division?

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Nov 15 '05 #7

P: n/a
"tigrfire" <bb******@gmail.com> writes:
It is a homework program, I'm not trying to lie to you. I didn't ask
for the answer, I asked for suggestions, please don't flame me without
understanding what I asked for. I didn't say source code couldn't be
posted here, not that I would copy it, just that it would still be of
aid to me. I merely asked for suggestions or alternative ways of doing
the program, not that it be written for me and I don't interpret that
as cheating in any way.


Ok, I didn't notice the "Homework 1" comment; sorry (and thanks to
Walter for pointing it out).

You still need to provide context when you post a followup. Please
see my previous article for instructions.

And you tell us you can't use functions other than main() or arrays.
We have no idea what other silly restrictions you're operating under,
which limits how much help we can offer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8

P: n/a
tigrfire wrote:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0


As you said in another post you can't use functions or arrays.
I'm not sure what the aim of the exercise is. Except maybe
to force you to do it this way and then show you how it should
be done.

Since what you basically have is one function to be called in a
loop (does a month of a given number of days, starting from a
specified day) it's possible to either call main recursively
(more trouble than it's worth, especially given that you can't
use arrays) or put the statements in a loop.

At which point the array restriction starts to bite. Of course
an array is little more than an adjacent list of objects.
You'll just have to consider how you might do;
char *array[12], c;
array[0] = "January";
array[1] = "February";
/* ... initialise rest of array[] and ii to something sensible */
c = array[ii];
without the convenience of [] doing the indexing for you.

(And finally, if you're not allowed to use arrays, are you
allowed to use string literals?)

--
imalone
Nov 15 '05 #9

P: n/a
On 2005-10-31, tigrfire <bb******@gmail.com> wrote:
It is a homework program, I'm not trying to lie to you. I didn't ask
for the answer, I asked for suggestions, please don't flame me without
understanding what I asked for. I didn't say source code couldn't be
posted here, not that I would copy it, just that it would still be of
aid to me. I merely asked for suggestions or alternative ways of doing
the program, not that it be written for me and I don't interpret that
as cheating in any way.


Well, the most sensible "alternative ways" are ones you've said
you're forbidden to do. You could fake functions using a few state
variables and gotos, but I seriously doubt that would go over well
with your instructor. That really is an assignment much more
complicated than it's reasonable to expect someone to do without
using subroutines. Your teacher is a jerk.
Nov 15 '05 #10

P: n/a
tigrfire wrote:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0

*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
.
.
.

(output continues for all 12 months)


If you're not allowed to use arrays, how about a switch statement? Is
that allowed?

It turns out the % operator is very useful in cases like this. You can
use it to work out when to insert a newline (at the end of each week),
and also use it to work out what the daycode of the next month will be.

Here's a full solution. I trust you won't submit it and pretend it is
your own work.
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int daycode;
int leapyear;
int month;

/* Display of Calendar */
printf(" MONTHLY CALENDAR\n\n");

printf("This program displays a calendar. You need to provide the"
" day of the week\non which January 1 falls, and indicate"
" whether or not the year is a leap year.");
printf("\n\nEnter the code number for the day of the week on which "
"January 1 falls:\n"
"0-Sun 1-Mon 2-Tue 3-Wed 4-Thu 5-Fri 6-Sat");
printf("\n\nEnter day code now (0-6): " );
fflush(stdout);
if(scanf("%d", &daycode) != 1 || daycode < 0 || daycode > 6)
{
printf("Invalid input\n");
exit(EXIT_FAILURE);
}

printf("\nIs the calendar for a leap year? (type 1 for yes, 0 for"
" no):");
fflush(stdout);
if(scanf("%d", &leapyear) != 1 || leapyear < 0 || leapyear > 1)
{
printf("Invalid input\n");
exit(EXIT_FAILURE);
}

for(month = 1; month <= 12; month++)
{
int num_days;
int day;

switch(month)
{
case 1: num_days = 31; printf("January\n\n"); break;
case 2: num_days = 28 + leapyear; printf("February\n\n"); break;
case 3: num_days = 31; printf("March\n\n"); break;
case 4: num_days = 30; printf("April\n\n"); break;
case 5: num_days = 31; printf("May\n\n"); break;
case 6: num_days = 30; printf("June\n\n"); break;
case 7: num_days = 31; printf("July\n\n"); break;
case 8: num_days = 31; printf("August\n\n"); break;
case 9: num_days = 30; printf("September\n\n"); break;
case 10: num_days = 31; printf("October\n\n"); break;
case 11: num_days = 30; printf("November\n\n"); break;
case 12: num_days = 31; printf("December\n\n"); break;
default: printf("Internal error, whoops!\n"); exit(EXIT_FAILURE);
}

printf("Sun Mon Tue Wed Thu Fri Sat\n");
for(day = 0; day < daycode; day++)
{
printf(" ");
}
for(day = 1; day <= num_days; day++)
{
printf("%3d ", day);

/* determine when to output newline */
if(day != num_days && (day + daycode) % 7 == 0) printf("\n");
}
printf("\n\n");

/* determine the daycode for the next month */
daycode = (daycode + num_days) % 7;
}
return 0;
}

--
Simon.
Nov 15 '05 #11

P: n/a
You've all been very helpful, especially Simon, who guessed correctly
that I could use the switch statement. As far as all your other
questions go, I couldn't use anything from the questions you asked
besides the for statement and multiplication and division. Hopefully
I'll be able to do this by tommorow night, when it's due, and I'll post
my final results here. Thanks for all your help everyone.

Nov 15 '05 #12

P: n/a
AK

tigrfire wrote:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0

*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
.
.
.

(output continues for all 12 months)

I'm not asking for source code necessarily, not that it wouldn't help,
but I'd just like some suggesstions on how to either modify or change
my current code in order to achieve a program that's not just a bunch
of wasted, repetitive code, like it currently is.

Here's what I've written, the problem is, it will only work, obviously,
for when there's no leap year, and January 1 comes on a Sunday. I don't
have a lot of programming experience and pretty much everything you see
here is all I can utilize.


Hi

Is this excactly what your teacher has given you? Why I ask is, because
I got a similar assignment from my teacher which asked me to get only 2
i/ps - month and year and display the calendar of that month in that
year as output formatted as in your case. Besides, there is one problem
with your assignment. If you are in window's command mode, your full
output won't be visible in the screen since it is too long. Anyway, it
is a class assignment, a ridiculous one too.

Regards
AK
Owner
Programmer's HQ
http://groups.google.com/group/programhq

Nov 15 '05 #13

P: n/a
It's possible you either attend my university, or have a teacher who
got his/her assignment from the same place. In any case, I wasn't able
to take much from Simon, this being my first programming assignment
ever and having only had a week of experience, though his was the one I
was most able to understand, so kudos again. I think I'm calling it
quits for tonight though, unable to get this format to work out for me,
I'm kind of at a stand still. If anyone wants to run through it and
figure out why it doesn't work, I won't stop them and it'd help me out
a ton. Thanks.
#include <stdio.h>

int main()
{

/* declarations */
int daycode, leapyear, monthnum, daynum, firstday, monthdays;
monthnum=1;

/* Display of Calendar */
printf(" MONTHLY CALENDAR\n\n");

printf("This program displays a calendar. You need to provide the day
of the week\non which January 1 falls, and indicate whether or not the
year is a leap year.");

printf("\n\nEnter the code number for the day of the week on which
January 1 falls:\n0-Sun 1-Mon 2-Tue 3-Wed 4-Thu 5-Fri
6-Sat");

printf("\n\nEnter day code now (0-6): " );
scanf("%d",&daycode);

printf("\nIs the calendar for a leap year? (type 1 for yes, 0 for no):
");
scanf("%d", &leapyear);

while (monthnum<=12)
{
if (daycode==0)
{
printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
if (monthnum==1||3||5||7||8||11||12)
{
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=14)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=21)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=31)
{
printf(" %d ",daynum);
daynum++;
}
}
else if (monthnum==4||6||9||10)
{
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=14)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=21)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=30)
{
printf(" %d ",daynum);
daynum++;
}
}
else if (monthnum==2)
{
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=14)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=21)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
if (leapyear==0)
{
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
}
else
{
while (daynum<=29)
{
printf(" %d ",daynum);
daynum++;
}
}
}
else
{
return 0;
}
}

else if (daycode==1)
{
printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
{
if (monthnum==(1||3||5||7||8||11||12))
{
printf(" ");
while (daynum <= 6)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=13)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=20)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=27)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=31)
{
printf(" %d ",daynum);
daynum++;
}
}
else if (monthnum==(4||6||9||10))
{
while (daynum <= 6)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=13)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=20)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=27)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=30)
{
printf(" %d ",daynum);
daynum++;
}
}
else
{
while (daynum <= 6)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=13)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=20)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=27)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
if (leapyear==0)
{
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
}
else
{
while (daynum<=29)
{
printf(" %d ",daynum);
daynum++;
}
}
}
}
}
monthnum++;
printf("\n\n");
}
return 0;
}

Nov 15 '05 #14

P: n/a

"tigrfire" <bb******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
It's possible you either attend my university, or have a teacher who
got his/her assignment from the same place. In any case, I wasn't able
to take much from Simon, this being my first programming assignment
ever and having only had a week of experience, though his was the one I
was most able to understand, so kudos again. I think I'm calling it
quits for tonight though, unable to get this format to work out for me,
I'm kind of at a stand still. If anyone wants to run through it and
figure out why it doesn't work, I won't stop them and it'd help me out
a ton. Thanks.
I didn't try to compile or run your code, or
look at it in depth, but one problem did jump
out at me. See below.


#include <stdio.h>

int main()
{

/* declarations */
int daycode, leapyear, monthnum, daynum, firstday, monthdays;
monthnum=1;

/* Display of Calendar */
printf(" MONTHLY CALENDAR\n\n");

printf("This program displays a calendar. You need to provide the day
of the week\non which January 1 falls, and indicate whether or not the
year is a leap year.");

printf("\n\nEnter the code number for the day of the week on which
January 1 falls:\n0-Sun 1-Mon 2-Tue 3-Wed 4-Thu 5-Fri
6-Sat");

printf("\n\nEnter day code now (0-6): " );
scanf("%d",&daycode);

printf("\nIs the calendar for a leap year? (type 1 for yes, 0 for no):
");
scanf("%d", &leapyear);

while (monthnum<=12)
{
if (daycode==0)
{
printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
if (monthnum==1||3||5||7||8||11||12)


This doesn't do what you probably think. Here, you're
comparing 'monthnum' only once, to a single value, not
against several values, as it appears you believe.

The expression '1||3||5||7||8||11||12' will yield a single
value, which will always be true. Then you compare it
with 'monthnum'. What you probably want is:

if(monthnum == 1 || monthnum == 3 || monthnum == 5 /* etc */)

A 'shortcut' way to do this would be with a 'switch', letting
each 'case' 'fall through to the next, e.g.:

switch(monthnum)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 11:
case 12:
/* do your stuff */
break;
/* and I see the same issue further down in your code, so
we just keep going with the other values: */
case 4:
case 6:
case 9:
case 10:
/* do your stuff */
break;
default:
/* something wrong */
break;
}

But doesn't something look odd? How many days in November?
In December? :-)

Again, this was just a quick look, all I've got time
for right now.

HTH,
-Mike
Nov 15 '05 #15

P: n/a
"Mike Wahler" <mk******@mkwahler.net> writes:
"tigrfire" <bb******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... [...]
printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
if (monthnum==1||3||5||7||8||11||12)


This doesn't do what you probably think. Here, you're
comparing 'monthnum' only once, to a single value, not
against several values, as it appears you believe.

The expression '1||3||5||7||8||11||12' will yield a single
value, which will always be true. Then you compare it
with 'monthnum'.


Not quite. "==" binds more tightly than "||", so the condition is
equivalent to:

(monthnum==1) || 3 || 5 || 7 || 8 || 11 || 12

If monthnum is 1, (monthnum==1) evalutes to 1 (true), and everything
else is short-circuited. Otherwise, (monthnum==1) evalutes to 0, 3 is
true, and everything else is short-circuited. The condition as a
whole is always true.

But once you realize that it doesn't mean what you think it means,
it's probably not important to figure out just *how* it misbehaves
(unless it's part of an existing program and you're trying to track
down a bug).
What you probably want is:

if(monthnum == 1 || monthnum == 3 || monthnum == 5 /* etc */)


Yes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.