473,385 Members | 1,720 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,385 software developers and data experts.

Modify it !

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();
}

Nov 15 '05 #1
4 1326
Don
How about writing a program which prints a full calendar for the
requested year?

Nov 15 '05 #2


Don wrote:
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.

Nov 15 '05 #3


james wrote:
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>
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 */
}

getch();
What's this getch() for?
}



Brian

Nov 15 '05 #4
On 5 Jul 2005 06:15:53 -0700, "james" <pr**************@gmail.com>
wrote:
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>
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
Nov 15 '05 #5

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

Similar topics

1
by: Franco Fellico' | last post by:
Hi. Suppose to have read and displayed (using PHP) a group of row of a DB table on a dinamyc table on a HTML/PHP page. The number of row displayed could be from 1 to n. Each row contains...
28
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one...
13
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
12
by: Michael B Allen | last post by:
Is it legit to modify static data like the following code? #include <stdlib.h> #include <stdio.h> struct tbl { int i; char *s; };
5
by: Martin Bischoff | last post by:
Hi, is it possible to modify the values of a SqlDataSource's select parameters in the code behind before the select command is executed? Example: I have an SqlDataSource with a...
3
by: Maxwell2006 | last post by:
Hi, When I run a web service project, ASP.NET shows me a default web method invoke page. How can I disable/modify the default test (or method invoke) page of the ASP.NET web services? ...
2
by: Bob | last post by:
Hi, I have a list of widgets. I want to iterate through the selected items collection and modify one of the widgets properties. I tried foreach(widget w in lst.SelectedItems) w.MyProperty =...
6
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified...
1
by: TimEl | last post by:
Hi. Using Perl, I want to modify data in an XML file and print out the entire modified file, not just the elements I modify. In CPAN I have found that XPath allows me to pinpoint the elements...
23
by: no1zson | last post by:
I have been adding buttons to my GUI to manipulate list data. I added a Delete button this morning in case I decide I no longer needed a particular element. I am now working on a modify button, in...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.