![]() |
|
#1
|
|||
|
|||
Anyone want to critique this?This C++ stuff is fun! Who knew?
I've taken an assignment from "Problem Solving, Abstraction, and Design" (and slightly modified it) and was wondering if any of you geniuses had any comments on its design, style, anything. WaltP, thanks for the use of your leap year function - it was better than mine. The original assignment asked for a "function to calculate the day number for a given day represented by three type int values", which I thought strange - if the three values are month, day, and year, are they asking for the cumulative number of days from year 0?? And it also asked for a "recursive function to determine the number of days between two dates, each represented by three integers." -since the first part of the assignment had asked for a function to compute the day number, that seemed like a waste, so I wrote a function that compared the dates using two values, the day number and year. Didn't do it recursively however - so I'd probably get an F. Here 'tis: CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: Anyone want to critique this?hi chef,
i am also a novice in CPP and i find your style & indenting very good. i have a small doubt. in your diff(...) , when (year1==year2), can't we simply return the value of abs(number1-number2)? and the rest prog is no doubt very good, at least for me. __________________
balu------>>>>>U perseverance pays slowly. |
|
#3
|
|||
|
|||
Re: Anyone want to critique this?Quote:
A couple of points of order... In: Please enter two dates in numeric form (i.e. 1/1/2001). Please enter first date: ...I'd probably put a newline character after the period and before the second Please. Also, I'd use a different convention than 1/1/2001, since it is unclear which "1" is month and which "1" is day of the month. Code:
...your error messages are typical "meaningless" error messages in that they don't tell the user anything meaningful in order to correct their input. Something like: DD/MM/YYYY might be worth considering...then followed with an "example" of 1/1/2001 it will be clear how you expect input. You can also use something like: 12/31/2001, because we all know that there are not 31 months in a year. Also, your program doesn't work "completely" properly: CPP / C++ / C Code:
Here is the calendar for the year 1752: Code:
Another issue is that it is not clear what "between" means. Your output is using "between" to mean the beginning of the day following the first date to the end of the day of the second date. If I said "How many (whole) numbers are between 1 and 3," any 5-year old would be able to tell me that there is only a single number "2" between them. However, in your code, if I say how many days are between 1/1/1 and 1/2/1 it tells me one day. How many whole numbers are between 1 and 2? None, they are adjacent. Basically, you have a start of day/end of day issue that should be clarified in your requirements elucidation phase in order to code a proper response to first = 1/1/1 and second = 1/2/1. A natural extension of any such "real world" program would be how many hours are between a certain date/time stamp and another date/time stamp. For example, if someone where to ask you "How many days are in the year, 2006?" You would easily say: 365. But your program says 364 when using first = 1/1/2006 and second = 12/31/2006. If someone asked you how many days are in a leap year, you would easily be able to say 366. Your program gives us 365. Do you see how important it is to clearly define what "between" means? Why isn't "between" the period beginning exactly at the start of the first date entered and ending at the end of the second date entered? In that case, if one entered first = 1/1/1 and second = 1/2/1 the answer would be 2 days and definitely not zero. If we think about what a "day" is, it is an interval of time, not so much a number on a calendar. Anyone older than a 5 year old will know that the day begins when the previous day ends. It is the whole "is the time 12:00AM or 00:00" issue. In 24-hour clocks, we have 23:59.59.9999 as the last "reasonable" moment before the new day begins...even if a particular clock isn't that accurate. When someone asks how many days are between Sunday and Saturday, what do we say? We look at the calendar and we see Monday, Tuesday, Wednesday, Thursday and Friday...and we say 5 days are between Sunday and Saturday. Of course, your program says 6. This is one of those situations where "human" logic and "computer/math" logic don't really add up very well...and certainly one where the "division" of where "between" starts and ends must be clearly understood. One could easily imagine a little web-based utility to tell you how many days your vacation would be by entering the date that your vacation started and the day that it ended. If you were to fly out on a Friday evening after work, get to the resort that night and wake up "in vacation mode" the next morning...(assuming that you didn't party too seriously Friday night!)...and relaxed, ate out and had fun until you had to fly back the next Sunday so that you could be to work early Monday morning, how long was your vacation and how many days did you have to "take off" from work? Use your program to figure it out as if it were a real utility and see if the output "makes sense." If it doesn't add up to what you would think a typical user would expect, then maybe you need more requirements clarification. :davis: |
|
#4
|
|||
|
|||
Re: Anyone want to critique this?baluss, thanks, you're absolutely right.
davis, many thanks for the points you elucidated. In regards to Quote:
Quote:
This book has a number of assignments that have the "huh?" factor..... So what do you think is the best book to learn from? |
|
#5
|
|||
|
|||
Re: Anyone want to critique this?Quote:
Let's take a look at what you're doing. Basically, you're learning C right now. You're learning how to perform the fundamental "mechanical" operations necessary in any C or C++ program. You really haven't used C++ in any particular way exclusive of iostreams, which, BTW...are reasonably complex and involved aspects of the language all by themselves. There is nothing wrong with learning these elements of the language, but, to be clear, there is nothing at all "C++"-like about it. In order to even begin to be C++-ish, one would need to create a Date class, implement a "-" operator (along with +, =, ==, !=, >>, <<, >, <, ++ (pre and post) -- (pre and post) etc.). This is the fundamental difference between C and C++. In other words, instead of doing some kind of "mechanical wizardry" inside of a "function" to calculate the "difference" between one date and another, you would simply implement the "-" operator. For example, a date class would be able to return the month, day and year of any date and probably operations like IsLeapYear() would return a bool. Of course, any reasonable implmentation of a "Date" class would really be a "DateTime" class, since the calculations can not be accurate without knowing when (by year, month, day, hour, minute, second, millisecond, microsecond) the particular "date" is. CPP / C++ / C Code:
...this is something along the lines of a (very) rudimentary start on an implementation of a "datetime" class(es). Once completed, it would be C++. The idea behind C++ is that there are elements of reusable code. If we stop for a moment to look at your code, there isn't really any aspect of it that is reusable. You may be able to cut-n-paste portions of it into other programs, but basically, it is a wind-up doll....turn the knob, release and it does its thing and finally dies. However, with a "proper" date/time class (perhaps as might be implemented in a library), anyone who needed date/time functionality could easily reuse the code for it. That is one of the many reasons why we have C++. Now, there is no reason at all why a good date/time infrastructure can't be implemented in C, in-fact, there already is... Furthermore, when we start to look at dates and times, we encounter a REALLY LOT of issues...such as whether someone is "right now" on the other side of the International Dateline, or not. If they are, it is "tomorrow" for them. How screwy is that when you're trying to write code?! Additionally, one needs a facility to convert between timezones and daylight savings time for those regions that support/use it. We really need to "think" in a different manner when it comes to C++. If not, we are not really doing anything close to C++...rather, it is just C with some C++ capability mixed in. As for good books, I recommend Jesse Liberty's books for learning C++. His content will get you well down the road to learning the needed C and eventually C++ aspects. Once you get the basic ideas of C++ understood, then you can try to start thinking more specifically OOP-mindedly. :davis: |
|
#6
|
||||
|
||||
Re: Anyone want to critique this?I'll just add two very small suggestions.
Firstly, since you're already using C++ headers, cout, std namespace... there is a Standard C++ swap() function. You could simplify your code a littel by using that. Secondly (and this is a personal preference really), I would typically use the preincrement operator ++result; instead of postincrement whenever I have a stand alone increment statement. With primitives it really doesn't matter, but I feel it's a good habit to get into, since it's more efficient when working with more complex objects. Matthew P.S. I fogot to say that the std::swap() function is defined in the header <algorithm>. |
|
#7
|
|||
|
|||
Re: Anyone want to critique this?QED, thanks, I figured there was a standard swap function somewhere, haven't gotten to it yet in my book. I didn't know that the pre-increment operator was preferable to post-increment, where possible - my book didn't state there was a preference.
davis, thanks for your comments. I should have started my post with "who knew this programming stuff was so much fun?" instead of specifically mentioning C++. I guess it's frustrating for you to have newbies like me posting our pitiful code. Just realize I'm posting out of enthusiasm, for feeling like I'm getting at least some aspects of the process. OK? |
|
#8
|
|||
|
|||
Re: Anyone want to critique this?Quote:
Of all the newbies I've seen here, your posts are probably the least frustrating of all. I'm glad that you're getting some of the fundamentals down. There is nothing wrong with your elation or your enthusiasm. I sometimes come across as being "too heavy" (to reverse-quote our recently gone-missing WaltP), but all I wanted to do was to explain to you the differences so that you understand that there is another "whole world" of "real" C++ just beyond the "C steps" you're now taking. It is far more exciting and interesting than just the C aspects you're now learning. It is a long journey, to be sure. Code:
This is the "problem" that I tried to explain previously about the leap year routine. How can it accept a date of 0? In "C" we often return some kind of "error code," in C++ we are obligated through a convention called "contract programming" to return the value type or throw an error. You will eventually get to the location in your book where exceptions are discussed. However, briefly, they are a mechanism that allows for propagation of an error condition to be delegated to those with an interest in managing particular errors. If no code directly manages the error, the error is propagated to a "top-level" error manager that deals with it...which usually means that the program is aborted. A rather abrupt end of things, but it definitely would have prevented the results returned above that are "impossible" based on the data entered. Since year 0 can't exist, it can not also be a leap year. I don't say any of this to discourage you, in fact, I say it to encourage you to adopt error management into your code at your earliest convenience. We call this "defensive programming." Program as if the data entered is going to be bad first and good last...or maybe never The fact that you actively seek critics of your code is excellent! You have demonstrated a level of openness about your inexperience that few others would share. You are a far better programmer because of that simple fact. Perhaps a first step in learning is coming to an understanding that you don't already know everything about something. No matter how good you get at programming, never lose sight of that openness and an understanding that there is far more out there to learn that to ever even be able to become a "know-it-all." Anyone who has ever seen a movie has been a movie critic. It is easy to be a critic. How hard is it to make a movie? ...even a bad one? Probably a lot harder than writing code...even code that "makes movies." You _are_ getting some aspects of the process...and good for you! It isn't easy, is it? I've been programming for many years and I still make programming mistakes at the rate of dozens (if not a lot more) per day. You seem like you're in a perfect position to really learn well through your efforts. Don't let something that I say distract you from the opportunity you have to learn this stuff. Rather, just pile it on or send it to /dev/null (the waste basket) as you see fit. Sometimes something said won't make a lot of sense in the "now" sense of it. Perhaps later it will. And, just so that there is something else for motivation, the elation one gets from "this programming stuff" is nothing compared to the "oh wow" that happens when the "lights come on" in understanding object-oriented programming. I don't say this to diminish your current level of enthusiasm, rather to let you know that there is something up ahead even cooler just waiting for you to discover it. :davis: |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Please critique my new web site | dhester | Websites Reviewed Forum | 3 | 15-Apr-2003 06:15 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The