Modify it !

J

james

Hello to all,
I have made a calendar from 1900-2000(Except leap years which i do my
own!)It gives you the day when inputting the month and date.
I want to know how can i improve it in terms of complexity(of
concepts).Waiting for your creative ideas
james.
-------------------------
#include<conio.h>
#include<stdio.h>
void days(int);
int *cal(int,int,int);
void main()
{
char ch='y';
int month=0,date=0,i,year,year1,*day;
clrscr();
printf("\t\t\t\tCALENDAR \n\n\t\t\t 1900--2000\n\n");

printf("\n Enter the year(1900-2000)");
scanf("%d",&year);
printf("\n\n\tEnter the date :: ");
scanf("%d",&date);
printf("\n\n\tEnter the month :: ");
scanf("%d",&month);

switch(month)
{
case 1:
case 10:
i=0 ;
break;

case 2:
case 3:
case 11:
i=3 ;
break;

case 4:
case 7:
i=6 ;
break;

case 5:
i=1;
break;

case 6:
i=4;
break;

case 8:
i=2;
break;

case 9:
case 12:
i=5;
break;
}
day = cal(year,date,i);
//printf("\nthe main%d",*day);
days(*day);

getch();
}

int *cal(int year,int date,int i)
{
int year1,j;
static int day;
year = year%100;
year1 = year/4;
j = date+i+year+year1;
day = j%7;
//printf("the fxn%d",day);
return(&day);
}
void days(int day)
{
if(day==0)
printf("\n\n\tThe day is : SUNDAY");
else if(day==1)
printf("\n\n\tThe day is : MONDAY");
else if(day==2)
printf("\n\n\tThe day is : TUESDAY");
else if(day==3)
printf("\n\n\tThe day is : WEDNESDAY");
else if(day==4)
printf("\n\n\tThe day is : THURSDAY");
else if(day==5)
printf("\n\n\tThe day is : FRIDAY");
else
printf("\n\n\tThe day is : SATURDAY");
getch();
}
 
J

james

Don said:
How about writing a program which prints a full calendar for the
requested year?
Ya i also am thinkin about it , but as i am new to C it might take some
time.
Anyways thaks for your suggestion.
 
D

Default User

james said:
Hello to all,
I have made a calendar from 1900-2000(Except leap years which i do my
own!)It gives you the day when inputting the month and date.
I want to know how can i improve it in terms of complexity(of
concepts).Waiting for your creative ideas
james.

There's no such header in standard C. Stuff like getch() and clrscr()
aren't usually necessary. They tend to be favorites of newbies, and
then get dropped as experience is gained.
#include<stdio.h>
void days(int);
int *cal(int,int,int);
void main()

int main(void) is the form you want here.
{
char ch='y';
int month=0,date=0,i,year,year1,*day;

I don't see year1 used in main().
clrscr();
printf("\t\t\t\tCALENDAR \n\n\t\t\t 1900--2000\n\n");

Tabs for formatting is highly platform dependent. I'd use spaces, maybe
predefine some space strings.
printf("\n Enter the year(1900-2000)");
scanf("%d",&year);
printf("\n\n\tEnter the date :: ");
scanf("%d",&date);
printf("\n\n\tEnter the month :: ");
scanf("%d",&month);

This style of reading in values is highly brittle. You are much better
off reading in an entire input string and translating the result.

See http://www.eskimo.com/~scs/C-faq/q12.19.html

You also need to do an fflush(stdout) following those printf() calls.
switch(month)
{
case 1:
case 10:
i=0 ;
break;

case 2:
case 3:
case 11:
i=3 ;
break;

case 4:
case 7:
i=6 ;
break;

case 5:
i=1;
break;

case 6:
i=4;
break;

case 8:
i=2;
break;

case 9:
case 12:
i=5;
break;
}
day = cal(year,date,i);
//printf("\nthe main%d",*day);
days(*day);

getch();

What is the point of getch() here? Probably to eat that carriage return
leftover after scanf() I guess.
}

