![]() |
|
#1
|
|||
|
|||
c++ helpHello everyone, as you can tell i am new to this and need help with my C++ coding. If any one can help me it would be greatly appreciated. The follwoing is the question to the problem:
Write a program that prints the day number of the the year, given the date in the form month-day-year. For example if the input is 1-1-02, the day number is 1; if the input is 12-25-02, the day number is 359. The program should check for a leap year. I have done the following but i am sure there is more efficent way of doing it. Thanks CPP / C++ / C Code:
Last edited by dsmith : 12-May-2004 at 21:36.
Reason: Please use [c] & [/c] to highlight C code
|
|
#2
|
||||
|
||||
|
Woah! Before you make any more posts, check out this page! By the way, welcome to the forum, and kudos for wanting to be a more efficient programmer. Since most of the months are different, I can't think of anything that would solve your problem any better right now except just doing all the addition in your head instead of with the computer processor. Add all the numbers to a sum... like, instead of saying...
CPP / C++ / C Code:
CPP / C++ / C Code:
__________________
-Aaron |
|
#3
|
|||
|
|||
|
hi homz, welcome to c++!
first, i'd like to point out in your if statement you have two modular divisions one by 4 and another by 400. i don't think you need the one by 400. if any number is wholly divisible by 400, it will also be wholly divisible by 4. second you might want to store the sums of the number of days in an array. if you haven't learned how to use arrays yet, then i think your program is as efficient as it could be good luck in your journey towards learning this great programming language! |
|
#4
|
||||
|
||||
|
I still remembers this Q was asked before, I think the solution is a better one.
Take alook at this thread: Help with array and function input Hope it would help you find a better solution __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#5
|
|||
|
|||
|
homz, i've found a way to just eliminate the second switch statement, i'm posting the code.
CPP / C++ / C Code:
notice that i started from bottom up, so that month 2 ends up closer toward bottom and i only have to type those if else statments in 2 cases, otherwise i'da have to put them in 11 cases. |
|
#6
|
||||
|
||||
|
What about:
CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#7
|
|||
|
|||
|
yes ofcourse using an array would be the best way, but i don't think he's covered arrays yet. also i think you forgot to account for variable d in your code and thats why it's not working correctly. also the last if statement numdays++ wouldnt work for days ranging from 1/1 thru 2/28 since the extra day doesnt apply to dates before feb 29.
Also you are returning from a void function. also you haven't specified the array type. did waltp actually post this code? also i fixed the switch statement a little! CPP / C++ / C Code:
turns out that only case 2 needs that if else statment! also my mistake on previous post. when i said starting bottom up, i meant starting from 365 or 366 and working your way down to 1, not the case statements in switch. you could post the case statments starting from 1 down to 12, but it seemed more appealing to me to do them upside down. __________________
spasms!!! |
|
#8
|
||||
|
||||
|
Looks like Walt posted it, but I'm guessing he was really tired!
__________________
-Aaron |
|
#9
|
||||
|
||||
|
Or maybe he is drunk!
![]() __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#10
|
||||
|
||||
|
I dunno... drunks have a tendency to be lazy and talkative... he wrote code and said nothing! :-)
__________________
-Aaron |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The