+ 2

Why is this program a mistake? [6]

Can anyone point out why my code can't satisfy the last test case in "Days between Dates" challenge? https://sololearn.com/compiler-playground/cH09bjWgPqg9/?ref=app

1st Nov 2024, 6:24 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
30 Respostas
+ 3
Ah i finally see where it wrong. In my code, i too focused using year for calculating leap day. Btw, i just ask chatgpt why my code go wrong (including guessing the input) and it gives some corrected output (which i wonder why it pass all test cases while my own fails at the last test case (the input most likely like this: August 15, 1979 and January 30, 2000) But honestly, the corrected code is more like overhaul than small editing. Chatgpt code: https://sololearn.com/compiler-playground/cWIt09z7nG4t/?ref=app
2nd Nov 2024, 3:33 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 3
Celvien Kurniawan It's also correct, except for the century years that are not divisible by 400. 1896 and 1904 are leap years. 1600 and 2000 are leap years, but 1800 and 1900 are not leap years.
1st Nov 2024, 8:42 AM
Jan
Jan - avatar
+ 3
Celvien Kurniawan There is no need to restart it from scratch. I'm quite sure you can fix it from line 48 with - if, else if, and else conditions.
1st Nov 2024, 3:10 PM
Jan
Jan - avatar
+ 3
Jan normal variables is simpler to write and for simple datatype like ints, all these pointer manipulations are actually extra work without significant optimization gain. Plus it is practically unreadable. Premature optimization is not a good habit overall. But Celvien Kurniawan's idea is a good one from an exploratory standpoint. It helps you understand arrays and pointers and ways of working with them. As an exercise, yes, it is worth keeping the code the way it is now. It's a good debugging challenge, too.
1st Nov 2024, 4:22 PM
Bob_Li
Bob_Li - avatar
+ 2
Looks like, i overlooked it. Honestly, i was taught that leap year is divisible by 4 only
1st Nov 2024, 8:12 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 2
Celvien Kurniawan if you want to simplify your month name to int conversion, here is a function I got from somewhere a long time ago while browsing, not sure of the source, but it's short and sweet. I keep it in my codebits. The numbers come from the first three month name characters. These numbers are specifically for capitalized month names(example : 'April'), you would have to generate a different set depending on how the month names are written (example: APRIL, april) mName[0]+mName[1]+mName[2] #include <stdio.h> int monthNameToInt(const char* mName){ const int idn[12] = {281,269,288,291,295,301,299,285,296,294,307,268}; int n = mName[0]+mName[1]+mName[2]; for(int i=0; i<12; i++) if(idn[i]==n) return i+1; return -1; } int main() { printf("October is %d\n", monthNameToInt("October")); }
1st Nov 2024, 2:26 PM
Bob_Li
Bob_Li - avatar
+ 2
Jan modern compilers are actually getting better and better. Manually micromanaging stuff in the code is generally unnecessary unless you have a really good reason and the compiler is not giving you a good enough result. But yes, manual optimizations might end up to be more work for 0 or negative gains in the end.
2nd Nov 2024, 12:01 AM
Bob_Li
Bob_Li - avatar
+ 2
...and the main language is already C# if you want to develop Windows applications.
2nd Nov 2024, 12:52 AM
Jan
Jan - avatar
+ 2
Also thank you so much Bob_Li and Jan for giving me insights and advice in debugging this code.
2nd Nov 2024, 3:34 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 2
Celvien Kurniawan programming is going to be very different from here on... yes, it's going to be interesting indeed. How to test and debug codes will be more important than how to write them. But I guess people are going to come up with an AI for that, too. AI development is essentially coding humans out of the code generation in the loop. The people providing the AI hardware and service will be the ones that are left standing because they will be providing essential service. Manually writing codes might end up as just a hobby, like building matchstick houses...
2nd Nov 2024, 11:01 PM
Bob_Li
Bob_Li - avatar
+ 2
It's now obvious that we won't be able to compete with ChatGPT in the future when it comes to generate code. The best "programmers" will be those who understand how to communicate with ChatGPT.
3rd Nov 2024, 12:13 AM
Jan
Jan - avatar
+ 2
Jan code could also be created, debugged and run within the AI framework. We could be facing a future where even the codes are inside tha AI blackbox. Then it could just scan or listen to the question and spit out the answer. And you don't have to be very proficient in communicating with it because it will probably evolve to become very good at deducing your intent. Linked networks are learning everything from everywhere all the time. image, text, audio, and video generation are already done this way, after all.
3rd Nov 2024, 1:26 AM
Bob_Li
Bob_Li - avatar
+ 1
have you considered leap years?
1st Nov 2024, 6:38 AM
Bob_Li
Bob_Li - avatar
+ 1
January 1, 1900 December 31, 1900 Output: 365 January 1, 1901 December 31, 1901 Output: 364 1900, 1800, 1700 are not leap years. Only century years divisible by 400 are leap years.
1st Nov 2024, 7:30 AM
Jan
Jan - avatar
+ 1
Celvien Kurniawan Actually, yes...
1st Nov 2024, 7:49 AM
Jan
Jan - avatar
+ 1
Jan i see... Thanks for the insight. I'll try to fix it soon.
1st Nov 2024, 8:57 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 1
Celvien Kurniawan your use of pointers and arrays makes your code very hard to debug. It might be very clever, but it is also unnecessary complexity. I would suggest normal variables instead of arrays as container. It does not need this level of optimization.
1st Nov 2024, 2:09 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li yeah, i just restart it from scratch again to fix it.
1st Nov 2024, 2:20 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 1
Bob_Li I agree with you about the pointers. Basically, there is no reason to use C at all for that kind of tasks, unless you are writing a new OS or a system driver.
1st Nov 2024, 8:10 PM
Jan
Jan - avatar
+ 1
Bob_Li I once made some tests in both C and C++ with pointers, and they were actually useless. Sometimes the pointers even decreased the performance, but maybe pointers still make sense in game development in C++.
1st Nov 2024, 11:48 PM
Jan
Jan - avatar