#### Archived

This topic is now archived and is closed to further replies.

# What is wrong with this code?

This topic is 5665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts


// Determines the day of any date in 2003. Will do other years after this code is fixed.

#include <stdio.h>

main()

{

int month, day, year, mcount, dcount;

printf("Month:");
scanf("%d", &month);
printf("Day:");
scanf("%d", &day);
printf("Year:");
scanf("%d", &year);
mcount = 0;		//January

if (month == 2)								//February

mcount = 31;
if (month == 3)								//March

mcount = 31+28;
if (month == 4)								//April

mcount = 31+28+31;
if (month == 5)								//May

mcount = 31+28+31+30;
if (month == 6)								//June

mcount = 31+28+31+30+31;
if (month == 7)								//July

mcount = 31+28+31+30+31+30;
if (month == 8)								//August

mcount = 31+28+31+30+31+30+31;
if (month == 9)								//September

mcount = 31+28+31+30+31+30+31+31;
if (month == 10)							//October

mcount = 31+28+31+30+31+30+31+31+30;
if (month == 11)							//November

mcount = 31+28+31+30+31+30+31+31+30+31;
if (month == 12)							//December

mcount = 31+28+31+30+31+30+31+31+30+31+31;

dcount = mcount + day;  //dcount is the number of days from Jan. 1 of 2003

//this part of the code determine the amount of days needed to be subtracted to make dcount the same day as Jan. 1, 2003

if ((dcount % 7)  == 0 || dcount == 1)					//Wednesday - 0 == Wednesday	or Jan. 1 == Wednesday

printf("Wednesday");

if (((dcount-1) % 7) == 0 || dcount == 2)			//Thursday - 1 == Wednesday		or Jan. 1 == Thursday

printf("Thursday");

if (((dcount-2) % 7) == 0 || dcount == 3)			//Friday - 2 == Wednesday		or Jan. 1 == Friday

printf("Friday");

if (((dcount-3) % 7) == 0 || dcount == 4)			//Saturday - 3 == Wednesday		or Jan. 1 == Saturday

printf("Saturday");

if (((dcount-4) % 7) == 0 || dcount == 5)			//Sunday - 4 == Wednesday		or Jan. 1 == Sunday

printf("Sunday");

if (((dcount-5) % 7) == 0 || dcount == 6)			//Monday - 5 == Wednesday		or Jan. 1 == Monday

printf("Monday");

if (((dcount-6) % 7) == 0 || dcount == 7)			//Tuesday - 6 == Wednesday		or Jan. 1 == Tuesday

printf("Tuesday");

}

[edited by - dampe64 on February 9, 2003 9:28:37 PM] [edited by - dampe64 on February 9, 2003 9:35:42 PM] [edited by - dampe64 on February 9, 2003 9:42:55 PM]

##### Share on other sites
looking at that code annoys me... you should comment it because i dont want to even try and figure out what your trying to do.

it sucks?

