This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.

Anand Hariharan

Rancher
+ Follow
since Aug 22, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Anand Hariharan

To a beginner to programming, would you really teach exception handling and KeyError in dictionary access, before you teach if statement?
4 years ago

Tim Moores wrote:Not if used in the main method. Since pointers are specifically required, this tongue-in-cheek approach would (hopefully obviously) not be the best approach.



Correct.  Besides introducing problems associated with integer overflows, it is harder to write (unsurprising you didn't get it correct the first time), significantly harder to understand and the benefits of not using a temporary third variable is far outweighed by performing all that arithmetic.
4 years ago

Logan Cherry wrote:(Hint: You may NOT USE any 'if' statements.)



I find that rather silly as a requirement and bizarre as a "Hint".
4 years ago
You have received some good advice.  Also, it has been a good two months since you posted your original question.   I just have one piece of criticism:



The format string in scanf is troublesome.  Look up buffer overflow on the internet to understand why.

It should read

scanf(%19s",getUserMark.name);

However, that is not the end of it.  Your input could still be a name that is longer than 19 characters, and it will be waiting to be read after the scanf call.

The bottom line is that I/O (especially 'I' in I/O) in C is quite hard and not easy to get it correct.
4 years ago
I realize I am responding two weeks after your original post.

First of all, kudos on the concept!  Is this original?  For sure, I have not come across an earlier attempt to come up with a picture book for a programming language.  Brilliant concept.

I have some comments on the code:

  • Do NOT define true or false!  Absolute No-No.
  • Prefer C to preprocessor.  open and close could be variables of type const int (in C they are not strictly equivalent to their #define counterparts, but in this case, those differences do not matter).
  • Avoid global variables.  These could just as well be moved within main()
  • String literal input to printf should have a newline


  • best,
    4 years ago
    Your code has ALL the following characteristics:

  • Templates!
  • Nested classes
  • The inner class using the template parameter of the outer class
  • Implementing the member functions of the template classes outside the class definition


  • I am no language lawyer, but I think it is reasonable to assume that ANY of the above could make the head of a C++ language lawyer hurt!  

    I hope this helps:  https://msdn.microsoft.com/en-us/library/71dw8xzh.aspx

    sincerely,
    - Anand
    6 years ago
    Please provide the following details:

  • Definition of myGraph, including declaration of myGraphNode
  • Instantiation of the template class
  • The line that calls to output your intantiated object
  • 6 years ago

    Adam Szewczyk wrote:Greetings,
    I am trying to translate following java code into c++ code.
    (...)
    I do not understand what Configuration.DEBUG is doing. What is the c++ equivalent for it? Will omitting it affect " if then " performance significantly?



    While your specific question regarding 'what Configuration.DEBUG is doing' was addressed and answered, just a note that unlike Java that requires an IEEE 754 based floating point representation, C++ does not require so. Also, C++ does not have functions such as double-to-long functions and one typically would have to use a cast. The behavior of the cast is also left undefined by the language (since the size of a double and the size of a long could be different).

    For most PC based compilers, such matters are not an issue though and your 'translation' from Java to C++ would appear to work just fine.
    9 years ago
    I see that you have figured out the problem. Just wanted to point out something:

    mark patindol wrote:



    You are relying on a compiler extension (i.e., it is not likely to work if you use another compiler or even the same compiler with different settings).

    The ISO standard for the C++ language (which is clearly what you are using, as opposed to the C language) does not allow you to declare an array whose size is specified only at run-time. If you were converting the code from Java, the Java code would have done a new[]. C++ would allow that as well. Only, in C++ you would have to ensure you do a delete[], whereas in Java, you would not have to (in fact, you would be unable to).

    There are also style issues in your code that would go against the grain of most folks who've written C++ for a while, but I will not bring them up now.

    - Anand
    9 years ago
    Thanks Paul. That explains quite a bit. I am responding this late because I am trying to find information on the +1, Thumbs Up, cows, and other similar incentives. Guess this is not a FAQ (since I was unable to find anything there), so is there a web-page that explains all these incentives?

    As one can see, I've been in the ranch for a while, but I don't post very often. One of the mods presented me with the Rancher title well before these incentives were set up (~August 2010).

    respectfully,
    - Anand Hariharan


    UPDATE:

    I found this: https://coderanch.com/how-to/java/RanchCows
    9 years ago
    Many thanks to the folks at JavaRanch for doing the book promotion and picking me as one of the winners this time.

    sincerely,
    - Anand
    9 years ago

    Al Sweigart wrote:Yes. In my opinion, Python is the most readable language I've encountered. Not to start a language war, but I would say it is generally more readable than JavaScript, Java, and Perl.


    In my opinion, Visual Basic is the most readable language. True, it has a "vendor lock-in" (and has its fair share of quirks, but then which language doesn't?) but there are no confusing types such as lists vs tuples vs dictionaries that are different only by virtue of bracket vs paren vs squiggly for their syntax.
    9 years ago

    Al Sweigart wrote:My preference for C++ ternary operators is probably just an old habit. But I do like how it reads "this-condition, this-value-if-true, this-value-if-false" instead of Python's "this-value, if-this-condition-is-true, or-else-this-value-if-the-previous-condition-was-false"



    Would you apply the same reasoning on list comprehensions? They sure read unusually when compared to the explicit for-loop counterparts.
    9 years ago

    Al Sweigart wrote:Anand's comments cover a lot of it.


    Thank you Al. Appreciate it.

    Al Sweigart wrote:Though I wish the ternary operator was more like Java or C++.


    Could you elaborate, please? I suppose opinions vary, but in this case, I know very, very little of Python, and you are ... well you! So opinion matters.

    In C++, one could write

    - whereas in Python one would write


    To me, the latter reads much better. I suppose there are operator precedence issues that come in play but I do not know the language well enough (read 'at all'!).

    thank you,
    - Anand
    9 years ago

    Campbell Ritchie wrote:That I think is C++ code because of the bool keyword, not C.


    C introduced bool albeit after C++. It is a 'typedef' and one needs to include <stdbool.h> for it to be usable.

    Campbell Ritchie wrote:I presume the == and != operators are overloaded on Strings.


    Actually, C does not have a native string type. In C, one uses an array of characters terminated by the NUL character to hold strings. In C++, one has the string class that comes with member functions (substr is one of them) and overloaded operators.

    Campbell Ritchie wrote:I suspect the greatest overhead is in the format time function. What complexity does that run in?


    The code posted by the OP does not need any while loops. Also, the OP could use a function that does the reverse of 'format_time' (i.e., one that takes the formatted string representation of the time and returns the integral equivalent.
    9 years ago