Trouble with loops - Calculate sum of even and odd integers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Trouble with loops - Calculate sum of even and odd integers

 
0
  #1
Oct 6th, 2008
As a homework assignment, I was asked to write a program in C++ that calculates the sum of the even and odd integers in a list of integers given by the user. I think I'm on the right track, but I can't seem to get my loop right. Here's what I have so far:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num=0, even=0, odd=0, i = 0;
  9. char stop = 'a';
  10.  
  11. cout << "Enter a list of integers followed by the '$' sign: " << endl;
  12. cin >> num;
  13. stop = num;
  14.  
  15. while(stop != '$')
  16. {
  17. if(num % 2 == 0) //If the number is even
  18. even = even + num; //Add it to the current sum of even integers
  19. else if(num % 2 != 0) //if the number is odd
  20. odd = odd + num; //Add it to the current sum of odd integers
  21. else
  22. {
  23. i = 1; //Otherwise, make i equal to 1
  24. }
  25.  
  26. cin >> num;
  27. stop = num;
  28. }
  29.  
  30. if(i == 1)
  31. cout << "Please enter only integers" << endl;
  32. else if(i == 0)
  33. {
  34. cout << "The sum of the even integers is: " << even << endl;
  35. cout << "The sum of the odd integers is: " << odd << endl;
  36. }
  37.  
  38. return 0;
  39. }

Thanks for the help
Last edited by ShadowOfBlood; Oct 6th, 2008 at 3:43 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #2
Oct 6th, 2008
Look at line 13

  1. stop = num;

Now ask yourself, "self, whats stop?"
"a character."
"self, whats num?"
"an integer."

Whats probably going down is that the the char (stop) is being assigned an integer, which in effect is tossing your loop off.

Are you only interested in positive integers? Because then you could ask for a negative value to terminate that list of integers.

  1. while(num != -1)
Last edited by chococrack; Oct 6th, 2008 at 3:54 am.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #3
Oct 6th, 2008
The question states: Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers.

I'm assuming it means positive and negative integers. Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #4
Oct 6th, 2008
Nvm, I was going about it wrong. I figured it out. Here's what I have:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int even = 0, odd = 0, dig, num;
  9.  
  10. cout << "Please enter a list of numbers (ex. - 12345)" << endl;
  11. cin >> num;
  12.  
  13. while (num > 0)
  14. {
  15. dig = num % 10;
  16. num = num / 10;
  17.  
  18. if(dig % 2 == 0)
  19. even = even + dig;
  20. else
  21. odd = odd + dig;
  22. }
  23.  
  24. cout << "The sum of the even integers is: " << even << endl;
  25. cout << "The sum of the odd integers is: " << odd << endl;
  26.  
  27. return 0;
  28. }

Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #5
Oct 6th, 2008
Dont You Think it is giving you the wrong answer?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #6
Oct 6th, 2008
It was giving me right answers when I checked it O.o

Is something wrong with it?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #7
Oct 6th, 2008
Originally Posted by ShadowOfBlood View Post
Nvm, I was going about it wrong. I figured it out. Here's what I have:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int even = 0, odd = 0, dig, num;
  9.  
  10. cout << "Please enter a list of numbers (ex. - 12345)" << endl;
  11. cin >> num;
  12.  
  13. while (num > 0)
  14. {
  15. dig = num % 10;
  16. num = num / 10;
  17.  
  18. if(dig % 2 == 0)
  19. even = even + dig;
  20. else
  21. odd = odd + dig;
  22. }
  23.  
  24. cout << "The sum of the even integers is: " << even << endl;
  25. cout << "The sum of the odd integers is: " << odd << endl;
  26.  
  27. return 0;
  28. }

Thanks for the help

what is quite confusing is the 'num' integer.
You said that you are taking input of a list of integers while you have just asked for a single integer.
I assume that you have taken the input of "list of integers" in a single integer 'num' itself, i.e., you have considered the value of 'num' as a list of integers (digit by digit)
if this is what you have assumed, i think this is a wrong approach. You would have rather used an array.
Think upon it.
Bhoot
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #8
Oct 6th, 2008
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int even = 0, odd = 0, dig, num;
  9.  
  10. cout << "Please enter a list of numbers (-7777 to terminate)" << endl;
  11. cin >> num;
  12.  
  13. while (num != -7777) // off the wall check value
  14. {
  15.  
  16. // dig = num % 10;
  17. // num = num / 10; <--- what are these for?
  18.  
  19.  
  20.  
  21. if(dig % 2 == 0) // if the number is even
  22. even += num; // add it to the previous even sum
  23. else
  24. odd += num; // otherwise add it to the odd sum
  25.  
  26. cout << "Enter next number: ";
  27. cin >> num;
  28. }
  29.  
  30. cout << "The sum of the even integers is: " << even << endl;
  31. cout << "The sum of the odd integers is: " << odd << endl;
  32.  
  33. return 0;
  34. }
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #9
Oct 6th, 2008
I haven't learned arrays yet :\

And what if the user wants to input -7777 as part of the list?

I dunno what I should do with this program :\
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: Trouble with loops - Calculate sum of even and odd integers

 
0
  #10
Oct 6th, 2008
K, class has arrived. Thanks for the help =)

I decided to use the example with the -7777 terminating value =)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC