![]() |
|
#1
|
|||
|
|||
Sum of the even numbers between n1 and n2Could someone please help me with this example in a repetition nested control structure from my textbook.
sum of the even numbers between n1 and n2 CPP / C++ / C Code:
say I assign n1=3 and n2=11 ok now i enter the for loop i % 2 never equals 0 so how do the sum of the even numbers between 3 and 11 get added ? Boggeling! Last edited by admin : 01-Nov-2007 at 16:50.
Reason: Please insert your C/C++ example codes between [CPP] and [/CPP] tags
|
|
#2
|
|||
|
|||
Re: quick problemQuote:
Your book should explain how the modulus operator (the '%') works. It gives the remainder after integer division. The expresson i % 2 is equal to zero if (and only if) the integer i divided by 2 has zero remainder. That's precisely what is needed to identify an even number. CPP / C++ / C Code:
Output: Code:
Regards, Dave Last edited by davekw7x : 01-Nov-2007 at 14:12.
|
|
#3
|
|||
|
|||
Re: quick problemso are you saying even though (i % 2 != 0) because 3 % 2 = 1
the program would still execute sum = sum + 1; ???? it looks like to me the program shouldnt execute sum = sum + 1 because the if condition is not met in if (i % 2 == 0 ) CPP / C++ / C Code:
Last edited by admin : 01-Nov-2007 at 16:52.
Reason: Please insert your C/C++ example codes between [CPP] and [/CPP] tags
|
|
#4
|
|||
|
|||
Re: quick problemQuote:
Quote:
In your program the only statement that got executed for a true condition is the sum = sum + i. The statements after that always get executed. Try again. I respectfully suggest that you not just look at the code; compile and execute it. CPP / C++ / C Code:
Output: Code:
If something happens that you don't understand, then make the program tell you what it is seeing. If you still don't get it, then show us exactly what code you are trying and exactly what your output is. (You might also tell us what compiler you are using. Sometimes it makes a difference to people who are trying to help.) Regards, Dave |
|
#5
|
|||
|
|||
Re: quick problemthanks for your help. I just joined the forum today and am now loving it!
|
|
#6
|
|||
|
|||
Re: quick problemQuote:
Welcome. You'll find that your questions were answered by one of the forums' best resources here on GIDForums. Dave is one of the most helpful people to turn to when you have a problem. There are quite a few good programmers on this forum who are ready to help, particularly when posters follow the guidelines and ask questions that are reasonably understandable. You may want to take a moment to review the guidelines and to also consider using [c++] ...your code... [/c++] tags for your code...as Dave did in his response to you. These help format the code in a visually appealing manner similar to syntax highlighting code editors and is one of the better features of this forum compared to others "out there." :davis: |
|
#7
|
||||
|
||||
Re: Sum of the even numbers between n1 and n2It looks like your question is already answered, but just FYI, you could compute this without using a loop at all, like this:
CPP / C++ / C Code:
This comes from the formula derived by Gauss for the sum of the numbers from 1 through n: 1 + 2 + ... + n = (n + 1) * n / 2 Multiplying each side by 2, we get 2 + 4 + ... + 2n = (n + 1) * n Now, we'll add (n + 1) * 2m to each side for some integer m. (n + 1) * 2m + 0 + 2 + 4 + ... + 2n = (n + 1) * n + (n + 1) * 2m We'll factor each side now: (0 + 2m) + (2 + 2m) + (4 + 2m) + ... + (2n + 2m) = (n + 1) * (n + 2m) Now, start is the smallest even integer that is greater than or equal to n1 (the first number in the sum), and end is the largest even integer that is less than or equal to n2 (the last number in the sum). The sum is then (0 + start) + (2 + start) + (4 + start) + ... + (end + start) We'll use the following substitutions: n = (end - start) / 2 m = start / 2 Then we have (0 + start) + (2 + start) + (4 + start) + ... + (end + start) = ((end - start)/2 + 1) * ((end - start)/2 + start) = [(end - start + 2) / 2] * [(end - start + 2*start)/2] = (end - start + 2) * (end + start) / 4 Dividing by four is the same as shifting right by 2, so we get (end - start + 2) * (end + start) >> 2 __________________
www.blake-foster.com |
|
#8
|
|||
|
|||
Re: Sum of the even numbers between n1 and n2Quote:
And, what is the sum of even integers between -11 and -3? The Original Poster's algorithm gives the correct answer: -28. By my reckoning your program gives -42. Now, Gauss's methodology works for integers in all ranges (negative and positive). At what point did you introduce the error(s) in the program? Regards, Dave "The purpose of computing is insight not numbers." ---Richard W. Hamming |
|
#9
|
||||
|
||||
Re: Sum of the even numbers between n1 and n2Good point. This fixes that problem:
CPP / C++ / C Code:
I just changed the modulus operators to the bitwise and operator. This works because n % 2 can be negative, while n & 1 is always positive since the high-order bit is forced to be zero. I also made a mistake typing out my derivation. I derived it by hand and then typed it, so the end result is right. I just copied one step incorrectly. the last bit should read: The sum is then (0 + start) + (2 + start) + (4 + start) + ... + (end - start + start) We'll use the following substitutions: n = (end - start) / 2 m = start / 2 Then we have (0 + start) + (2 + start) + (4 + start) + ... + (end - start + start) = ((end - start)/2 + 1) * ((end - start)/2 + start) = [(end - start + 2) / 2] * [(end - start + 2*start)/2] = (end - start + 2) * (end + start) / 4 __________________
www.blake-foster.com Last edited by Blake : 01-Nov-2007 at 21:53.
|
|
#10
|
|||
|
|||
Re: Sum of the even numbers between n1 and n2Quote:
I agree. You (and Gauss) do good work. Some insight into the "%" operator is always interesting. If we are dealing with positive ints, there isn't much ambiguity. However...given that a%b can give a negative result (when a is negative), would it have worked if you had used abs(n1 %2) and abs(n2%2) instead of the neater (sneaky, but clever/cool) bit operations? Regards, Dave |
Recent GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Table | killer9012 | C++ Forum | 29 | 10-Oct-2006 21:15 |
| subscript error in coding | warborules | C Programming Language | 6 | 27-Nov-2005 17:16 |
| sorting numbers through array | zidanefreak01 | C++ Forum | 3 | 26-Jun-2005 02:41 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 08:36 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The