Home > Archive > C > October 2005 > Calendar Program
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| tigrfire 2005-10-30, 6:56 pm |
| 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
w
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 w 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
| |
| osmium 2005-10-30, 9:56 pm |
| "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
> w
> 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 w 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.
| |
| tigrfire 2005-10-31, 3:55 am |
| 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.
| |
| Keith Thompson 2005-10-31, 3:55 am |
| "tigrfire" <bbarcelo@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) kst-u@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.
| |
| Walter Roberson 2005-10-31, 3:55 am |
| In article <ln64res2ua.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@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
| |
| Walter Roberson 2005-10-31, 3:55 am |
| In article <1130733113.250905.220150@g47g2000cwa.googlegroups.com>,
tigrfire <bbarcelo@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
| |
| Keith Thompson 2005-10-31, 3:55 am |
| "tigrfire" <bbarcelo@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) kst-u@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.
| |
| Ian Malone 2005-10-31, 7:55 am |
| 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
> w
> 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 w 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
| |
| Jordan Abel 2005-10-31, 7:55 am |
| On 2005-10-31, tigrfire <bbarcelo@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.
| |
| Simon Biber 2005-10-31, 6:56 pm |
| 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
> w
> 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 w 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 w ),
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 w \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 w 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.
| |
| tigrfire 2005-10-31, 9:55 pm |
| 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.
|
|
|
|
|