1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Accelerated C++ book - no solutions for exercises, should I still solve them?

  1. Aug 8, 2009 #1

    KCL

    User Avatar

    As the title says, there are no answers for the book I'm learning C++ from, should I ignore the exercise section (well except the first exercise which is always compile and execute chapter examples) and move on or give them a try? I can get my hands on many exercises with solutions, but I'd like to know which way is best. Thanks. :)
     
  2. jcsd
  3. Aug 8, 2009 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    I suggest you do the exercises. Theory is not everything in programming: actually writing code will also learn you about common mistakes and learn to avoid them, teach you how to interpret and solve compiler warnings and many other advantages.

    If you are not sure about some code, you can post it on the internet (there are many well-known programming forums) and ask the experts, but I suggest not skipping them.
     
  4. Aug 8, 2009 #3
    You want to learn programming, you better get used to doing it wrong at least twice. Thats how you learn how to program. In real life there are no answers in the back of the book. Give em all a try. Ask me for help if you need it. I program all day long.
     
  5. Sep 17, 2009 #4
    @CFDFEAGURU:
    Thanks. This is regarding C++ functions with parameters that are References.
    Could you please let me know in the following code why I can call the function myFunc(), in the same way, even if I define the parameter differently: (int i2) and (int& i2)

    eg:

    #include <iostream>

    int myFunc(int i2){
    std::cout << i2;
    return 0;
    }

    int main(){
    int i = 1;
    myFunc(i); //Valid even if myFunc has (int& i2) above
    return 0;
    }
     
  6. Sep 17, 2009 #5
    The myFunc(int i2) is a pass by value method. The myFunc(int &i2) is passing the argument by reference using the address (&) of i2.

    Also, in order to not have to use a lot of standard functions, in this manner std:: put this line of code above your main() function.

    using namespace std;

    That will allow you to simply write

    cout << " .... " << endl;

    You won't have to use the std:: at all.

    Thanks
    Matt
     
  7. Sep 18, 2009 #6


    Thanks for the response. However my question still remains: Why is it that I can call the function myFunc(), in the same way, even if I define the parameter differently?

    I am aware of the concepts of pass-by-value and pass-by-reference. In my example I am using reference to an object, but my confusion is about the call.

    Different parameter definitions but same call succeeds. Why?
     
  8. Sep 18, 2009 #7
    The C++ lanquage allows for different options when making calls to functions. Based upon what your function does and how it is incorporated into your program, you may want to pass arguments to it through different methods.

    Maybe I don't understand your confusion on this issue but C++ (and other languages)allow for numerous methods to achieve the same goal.

    Thanks
    Matt
     
  9. Sep 18, 2009 #8

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Apparently you indeed don't understand mjaw's confusion :)
    I think that he is asking for how C++ handles pass-by-value and pass-by-reference internally.

    For example, when you declare the function to use a pointer, you tell the compiler that the function expects a pointer to an int. Then when you call the function, you have to explicitly obtain your variables address (&variable) and pass that to the function - then in the function you dereference the pointer.

    However, when passing by value or by reference, there is no such explicit construction. So what happens in these cases? For example, here is one possible explanation (which is probably nonsense, but hopefully will make you understand the question):

    The compiler internally rewrites all functions to take pointers as arguments, so myFunc(int x) and myFunc(int& x) are both rewritten to compiler_myFunc(int* x). When you call a function which passes-by-value, it creates a new variable (using the copy constructor) and passes its address to the function, i.e. it calls compiler_myFunc(new int(x)). When the function terminates, it cleans this temporary variable up again. When passing by reference, the compiler calls the function with the address of the original variable -- compiler_myFunc(&x) -- without making local copies.

    Is it more clear now? :)
     
  10. Sep 18, 2009 #9
    Yes, I understand what was happening, just couldn't get the answer to the person correctly.

    Thanks for your help CompuChip.

    Matt
     
  11. Sep 18, 2009 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    C++ does not have a "take the reference of" operator. The same thing arises here:
    int & iref = i;

    Just one of the many goofy things about the language. When you see myFunc(i) in someone else's code you have to look at the declaration of MyFunc to see whether what this is a call by value or a call by reference. This has led many organizations to make programming rules regarding call by reference. Some (e.g., google) only allow constant references (MyFunc(const int &) is allowed but MyFunc(int &) is illegal). Others disallow call by reference for primitive types. Some just out-and-out disallow call by reference, period.
     
  12. Sep 18, 2009 #11
    Yes, const correctness as the Numerical Recipes authors call it. I didn't know that is was an enforced rule at Google.

    Thanks
    Matt
     
  13. Sep 18, 2009 #12

    Thank you CompuChip. You make perfect sense.
    It is under-the-hood stuff and I will now look it up myself based on what you have written. It is actually a big thing for me, I will use your track to resolve other confusions too.

    Aside to CFDFEAGURU: using namespace std is very basic and I know it. But such a global using statement is not recommended. Authors of the book discussed in this thread also recommend against it and with very strong reasons.

    Thanks.
     
  14. Sep 18, 2009 #13

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    Good luck searching. Please bear in mind that a) it has been a long time since I programmed C++ and b) I know next to nothing about the inner workings of it. So please don't attach too much value to the "explanation" I just cooked up :)
     
  15. Sep 18, 2009 #14

    Thank you.
    You reveal something completely new to me. I will keep this thing in mind henceforth.
     
  16. Sep 18, 2009 #15
    I highly disagree with your books authors. It is also very inefficient to have to specify std:: before each of the standard functions. Hence the entire purpose of statement

    using namespace std;

    My programs are at minimum a few hundred lines long and usually end up being thousands and if I have to use the std:: operator every time I needed a standard cout or cin statment or any of the others that are support by using namespace std, I would be very aggravated.

    Who wrote this book?

    Thanks
    Matt
     
  17. Sep 18, 2009 #16
    I do not want to continue this discusion with you.
     
  18. Sep 18, 2009 #17
    Sorry, I mean no offense, I just don't agree with the authors. I was taught to use that statement and I use it. It is used routinely in many publications. Such as, Numerical Recipes, one of the most informative books there is on using C++ for numerical computations.

    Anyways, have a good day.

    Thanks
    Matt
     
  19. Sep 18, 2009 #18

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Five extra characters is highly inefficient? Come off it.

    There are many organizations that make "using" a verboten word in their programming standards, with no waivers allowed. (Why allow a waiver when the desired functionality can be achieved with the addition of five easy characters?)
     
  20. Sep 18, 2009 #19
    LOL, yeah I get that sometimes. Just following my own approach. Anyways, what do you think D H?

    EDIT: Let me clarify my position. I don't work for a software developement company, I write custom programs to design heat transfer equipment. I am one of 3 programmers and we all use the namespace std declaration.

    My goodness, there is no reason to argue with this. LOL Use whatever you like, I was only stating the fact that I don't agree.

    End EDIT:

    Thanks
    Matt
     
  21. Sep 18, 2009 #20

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You are overly enamored with Numerical Recipes. It is at best a good starting point. The algorithms are rudimentary, old, and all too often, buggy. The code is, simply put, atrocious.

    http://www.ibiblio.org/pub/languages/fortran/ch3-3.html
    http://www.astro.indiana.edu/~jthorn/numerical.html [Broken]
    http://amath.colorado.edu/computing/Fortran/numrec.html [Broken]
     
    Last edited by a moderator: May 4, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Accelerated C++ book - no solutions for exercises, should I still solve them?
Loading...