##### Share on other sites
quote:
Original post by dampe64
main(int month, int day, int year, int mcount, int dcount)
{

I refuse to try and figure out what''s going on in your code, but this is kind of obvious. Change those two lines into:

int main () {
int month, day, year, mcount, dcount;

##### Share on other sites

  main(int month, int day, int year, int mcount, int dcount)

Make that

  int main(){    int month, day, year, mcount, dcount;

  printf("Month:");        scanf("%d", &month);       printf("Day:");        scanf("%d", &day);       printf("Year:");        scanf("%d", &year);       mcount = 0;

You should do some bounds checking to ensure that your values for month, day, and year and within proper ranges. Otherwise later parts of your program may give you faulty results.

        if (month == 2)             mcount = 31;      if (month == 3)             mcount = 31+28;      if (month == 4)             mcount = 31+28+31;              if (month == 5)             mcount = 31+28+31+30;      if (month == 6)             mcount = 31+28+31+30+31;      if (month == 7)             mcount = 31+28+31+30+31+30;      if (month == 8)             mcount = 31+28+31+30+31+30+31;      if (month == 9)             mcount = 31+28+31+30+31+30+31+31;      if (month == 10)             mcount = 31+28+31+30+31+30+31+31+30;      if (month == 11)             mcount = 31+28+31+30+31+30+31+31+30+31;      if (month == 12)             mcount = 31+28+31+30+31+30+31+31+30+31+31;

Since all of the above cases are mutually exclusive, use either a sequence of if-else if''s or a switch statement. A case is best because the cases are ordered and numeric. Also use enum''s to better identify the cases. Something such as

enum {JANUARY = 1, FEBRUARY, ..., DECEMBER};

would suffice.

          if ((dcount/7) - ((int)(dcount/7)) == 0 || dcount == 1)               printf("Wednesday");        if (((dcount-1)/7) - ((int)((dcount-1)/7)) == 0 || dcount == 2)               printf("Thursday");        if (((dcount-2)/7) - ((int)((dcount-2)/7)) == 0 || dcount == 3)               printf("Friday");        if (((dcount-3)/7) - ((int)((dcount-3)/7)) == 0 || dcount == 4)               printf("Saturday");        if (((dcount-4)/7) - ((int)((dcount-4)/7)) == 0 || dcount == 5)               printf("Sunday");        if (((dcount-5)/7) - ((int)((dcount-5)/7)) == 0 || dcount == 6)               printf("Monday");        if (((dcount-6)/7) - ((int)((dcount-6)/7)) == 0 || dcount == 7)               printf("Tuesday");

Again, identify the constants 1-7 with enum''s that make the code numbers to interpret as days of the week.

(dcount/7) - ((int)(dcount/7)) == 0

The above line is equivalent to

dcount % 7 == 0

which is easier to read an understand. In subsequent cases you''re doing extra work though. If you already know the remainder of dcount when divided by 7 (dcount % 7), then you don''t need to shift dcount and recompute the value. Essentially

((dcount-1)/7) - ((int)((dcount-1)/7)) == 0

is equivalent to

(dcount/7) - ((int)(dcount/7)) == 1

which is of course equivalent to

dcount % 7 == 1

Also, the extra case you handle in the if statement can be rolled into the above line by simply adding one. This is because the remainder if an integer divided by n is in the range [0, n-1], and if you add 1 to it you get a number in the range [1, n]. Thus by adding one your checks for dcount to be 1, 2, ..., or 7 become redundant. Essentially you write a switch statement such as

  switch ((dcount % 7)+1) {case SUNDAY:      // ...case MONDAY:      // ......case SATURDAY:      // ...default:      // ...}

Also you''ve made one assumption in your code thus far, that the first day for each year is a Sunday. You''ll obviously have to compute this some other way, but the effect will be to just subtract (or add) a value to dcount and continue on. I hope this helps and good luck. One last thing you could do is double check the numbers in all of your constants if you are getting incorrect results.

quote:

I refuse to try and figure out what''s going on in your code, but this is kind of obvious.

quote:

it sucks?

quote:

looking at that code annoys me... you should comment it because i dont want to even try and figure out what your trying to do.

It took my less than 30 seconds to figure out what he was trying to do (find the day of the week for a given date). You guys shouldn''t be so rude. This is a forum for people to learn, not to be berated.

thanks

##### Share on other sites
quote:
It took my less than 30 seconds to figure out what he was trying to do (find the day of the week for a given date).
You guys shouldn't be so rude. This is a forum for people to learn, not to be berated.

I knew exactly what he was trying to do. And iteration is a crucial lesson. Not using iteration when it is appropriate to is a sign that your program sucks.

For example:

  int main(){	int day,month;	cin>>month>>day;	int days[]={31,28,31,30,31,30,31,31,30,31,30,31};	for(int i=0;i<month - 1;i++)		day += days[i];	string weekdays[]={"Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday","Monday"};	cout<< weekdays[day%7] << endl;}

[edited by - sjelkjd on February 9, 2003 10:54:41 PM]

##### Share on other sites
quote:

I knew exactly what he was trying to do. And iteration is a crucial lesson. Not using iteration when it is appropriate to is a sign that your program sucks.

So instead of describing a better solution the proper thing to do is to criticize? Saying that his program sucks is far from constructive criticism. By inspecting the code posted you can easily see he''s a beginner. Providing a solution based on different constructs than those displayed obviously won''t help him understand what he was doing wrong. You have to understand basic flow control constructs prior to moving onto iteration and arrays. If someone is struggling with using the former constructs you don''t show them a solution with the latter constructs. But regardless, why the nasty tone towards those who are asking for help? This forum is for asking for help and advice, not for those more learned to stomp on the laymen. If you have a better solution, post it an explain it, but don''t be condescending.

##### Share on other sites
quote:
By inspecting the code posted you can easily see he''s a beginner.

There''s a forum for beginners, and it''s not called "general programming."

My response may seem like condescension, but to me the original post is condescension. Dumping a large volume of code and saying "what''s wrong" isn''t a very good attitude to have when asking for help. At the very least the poster can state the symptom of his problem(won''t compile, crashes, doesn''t work for n>2, etc).

The moral? Don''t waste my time and I won''t waste yours.

EDIT: stupid 500 errors

##### Share on other sites
quote:

My response may seem like condescension, but to me the original post is condescension. Dumping a large volume of code and saying "what''s wrong" isn''t a very good attitude to have when asking for help. At the very least the poster can state the symptom of his problem(won''t compile, crashes, doesn''t work for n>2, etc).

If someone walks up to you on the street and asks you for directions, do you say, "Screw off, do I look like a map?" People don''t always ask for help in the proper or most convenient places. Also, people don''t always provide a good description of the problem. Those who have little experience working with others often have difficulty troubleshooting as a group. Concise, accurate, and complete descriptions of problems aren''t always to be had. The skill to provide such descriptions is what falls into communication skills. Not everyone has this when they immediately enter the software industry. Again, make an attempt to educate someone before you throw the book at them.

1. 1
2. 2
frob
13
3. 3
4. 4
5. 5

• 9
• 13
• 14
• 67
• 14
• ### Forum Statistics

• Total Topics
632132
• Total Posts
3004299

×