Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quick c++ basics prob?

Options
  • 02-05-2009 10:55pm
    #1
    Registered Users Posts: 329 ✭✭


    Hey just studying arrays, cant seem to remember the basics...

    For the code

    int billy [] = {16, 2, 77, 40, 12071};
    int n, result=0;

    int main ()
    {
    for ( n=0 ; n<5 ; n++ )
    {
    result += billy[n];
    }

    Just wondering for text in bold is this equivalent to
    result = billy[n] + 1;

    Thanks..


Comments

  • Moderators Posts: 51,799 ✭✭✭✭Delirium


    Would it not be the equivalent to

    result = result + billy[n];

    If you can read this, you're too close!



  • Closed Accounts Posts: 448 ✭✭ve


    No I'm afraid those 2 lines are not the same.

    Essentially that code is cycling through the integer array you created and summing all the numbers. So on the first loop iteration the result variable is set to 16, on the next iteration it's set to 18, on the 3rd iteration it's set to 95...and so on.

    I think you may be confusing ++ and += which are not the same. If you had the following.

    int i = 5;

    and then went, i++; // you would end up with 6

    alternatively, if you had

    int i = 5, j = 6;

    and then went i += j; // you would end up setting i to 11

    i += j above is the same as saying i = i + j;

    HTH ;)


  • Registered Users Posts: 329 ✭✭Nappy


    Ah yea,

    Cheers...:o


  • Registered Users Posts: 32,417 ✭✭✭✭watty


    To start with type more and avoid compound operators which where invented as shorthands because in 1976 we could not type and the compilers were rubbish.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Thats terrible advise Watty.


  • Advertisement
  • Closed Accounts Posts: 124 ✭✭BertrandMeyer


    // start of code
    int watty = billy[n];  // set watty to billy[n]
    result = result + watty;  // add watty to the result
    // end of code
    


  • Registered Users Posts: 32,417 ✭✭✭✭watty


    Do you want the programs to look like 1976 C?
    Or do you want the programs to be easy to maintain and bug free?

    Original C operators and syntax was all about speed of text entry and hints to compilier. Your main consideration should not be speed of text entry but program correctness.

    Using tradition style C programming and cramming as much as possible in one line is why there is so much buggy software and so many C++ programs that could practically use a C compiler.

    Do you want programs that always do as intended or ones that are "cool" terse C.

    I thought really this question had been answered 25 years ago. But obviously some people weren't paying attention.

    ADA goes too far on the verbose trip though. The next person maintaining the SW may not be as expert as you.

    I may have a strange outlook, having done Modula-2 & Occam before C++. I learnt C++ in 1987 and did my 1st major C project a year later.

    My advice may seem strange, but 20 years of programs that mostly work 1st time suggests it's not bad advice. Just because a language feature exists, does not mean that today you should use it.
    I'm sure you can think of many C library functions that are traps for the unwary.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    I understand that clarity is key, especially in a group project, but surely saying to expand the "+=" operator is a bit much? I'd say you'd cause more confusion by not using it.

    I agree though of course, the less obfuscated the code is the better.


  • Closed Accounts Posts: 124 ✭✭BertrandMeyer




Advertisement