943,660 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3013
  • C++ RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Apr 10th, 2009
0

Re: Even sum of digits

The result is the same.
Both programs were intended to calculate the sum of the digits of a number, however, the second program's output is different, try it !
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 10th, 2009
0

Re: Even sum of digits

I agree that the sum is different. I said so before. All I say is that the result of what we're interested in, in this case, remains the same - just because the ASCII value of '0' modulo 2 is 0.
We're not interested in the sum of the digits. We're interested to know if the number's sum of digits is even or odd, or in other words: if it's 0 (modulo 2) or 1 (modulo 2) respectively.
Last edited by unbeatable0; Apr 10th, 2009 at 2:17 pm.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 10th, 2009
0

Re: Even sum of digits

Guys instead of USING AN ARPHODOX method to solve your conversion from char to numerical. Why dont you use the atoi() function built in. That way you will not need to care abt any format.

c++ Syntax (Toggle Plain Text)
  1. string a;
  2. unsigned short int Sum=0,i;
  3. a="12522";
  4. for(i=0;i<a.length();i++)
  5. {
  6. string b;
  7. b=a[i];
  8. Sum+=atoi(b.c_str())];
  9. }
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 10th, 2009
0

Re: Even sum of digits

Guys instead of USING AN ARPHODOX method to solve your conversion from char to numerical. Why dont you use the atoi() function built in. That way you will not need to care abt any format.

c++ Syntax (Toggle Plain Text)
  1. string a;
  2. unsigned short int Sum=0,i;
  3. a="12522";
  4. for(i=0;i<a.length();i++)
  5. {
  6. string b;
  7. b=a[i];
  8. Sum+=atoi(b.c_str())];
  9. }
Yeah, but with atoi you're again doing conversions, which means it's not the most efficient way ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 10th, 2009
0

Re: Even sum of digits

just because the ASCII value of '0' modulo 2 is 0.
Agreed, but that's logic: 48 modulo 2 is equal to 0 ...

We're interested to know if the number's sum of digits is even or odd
Agreed.

or in other words: if it's 0 (modulo 2) or 1 (modulo 2) respectively.
You'll have to explain this carefully using an example ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 10th, 2009
0

Re: Even sum of digits

Click to Expand / Collapse  Quote originally posted by tux4life ...
Agreed, but that's logic: 48 modulo 2 is equal to 0 ...


Agreed.


You'll have to explain this carefully using an example ...
C++ Syntax (Toggle Plain Text)
  1. 48%2=0;
THats true.
Unbeatable wants
C++ Syntax (Toggle Plain Text)
  1. 48,
  2. 4+8=12;//SUM OF DIGITS
  3. 12%2=0// even
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 10th, 2009
0

Re: Even sum of digits

Unbeatable wants
C++ Syntax (Toggle Plain Text)
  1. 48,
  2. 4+8=12;//SUM OF DIGITS
  3. 12%2=0// even
Agreed with what you're saying, I also want that, but my problem is that Unbeatable explained this in a very strange way ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 11th, 2009
0

Re: Even sum of digits

I thought I would throw another solution using recursion out there. Not fully tested but should do the job.
cpp Syntax (Toggle Plain Text)
  1. int sum_digits(int number)
  2. {
  3. int remainder = number%10;
  4. if (remainder > 0)
  5. return remainder+sum_digits((number-remainder)/10);
  6. else if (number >= 10 && remainder == 0)
  7. return sum_digits(number/10);
  8. return number;
  9. }
  10.  
  11. bool sum_digits_even(int number)
  12. {
  13. return !(sum_digits(number) % 2);
  14. }
Reputation Points: 22
Solved Threads: 28
Junior Poster
adam1122 is offline Offline
181 posts
since Mar 2009
Apr 12th, 2009
0

Re: Even sum of digits

sorry your algorithm is not working correctly. Any ideas
Reputation Points: 10
Solved Threads: 2
Light Poster
FREEZX is offline Offline
44 posts
since Mar 2009
Apr 12th, 2009
1

Re: Even sum of digits


c++ Syntax (Toggle Plain Text)
  1. bool isEven(const char* number)
  2. {
  3. bool even = false;
  4. if (number) {
  5. if (*number == '-' || *number == '+')
  6. number++;
  7. if (*number) {
  8. even = true;
  9. do switch (*number++) {
  10. case '1': case '3': case '5': case '7': case '9':
  11. even = !even;
  12. continue;
  13. case '0': case '2': case '4': case '6': case '8':
  14. continue;
  15. default: return false; // not a number
  16. } while (*number);
  17. }
  18. }
  19. return even;
  20. }
  21. inline
  22. bool isEven(const std::string& number) {
  23. return isEven(number.c_str());
  24. }
  25. /// for true integers:
  26. bool isEven(unsigned long number)
  27. {
  28. std::string buff;
  29. std::ostringstream is(buff);
  30. (is << number).flush();
  31. return isEven(buff);
  32. }
  33. using std::cout;
  34. using std::cin;
  35. void TestEven()
  36. {
  37. std::string line;
  38. while (cout << "Integer: ",std::getline(cin,line))
  39. cout << isEven(line) << '\n';
  40. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Console Text Alignment!
Next Thread in C++ Forum Timeline: Help needed for an iterator going out of bounds





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC