Reputation: 0 Apprentice
- Active Members
- Active Posts:
- 142 (0.18 per day)
- 14-March 11
- Profile Views:
- Last Active:
- Apr 30 2013 07:10 AM
- Dream Kudos:
Posts I've Made
Posted 30 Apr 2013Another way to solve it would be to use a single for loop that loops through the elements in the vector and then a if condition in the loop that tests whether the index is even or odd.
Hint: To test even and odd use the modulus operator %
I was using the modulus op % but my stupid self used an if/else instead of a loop...thanks again Zereo
Posted 30 Apr 2013
//alternating sum for (int i=0;i<v.size();i+=2) altsumEven+=v[i]; for (int i=1;i<v.size();i+=2) altsumOdd+=v[i]; altsum=altsumEven-altsumOdd; // takes sum of all even positions and subtracts by sum of all odd positions in the vector
Got it thanks alot
Posted 14 Mar 2013Ohh so that's why haha
Posted 14 Mar 2013Yes did you not read my last comment. I knew the answer but it wasn't working and it was because I was forgetting a semicolon after [n-1] for the last element in the array...
I apologize, turns out my comment didn't post for some reason but I figured it out a long time ago, was a stupid mistake.
Posted 14 Mar 2013I can do that for the first element of the array, but how to do it for the last ?
- Member Title:
- D.I.C Head
- Age Unknown
- Birthday Unknown
daonlyillwiz hasn't added any friends yet.