• Advertisement

Archived

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

What is wrong with this code?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster

  
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
quote:
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?"


Not a valid analogy. This is a technical forum, not a social setting.

quote:

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.


The poster has been a gamedev.net member for about a year. Is it really too much to ask that they post intelligent questions?

Share this post


Link to post
Share on other sites
Asking ''what is wrong with this code?'' means dampe64 knows there is something wrong with it. It wouldn''t take much effort to explain what the problem is and why she knows it isn''t working correctly. By explaining the problem more clearly she would also find herself thinking it through and possibly understanding it better.

How to ask

Share this post


Link to post
Share on other sites
quote:
Original post by sjelkjd
[quote]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?"


Not a valid analogy. This is a technical forum, not a social setting.

quote:

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.


The poster has been a gamedev.net member for about a year. Is it really too much to ask that they post intelligent questions?

A forum, I''m afraid to announce to you, is just as much a social setting as the streets are. You interact with people and reply to queries concerning their problems in an environment with other people.

If you can''t provide constructive replies to someone''s questions, then don''t waste their time, thanks. This is a forum to help people, regardless of their skills, not a place to yell at them because you''re unable to make sense of their code, even if it''s really simple. Granted, this was not posted in the right place, but a question is still a question.

Share this post


Link to post
Share on other sites
Is there a reason why the value has to be calculated manually? Would not mktime() be a better solution?

Just fill in a struct tm variable with the specified date and let mktime() fill in the tm_wday field. Done.

Here''s a link to the function description:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_mktime.asp

Share this post


Link to post
Share on other sites
quote:
Original post by RuneLancer
A forum, I''m afraid to announce to you, is just as much a social setting as the streets are. You interact with people and reply to queries concerning their problems in an environment with other people.


With different responsibilities. Posting in this forum implies you have some sort of prorgamming knowledge, and a responsibility not to waste the time of others.

quote:

If you can''t provide constructive replies to someone''s questions, then don''t waste their time, thanks.


Forgive my attepts to raise the S/N ratio. By replying "it sucks" I was hoping the OP could elaborate on their program, and learn some valuable comminucation skills while doing so.

quote:

This is a forum to help people, regardless of their skills, not a place to yell at them because you''re unable to make sense of their code, even if it''s really simple. Granted, this was not posted in the right place, but a question is still a question.

Ok, for the last time, I didn''t have a problem understanding their code. What I did have a problem with was being QA for them. I don''t think it''s too much to ask that if someone has a problem with their code, they can at least explain the symptoms, instead of just posting the code(especially when the code is tedious).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by sjelkjd
Not a valid analogy. This is a technical forum, not a social setting.



nobody forced you to reply to this thread.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
nobody forced you to reply to this thread.

why don''t you label your posts with a name? are you afraid of something?

this "don''t reply" attitude, taken heartily by many easily offended gamedev members, has the unfortunate side-effect of forcing the more knowledgeable population to leave or become less active. how, you ask?

the two components of a forum are people asking and people answering questions. most people come to a forum with a question, and start by playing the first role. many stay around hoping to help someone in the future with their question, if they can, just like someone else helped them earlier. without this second component, the forum isn''t much useful.

now, consider people answering questions. obviously, some know more and others know less. an easily observed tendency is that people who know more tend to have higher questions answered to questions asked ratio, and moreover, that they are generally more often correct. such people have an interest in helping other people learn something, troubleshoot their problems, and so on. instead of posting wild guesses, they would research information for the benefit of a single poster.

said knowledgeable people often see misleading and incorrect replies posted by people without much clue. often posters would pull authoritative claims out of thin air, knowing next to nothing about subject at hand. should wrong responses be corrected? as someone who is interested in helping others become better programmers, i say yes. others, whose worst nightmare is have their error pointed out to them, say no. they prefer to live in ignorance, and that''s fine by me; but when they take their ignorance out to other people, they become a pain. i spend time here to help people be better programmers, and these people do the opposite by posting incorrect information.

similarly, there are people who ask questions in the form close to "i copied this program from a web page/book cd/elsewhere, (optionally changed something,) and it doesn''t work. somebody fix it for me." while it''s obvious that providing such fix benefits the poster in the short run, in the long run nothing is achieved. thus the poster is told how to find a solution himself, with the hope that he will be able to solve similar problems in future himself, without asking others for help.

back to "don''t reply." should i give up on threads which contain false or misleading posts because someone might be offended by me correcting them? should i ignore beginners and let other beginners, who were more successful in pasting the code, paste their code into posts here and help the original poster with his pasting? would you like to receive incorrect facts as replies to your posts, that are not corrected by members who know for sure the falsehood of those facts, but are afraid of hurting someone''s ego?

receiving knowledgeable answers is a privilege, not a right. people who respond do so because they have some interest in that. killing this interest will leave you with a board of lower S:N ratio on the answer side, which means that when you have a question, you''ll get lower quality responses or no responses at all. do you really desire it?

Share this post


Link to post
Share on other sites
And has any of this ego bashing and testosterone spraying helped the original poster in any way? I think not.

Share this post


Link to post
Share on other sites
sjelkjd, YOU wasted everybody's time by posting "it sucks". All anybody is going to get from that response is the impression you are a jerk.

If you didn't like what he was doing, how about
1. Don't Respond
2. If you HAVE to respond, say something like (I'll practically quote you), "Please post in the "For Beginners" Forum - it is meant for you. And for the record, 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, state the symptoms of your problem."

Done and done (sorry, I'm bored at work :\).

[edited by - neonfaktory on February 10, 2003 6:06:30 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by neonfaktory
sjelkjd, YOU wasted everybody''s time by posting "it sucks".


Let''s think about that. "it sucks" takes about 5 seconds to parse, analyze, and understand. It conveys a large amount of information in a small amount of words. If you really want to argue that the time it took you to read "it sucks" is such a huge waste of your time, go ahead. The real time waster is large code dumps and pointless moralizing.

quote:
All anybody is going to get from that response is the impression you are a jerk.


I honestly don''t have a problem with that. If that''s the way you feel because of that post, great. I''d rather interact with people who aren''t hypersensitive and show a sense of levelheadedness and humor about these sorts of things.

Share this post


Link to post
Share on other sites

  • Advertisement