int *cal(int year,int date,int i)
{
int year1,j;
static int day;
year = year%100;
year1 = year/4;
j = date+i+year+year1;
day = j%7;
//printf("the fxn%d",day);
return(&day);

Ugh. Why are you returning the address of a static int? That turned
your code non-reentrant for no good reason that I can see. Just return
the value, and make day back in main() an int.

Also, as a style mention, don't use i for anything but a loop variable.
Especially not as a function parameter. There's no obvious meaning in
this function.
}
void days(int day)
{
if(day==0)
printf("\n\n\tThe day is : SUNDAY");
else if(day==1)
printf("\n\n\tThe day is : MONDAY");
else if(day==2)
printf("\n\n\tThe day is : TUESDAY");
else if(day==3)
printf("\n\n\tThe day is : WEDNESDAY");
else if(day==4)
printf("\n\n\tThe day is : THURSDAY");
else if(day==5)
printf("\n\n\tThe day is : FRIDAY");
else
printf("\n\n\tThe day is : SATURDAY");

This seems cumbersome. I'd use an array of strings for the day names
and index into it.

char *day_names[] = {"SUNDAY", /* etc etc */};

if (day >= 0 && day <= 6)
{
printf("\n\n\tThe day is : %s\n", day_names[day]);
}
else
{
/* error handling */
}


What's this getch() for?




Brian
 
D

Dave Thompson

Hello to all,
I have made a calendar from 1900-2000(Except leap years which i do my
own!)It gives you the day when inputting the month and date.
I want to know how can i improve it in terms of complexity(of
concepts).Waiting for your creative ideas
james.

conio.h (and clrsrc() and getch() below) are not standard C and should
not be used in code intended to be portable or (equivalently?) posted
here. Assuming that clrscr() clears the screen (and assuming there is
a screen to be cleared) for most programs this is a bad idea; I
certainly won't use (or buy) programs which totally unnecessarily
erase data I may and sometimes do want to keep.
void days(int);
int *cal(int,int,int);
void main()

main() standardly returns int, and it's better style to explicitly say
(void) for the 0-arguments version; FAQ 11.12 et seq. at usual places
and http://www.eskimo.com/~scs/C-faq/top.html .
{
char ch='y';
int month=0,date=0,i,year,year1,*day;
clrscr();
printf("\t\t\t\tCALENDAR \n\n\t\t\t 1900--2000\n\n");
You don't use ch for anything. 'date' is ambiguous, and you actually
use it for day-of-month; something like 'dom' or 'mday' would be
clearer, or 'day' and use 'dow' for what you now call 'day'.
printf("\n Enter the year(1900-2000)");
scanf("%d",&year);
printf("\n\n\tEnter the date :: ");
scanf("%d",&date);
printf("\n\n\tEnter the month :: ");
scanf("%d",&month);
Depending on how accurately your C implementation can identify
'interactive' input and output streams, it isn't guaranteed your
prompts will actually appear before your program waits for input. To
be certain add fflush(stdout). You don't check for input error (user
doesn't type numbers, etc.) or values out of valid ranges. I would
accept command-line (args to main) instead at least optionally.
switch(month)
{
case 1:
case 10:
i=0 ;
break;

case 2:
case 3:
case 11:
i=3 ;
break;

case 4:
case 7:
i=6 ;
break;

case 5:
i=1;
break;

case 6:
i=4;
break;

case 8:
i=2;
break;

case 9:
case 12:
i=5;
break;
}
You could replace all this by an array (declared at top in C90):
static const int startdow[12] = {0,3,3,4,1,4,6,2,5,0,3,5};
... cal(year,dom,startdow[month-1]) ...
except that your figures are wrong; for 1900 it should be
{1,4,4,0,2,5,0,3,6,1,4,6}.
day = cal(year,date,i);
//printf("\nthe main%d",*day);
days(*day);

getch();
}

int *cal(int year,int date,int i)
{
int year1,j;
static int day;
year = year%100;

This will wrongly give the same result for 2000 as for 1900, if a user
follows your instructions literally. Perhaps you want to limit
yourself to 1900 to 1999 -- although 1901 (or perhaps 1904 or 1920) to
2099 (and use year - base instead of % 100) would be simpler because
you can implement only the first-level leap rule.
year1 = year/4;
j = date+i+year+year1;
day = j%7;
//printf("the fxn%d",day);
return(&day);
}

It's rather silly to return the address of a local static int when you
could easily just return the int value.
void days(int day)
{
if(day==0)
printf("\n\n\tThe day is : SUNDAY");
else if(day==1)
printf("\n\n\tThe day is : MONDAY");
else if(day==2)
printf("\n\n\tThe day is : TUESDAY");
else if(day==3)
printf("\n\n\tThe day is : WEDNESDAY");
else if(day==4)
printf("\n\n\tThe day is : THURSDAY");
else if(day==5)
printf("\n\n\tThe day is : FRIDAY");
else
printf("\n\n\tThe day is : SATURDAY");
getch();
}

This could be done more simply (and perhaps directly in the caller)
with an array of pointers to literal strings:
/*static*/ const char * wkdayname [] = {"SUNDAY", ...};
... printf ("Month begins on %s\n", wkdayname[wday]); ...

and you should end your output with a newline \n, either in the same
operation as I did or by a later putchar ('\n') or similar, to ensure
it appears; C implementations are not required to support 'partial'
last lines and some don't.

- David.Thompson1 at worldnet.att.net
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top