Dear pebbles:
Thank you for your question.
I'm assuming you're using the standard format for one week, starting
from Sunday = 1, and ending at Saturday = 7. Although I can't actually
implement this for you, I have done a similar project in the past.
You said you already know the day of the first day of the year. Let's
say it's Wednesday. If we number the weekdays starting at Sunday = 1,
Monday = 2, etc, then Wednesday will equal to 4, which is the fourth
column in your calendar. Let's call this variable "FirstDayOfYear."
Now, you should already have an array with the number of days for each
month. Let's call your array int MonthDays[x], where x is a number
corresponding to January = 1, February = 2, etc, and returns the
number of days in your month. To find the correct position of the
column for the first day of any month, use the following method:
////////////
int totaldays = 0; // initialize total days since Jan 1st to 0
totaldays += FirstDayOfYear; // add the first position of the first
date
for (int x = 1; x < currentmonth; x++) {
int totaldays += MonthDays[x];
} //add the total number of days passed in each month
//for all months passed
// now you have the total number of days passed since Jan 1st,
// and also the first date of the year
int firstdayofmonth = totaldays % 7;
// the left over portion must be the correct position of the first day
// of the current month, which is stored is "firstdayofmonth"
////////////
That's pretty much it. I hope this helps! If you need any
clarifications, please don't hesitate to ask. I would be more than
happy to assist you further.
Best Regards,
blader-ga |
Request for Answer Clarification by
pebbles-ga
on
26 Jun 2002 03:50 PDT
Dear blader-ga, your answer helps a lot, although not quite enough.
Because of my assignment specifications I cannot initialise any other
variables. Although it seems like I'm cheating but I've filled in
heaps, however this is my last hurdle and I've spent days on it and
it's not becoming any clearer. I'm with you for the most part, your
MonthDays[x] is my mondays[i], and your FirstDay of Year is my
firstDay, your Sunday = 1, is my Sunday = 0. I think your totaldays
should be my col? I cannot initialise currentmonth however, and I
don't know really how to program in the same value with the variables
I already have, as stipulated by course boss. I'm sorry to do this to
you but I'm going to send you all I have so far, it's this statement
in void setCal e.t.c that's causing me the trouble I think,
if (month == 0)
col = firstDay(yr);
else col = startCol;
speak really simple dear blader...thank you for your time.
// cal.c - calendar for 1583 on
#include <stdio.h>
#include <string.h>
FILE *myfile;
typedef int calendar[20][21];
int getYear(char []);
int firstDay(int);
void setCal(int, calendar);
void writeHead(int);
void writeCal(calendar);
int main(void)
{
calendar cal = { {0} };
char calName[] = " calendar.doc";
int yr;
yr = getYear(calName);
setCal(yr, cal);
myfile = fopen(calName, "w");
writeHead(yr);
writeCal(cal);
fclose(myfile);
printf("\nFinished\n");
return 0;
}
int getYear(char calName[])
{
char dummy[80];
int cnt, yr, y;
do
{
printf("year(dddd): ");
cnt = scanf("%d", &yr);
gets(dummy);
if (cnt == 1)
if (yr > 1582)
break;
else
printf("Year must be > 1582\n");
else
printf("Non-numeric data\n");
} while (1);
y = yr;
calName[3] = y % 10 + '0';
y /= 10;
calName[2] = y % 10 + '0';
y /= 10;
calName[1] = y % 10 + '0';
calName[0] = y / 10 + '0';
return yr;
}
int firstDay(int yr) // using Zeller's congruence
{
int a, b, c, d, dd = 1, m, mm = 1;
if (mm < 3)
yr--;
m = (mm + 9) % 12 + 1;
a = (int)(2.6 * m - 0.2);
b = yr % 100;
c = yr / 100;
d = (a + dd + b + b / 4 + c / 4 - 2 * c) % 7;
if (d < 0)
d += 7;
return d;
}
void setCal(int yr, calendar cal) // prepare calendar for year yr
{
int mondays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, row, col, startCol, startRow;
// you are to complete this
if ((yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0)
mondays[1] = 29;
else mondays[1] = 28;
if (month == 0)
col = firstDay(yr);
else col = startCol;
for (month = 0; month < 12; ++month)
{ startRow = (month / 3) * 5;
startCol = (month % 3) * 7;
row = startRow;
for (day = 1; day <= mondays[month]; ++day)
{
cal[row][col] = day;
if (col < startCol + 6)
++col;
else
{col = startCol;
if (row < startRow + 4)
++row;
else row = startRow;}
}
}
}
void writeHead(int yr)
{
int i, spaces, width, y1, y2, y3, y4;
char *num[10][8] = {
{" @@@ ", "@ @ ", "@ @ ", "@ @ ", "@ @ ", "@ @ ", " @@@
", "======"},
{" @ " , "@@ " , " @ " , " @ " , " @ " , " @ " , "@@@ "
, "====" },
{" @@@ ", "@ @ ", " @ ", " @ ", " @ ", " @ ", "@@@@@
", "======"},
{" @@@ ", "@ @ ", " @ ", " @@@ ", " @ ", "@ @ ", " @@@
", "======"},
{" @ ", " @@ ", " @ @ ", "@ @ ", "@@@@@ ", " @ ", " @
", "======"},
{"@@@@@ ", "@ ", "@ ", "@@@@ ", " @ ", "@ @ ", " @@@
", "======"},
{" @@@ ", "@ ", "@ ", "@@@@ ", "@ @ ", "@ @ ", " @@@
", "======"},
{"@@@@@ ", " @ ", " @ ", " @ ", " @ ", "@ ", "@
", "======"},
{" @@@ ", "@ @ ", "@ @ ", " @@@ ", "@ @ ", "@ @ ", " @@@
", "======"},
{" @@@ ", "@ @ ", "@ @ ", " @@@@ ", " @ ", " @ ", " @@@
", "======"}};
y4 = yr % 10;
yr /= 10;
y3 = yr % 10;
yr /= 10;
y2 = yr % 10;
y1 = yr / 10;
spaces = (int)(strlen(num[y1][0]) + strlen(num[y2][0])
+ strlen(num[y3][0]) + strlen(num[y4][0]));
spaces = 3 + (70 - spaces) / 2;
width = (int)strlen(num[y4][0]) - 1;
for (i = 0; i < 8; i++)
fprintf(myfile, "%*c%s%s%s%.*s\n", spaces, ' ', num[y1][i],
num[y2][i],
num[y3][i], width, num[y4][i]);
}
void writeCal(calendar cal)
{
char *head[] = {"JANUARY FEBRUARY
MARCH",
"APRIL MAY JUNE",
"JULY AUGUST SEPTEMBER",
"OCTOBER NOVEMBER DECEMBER"};
char days[] = " S M T W Th F S S M T W Th F S S
M T W Th F S";
int i, row, col;
i = 0;
for (row = 0; row < 20; ++row)
{ if (row == 0)
{printf(" %s\n", head[i]);
printf("%s\n", days);}
else if (row == 5)
{printf(" %s\n", head[i + 1]);
printf("%s\n", days);}
else if (row == 10)
{printf(" %s\n", head[i + 2]);
printf("%s\n", days);}
else if (row == 15)
{printf(" %s\n", head[i + 3]);
printf("%s\n", days);}
for (col = 0; col < 21; ++col)
if (cal[row][col] == 0)
printf("%3c", ' ');
else if (col == 0)
printf("%2c %3d", ' ', cal[row][col]);
else if (col == 7)
printf("%2c %3d", ' ', cal[row][col]);
else if (col == 14)
printf("%2c %3d", ' ', cal[row][col]);
else printf("%3d", cal[row][col]);
printf("\n");
}
// you are to complete this
}
|
Clarification of Answer by
blader-ga
on
26 Jun 2002 12:19 PDT
Dear pebbles:
Thank you for your clarification request. I've tried my best to decode
your assignment, but I'm not having much luck. For some reason, (most
likely due to Google's automatic formatting) I can't compile your
program. It would be great if you could upload the source file
somewhere so I can download it, because I can't give you my contact
information, it's Google Answers policy. Is the function below your
only problem? Or do you not know exactly how much work is required to
fix your problem? There appears to be two places that your assignment
requires you to finish, and the below function appears to be only one
of them.
void setCal(int yr, calendar cal) // prepare calendar for year yr
{
int mondays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, row, col, startCol, startRow;
// you are to complete this
if ((yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0)
mondays[1] = 29;
else mondays[1] = 28;
if (month == 0)
col = firstDay(yr);
else col = startCol;
for (month = 0; month < 12; ++month)
{ startRow = (month / 3) * 5;
startCol = (month % 3) * 7;
row = startRow;
for (day = 1; day <= mondays[month]; ++day)
{
cal[row][col] = day;
if (col < startCol + 6)
++col;
else
{col = startCol;
if (row < startRow + 4)
++row;
else row = startRow;}
}
}
}
It seems to me though, that it would make more sense if you were to
move
if (month == 0)
col = firstDay(yr);
else col = startCol;
from the beginning of the function to inside the for loop, at the very
top of the loop, because month isn't initialized until the loop
begins.
I will be happy to help you further with your problem. Please respond
with a clarification request.
Best Regards,
blader-ga
|
Request for Answer Clarification by
pebbles-ga
on
26 Jun 2002 14:10 PDT
Dear blader-ga, yes it is the function void setCal which is causing me
difficulty, the rest seems to work, no web page I'm sorry! I do not
know how to adjust col for every month so each month starts on the
right column. You are right about putting my if question inside the
loop.
|
Clarification of Answer by
blader-ga
on
26 Jun 2002 14:33 PDT
Dear pebbles:
Can you remove all of your added code in in the setCal() function and
paste to me the original code that you were given please? Thanks.
Best Regards,
blader-ga
|
Request for Answer Clarification by
pebbles-ga
on
26 Jun 2002 15:34 PDT
Dear bladar-ga, sure. I'll send you the whole original file as I had
to do void writeCal too and I may also havedone that incorrectly,
cheers again.
// cal.c - calendar for 1583 on
#include <stdio.h>
#include <string.h>
FILE *myfile;
typedef int calendar[20][21];
int getYear(char []);
int firstDay(int);
void setCal(int, calendar);
void writeHead(int);
void writeCal(calendar);
int main(void)
{
calendar cal = { {0} };
char calName[] = " calendar.doc";
int yr;
yr = getYear(calName);
setCal(yr, cal);
myfile = fopen(calName, "w");
writeHead(yr);
writeCal(cal);
fclose(myfile);
printf("\nFinished\n");
return 0;
}
int getYear(char calName[])
{
char dummy[80];
int cnt, yr, y;
do
{
printf("year(dddd): ");
cnt = scanf("%d", &yr);
gets(dummy);
if (cnt == 1)
if (yr > 1582)
break;
else
printf("Year must be > 1582\n");
else
printf("Non-numeric data\n");
} while (1);
y = yr;
calName[3] = y % 10 + '0';
y /= 10;
calName[2] = y % 10 + '0';
y /= 10;
calName[1] = y % 10 + '0';
calName[0] = y / 10 + '0';
return yr;
}
int firstDay(int yr) // using Zeller's congruence
{
int a, b, c, d, dd = 1, m, mm = 1;
if (mm < 3)
yr--;
m = (mm + 9) % 12 + 1;
a = (int)(2.6 * m - 0.2);
b = yr % 100;
c = yr / 100;
d = (a + dd + b + b / 4 + c / 4 - 2 * c) % 7;
if (d < 0)
d += 7;
return d;
}
void setCal(int yr, calendar cal) // prepare calendar for year yr
{
int mondays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, row, col, startCol, startRow;
// you are to complete this
}
void writeHead(int yr)
{
int i, spaces, width, y1, y2, y3, y4;
char *num[10][8] = {
{" @@@ ", "@ @ ", "@ @ ", "@ @ ", "@ @ ", "@ @ ", " @@@
", "======"},
{" @ " , "@@ " , " @ " , " @ " , " @ " , " @ " , "@@@ "
, "====" },
{" @@@ ", "@ @ ", " @ ", " @ ", " @ ", " @ ", "@@@@@
", "======"},
{" @@@ ", "@ @ ", " @ ", " @@@ ", " @ ", "@ @ ", " @@@
", "======"},
{" @ ", " @@ ", " @ @ ", "@ @ ", "@@@@@ ", " @ ", " @
", "======"},
{"@@@@@ ", "@ ", "@ ", "@@@@ ", " @ ", "@ @ ", " @@@
", "======"},
{" @@@ ", "@ ", "@ ", "@@@@ ", "@ @ ", "@ @ ", " @@@
", "======"},
{"@@@@@ ", " @ ", " @ ", " @ ", " @ ", "@ ", "@
", "======"},
{" @@@ ", "@ @ ", "@ @ ", " @@@ ", "@ @ ", "@ @ ", " @@@
", "======"},
{" @@@ ", "@ @ ", "@ @ ", " @@@@ ", " @ ", " @ ", " @@@
", "======"}};
y4 = yr % 10;
yr /= 10;
y3 = yr % 10;
yr /= 10;
y2 = yr % 10;
y1 = yr / 10;
spaces = (int)(strlen(num[y1][0]) + strlen(num[y2][0])
+ strlen(num[y3][0]) + strlen(num[y4][0]));
spaces = 3 + (70 - spaces) / 2;
width = (int)strlen(num[y4][0]) - 1;
for (i = 0; i < 8; i++)
fprintf(myfile, "%*c%s%s%s%.*s\n", spaces, ' ', num[y1][i],
num[y2][i],
num[y3][i], width, num[y4][i]);
}
void writeCal(calendar cal)
{
char *head[] = {"JANUARY FEBRUARY
MARCH",
"APRIL MAY JUNE",
"JULY AUGUST SEPTEMBER",
"OCTOBER NOVEMBER DECEMBER"};
char days[] = " S M T W Th F S S M T W Th F S S
M T W Th F S";
int i, row, col;
// you are to complete this
}
|
Clarification of Answer by
blader-ga
on
26 Jun 2002 17:04 PDT
Dear pebbles:
Thanks for the code. I am willing to take as long as it takes to guide
you in the right direction, but I am afraid that I can not write any
actual code for you. I just feel that that would not be ethical to do,
since I'm pretty sure this a school assignment.
I understand how your calendar works now. I didn't really understand
what you have before, but here's the way I believe you would implement
this:
You already know the first day of the month for the first month of the
year, through the firstDay(), correct? What you need to do to find out
the correct day for the next month is add the number of days in the
first month to firstDay(), and the mod (%) by 7. This will give you
the "left over" off set that you need. Then you set the startCol to
this new left over offset, and repeat this for the next month.
E.g., let's say the firstDay() is 3. Set that to your current
startCol. Then what you would do, is add 31 (the number of days in
January) to that, and you get 34. Mod 7, and you have 5 left over.
Your correct off set is 5, and you set that as your new startCol. For
March, you add the number of days in March to your new startCol, and
mod 7, etc.
I hope this was clear enough to get you going. If you need any
additional clarifications, please don't hesitate to ask. I will be
here for however long it takes for it to work.
Best Regards,
blader-ga
|
Request for Answer Clarification by
pebbles-ga
on
26 Jun 2002 19:58 PDT
Dear Blader-ga, this is not really a request for clarification,
somebody else from Kasamba has also been helping me and seems to have
fixed my problem. I couldn't interfere with the variable startCol
because that is how the rest of each month, except for the first line
of said month, knows where to start. He explained that col at the end
of the loop is already at the correct place to begin the next month,
(except for something screwy with march where I had to adjust again,
still don't completely understand that bit but working on it), so the
equation I was looking for (except for march) was col = col +
startCol! so simple once you know. Anyhow, thank you very much again
but I'm all fixed, I will still pay you for your time if need be as
you've been so helpful and I know we eventually would have fixed it
but I was too impatient...
|
Clarification of Answer by
blader-ga
on
26 Jun 2002 21:08 PDT
Dear pebbles:
I'm sorry I couldn't have helped you a bit faster, but I am happy that
you have fixed your problem. The solution you posted does make sense.
I was planning on guiding you through to the end, but it turns out
that the solution was rather simple. Thanks for your time. =)
Best Regards,
blader-ga
|