Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I have this program working with the commented version. But that is just my way to do it. The tutorial wants it done by applying pass by reference.

SO I started modifying it and now I'm a little stuck but almost there.

Can you guys take a look at it and tell me what am I missing here.

Thanks

C++
#include <iostream>
#include <cstdlib> //had to force it becasue my compiler (Code::Blocks) does not contain system.

using namespace std;
/*int n = 1, sum = 0;

int sumDigits(int n, int sum)
{
    //
	if (n == 0)
    {
        return sum;
    }
    else
    {
        // applying recursion and returning the value into the function
        sum = sum + n%10;
		n= n/10;
        return sumDigits(n, sum);
    }
}

int main(int argc, char* argv[])
{
	n = 1, sum = 0;

        cout << "Enter a non-negative integer: ";
        cin >> n;
        sum = sumDigits (n, sum);
        cout << "The sum of all digits "<< n << " is: " << sum << endl;

	system ("PAUSE");

        return 0;
}
*/

int sumDigits(int &);

int main()
{
	int n;
	sumDigits(n);
}

int sumDigits(int &n)
{
    cout << "Enter a non-negative integer: ";
    cin >> n;
        if (n == 1)
        {
            return 1;
        }
        else
        {
            return (n - 1) + n;
        }
    cout << "The sum of all digits "<< n << " is: " << n << endl;


	system ("PAUSE");

        return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jun-13 14:03pm    
"Tutorial wants..."? You personification in a kind of your figure of speech went too far. A tutorial does not "want" anything". Your post cannot make sense until you explain what do you want to achieve. And why passing by reference + recursion, without foolishness? I don't say you cannot do it, I'm asking why.
—SA
Sergey Alexandrovich Kryukov 10-Jun-13 16:07pm    
Please see my comment to my answer.
—SA

C++
void sum_digits(int & n, int & sum)
{
  if ( n == 0 ) return;
  sum += n % 10;
  n /= 10;
  sum_digits(n, sum);
}

#include <iostream>
using namespace std;

int main()
{
  int n, sum=0;
  cout << "enter a non-negative number" << endl;
  cin >> n;
  if ( n < 0 ) return -1; // don't trust the user
  sum_digits(n,sum);
  cout << "sum is " << sum << endl;
}
 
Share this answer
 
Comments
lotussilva 10-Jun-13 15:53pm    
Thank you CPallini. This makes a lot more sense to me. It's a little similar to my first design. But definitely cleaner. However, I'm a little raw at Cpp, and this may be a stupid question, but where is the pass by reference in your solution? I just wanna identify it if it's there.
Sergey Alexandrovich Kryukov 10-Jun-13 16:24pm    
Don't get me wrong: your question wasn't really stupid, otherwise I would not answer. It only should have been more comprehensively formulated...
—SA
CPallini 10-Jun-13 16:29pm    
C++ language specifies parameter passing by reference with the ampersand operator ('&') in the function definition (and declaration). I believe you now can see it.
lotussilva 10-Jun-13 16:43pm    
Of course! Lol. Right in front of my face! Thank you. I do see it!

edit...

I just got home and rechecked your code and it looks like I wasn't too far from the right answer. All I needed was to add the ampersand in the parameters. But Help you for helping me see it clearer.
Sergey Alexandrovich Kryukov 10-Jun-13 16:09pm    
My 5, but I would not answer OP like that; this person has wrong attitude; please see the comments to my answer and comment; besides, it looks like a school assignment, it's not fair to provide a complete solution.
—SA
You can use passing by reference, of course, but first look at my comment to the question.

The code (not commented) you provided, has two blocker problem: 1) n is not initialized before the first call; 2) there is no recursion at all.

Keep thinking. Even if I wanted to give you a complete solution, I could not, because your post does not contain the real specifications, and none of the existing code does not provide unambiguous description of what do you want to achieve. Formally, this is not even an acceptable approach to asking questions. Even if you assumed that your code should be considered as a correct spec for the problem, you cannot rely on it, because it's not known if this code is correct or not. It does suggest that the problem is to sum all the decimal digits in some previously number, so…

…if the assumption above is correct, the other problem of the code (not commented out) is: 3) the method sumDigits contains input in it; it shouldn't.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Jun-13 14:35pm    
Help who?! I personally don't need any help, but it looks like you do.

You haven't added much with this comment. This does not spec the problem. This is not too bad, but it's bad that you don't understand it.
Besides, your item statements don't look correct. For example, "value is passed by reference" is an apparent oxymoron. :-)

I just don't want to give you an answer. If you won't overcome very basic issues by yourself, consider the goal of an exercise is completely defeated.

—SA
Sergey Alexandrovich Kryukov 10-Jun-13 16:05pm    
OK, you have to understand some things. You need help, and I provide this help. You are the student who is supposed to solve some problem by yourself, without any help. Is that so?

However, we all realize that some people can have some particular problem in some cases, so we are trying to help, even is such cases. But it looks like "rude" for you means simply the idea that people are not doing all your work for you, that would be plain cheating. Will you call your professor "rude" for an insufficient mark on your exam? Or some cases like that? But that professor get some salary to teach you. Here, you are getting help from qualified people for free. And yet, you think you can call "rude" something which you don't like. Isn't that merely because you post here as anonymous? Is so, you are rude, and shameless.

With such attitude, you cannot hope to be welcome here. You don't even take the labor to help us to help you.

And let me decide by yourself where to comment and where not. I won't ask if you want some help from me or not. I can write what I think is right, no more, no less.

Thank you for understanding.
—SA
CPallini 10-Jun-13 16:22pm    
My 5.
Sergey Alexandrovich Kryukov 10-Jun-13 16:25pm    
Thank you, Carlo.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900