943,590 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3192
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 26th, 2008
0

Re: Looping Problem and some questions

Wow... I didn't realise that I missed out so many things.
I'm going to try get this code right. Thanks all!
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Jan 26th, 2008
0

Re: Looping Problem and some questions

Click to Expand / Collapse  Quote originally posted by WolfPack ...
When you call the function calculation , you will have to pass the variable names that you want to get the output from.

int main(void)
{
    int num;
    int first, last, total;
    while (num != 0)
    { 
        cout << "Enter an integer (0 to stop): ";
        cin >> num;
        
        if ( num != 0)
        {               
            calculation(num, last, first, total);
            cout << "Last digit:   " << last << endl;
            cout << "First digit:  " << first << endl;
            cout << "Total digits: " << total  << endl;
        }
    }
    
    cout << "End of task.\n\n";
    system("pause");
    
    return 0;   
    }

You do not want to pass t1 as an input for calculation, so change it like this. You do not need t1 at all.
void calculation (int x, int &last, int &first, int &total)
{
    last = (x % 10);
    while (x>9) 
        first = x / 10;

    while (x>0)
    { 
          total++;
          x /=10;
    }
}
The problem is that he's not passing all those variables in the arguments. They are CALCULATED and assigned within the function.
There is only one, "x"
Since it's a void function, the varaibles must exist as globals to 'survive" outside of the fuction scope.
otherwise it needs to be something else like int calculation(...) with a return.
I have this working with a single reference pass void calcultion (int& x) along with a few other minor loop repairs.
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Jan 26th, 2008
0

Re: Looping Problem and some questions

Almost!!! I'm now left with placing counter =0 for total, odd and even.
The problem is, I can't find a suitable place to slot them in. Hmmm...

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void calculation (int x, int& last, int& first,
  5. int& total, int& odd, int& even);
  6.  
  7.  
  8.  
  9. int main(void)
  10. {
  11. int num;
  12. int last, first, total, odd, even;
  13.  
  14.  
  15. while (num != 0)
  16. {
  17. cout << "Enter an integer (0 to stop): ";
  18. cin >> num;
  19.  
  20.  
  21. if ( num != 0)
  22. {
  23. calculation(num, last, first, total, odd, even);
  24. cout << "Last digit: " << last << endl;
  25. cout << "First digit: " << first << endl;
  26. cout << "Total digits: " << total << endl;
  27. cout << "Odd digits: " << odd << endl;
  28. cout << "Even digits: " << even << endl;
  29.  
  30. cout << "\n";
  31. }
  32. }
  33.  
  34.  
  35. cout << "End of task.\n\n";
  36. system("pause");
  37.  
  38. return 0;
  39. }
  40.  
  41. void calculation (int x, int& last,int& first,
  42. int& total, int& odd, int& even)
  43. {
  44.  
  45. last = (x % 10);
  46.  
  47. while (x>9)
  48. {
  49. x /=10;
  50. }
  51. first = x;
  52.  
  53. while (x>0)
  54. {
  55. total++;
  56. x /=10;
  57. }
  58.  
  59. while (x>0)
  60. {
  61. if (x%2 !=0)
  62. odd++;
  63. x /= 10;
  64. }
  65.  
  66. while (x>0)
  67. {
  68. if (x%2 ==0)
  69. even++;
  70. x /= 10;
  71. }
  72. }
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Jan 26th, 2008
0

Re: Looping Problem and some questions

Hi all, I just solved my assignment with help from here and there.
Thanks for the help! Thought I'll post the result here.

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void calculation (int x, int& last, int& first,
  5. int& total, int& odd, int& even);
  6.  
  7. int main(void)
  8. {
  9. int num, last, first, total, odd, even;
  10.  
  11. while (num != 0)
  12. {
  13. cout << "Enter an integer (0 to stop): ";
  14. cin >> num;
  15.  
  16. if ( num != 0)
  17. {
  18. calculation(num, last, first, total, odd, even);
  19. cout << "Last digit: " << last << endl;
  20. cout << "First digit: " << first << endl;
  21. cout << "Total digits: " << total << endl;
  22. cout << "Odd digits: " << odd << endl;
  23. cout << "Even digits: " << even << endl;
  24. cout << "\n";
  25. }
  26. }
  27.  
  28. cout << "End of task.\n\n";
  29. system("pause");
  30. return 0;
  31. }
  32.  
  33. void calculation (int x, int& last, int& first,
  34. int& total, int& odd, int& even)
  35. {
  36. total = 0;
  37. odd = 0;
  38. even = 0;
  39.  
  40. last = (x % 10);
  41.  
  42. while (x > 0)
  43. {
  44. total++;
  45.  
  46. if (x % 2 !=0)
  47. odd++;
  48.  
  49. else
  50. even++;
  51.  
  52. if (x < 9)
  53. first = x;
  54.  
  55. x /= 10;
  56. }
  57. }
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Jan 26th, 2008
1

Re: Looping Problem and some questions

congratulations! It works, albiet a bit inefficient.
As I said before, the only reference needed was x
and you pulled that one in as a value!
All you are doing by taking in all those other unneccessary references is to bring the variable scopes INSIDE the function. It works just as well if the variables were set as global. Aguments pass things in, not out!

I get the feeling that you have missed this point.
good luck.
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Jan 26th, 2008
2

Re: Looping Problem and some questions

Click to Expand / Collapse  Quote originally posted by JRM ...
congratulations! It works, albiet a bit inefficient.
As I said before, the only reference needed was x
and you pulled that one in as a value!
All you are doing by taking in all those other unneccessary references is to bring the variable scopes INSIDE the function. It works just as well if the variables were set as global. Aguments pass things in, not out!

I get the feeling that you have missed this point.
good luck.
No he has not missed the point. You have. Arguments can be used to pass things both in and out. Passing by reference can be used to get things out. It is also memory efficient because another copy of the variable is not created in the stack like it is done when you use passing by value.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jan 26th, 2008
1

Re: Looping Problem and some questions

I don't feel that your code works 100% correctly. What's happen when the first digit is number 9? 9 < 9 == false .
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jan 27th, 2008
0

Re: Looping Problem and some questions

Aaa I missed that, if I changed it to 10 it should work perfectly right?
Anyway, I send it in and my lecturer gave me 10/10 for it... Guess he missed it as well.
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Jan 27th, 2008
0

Re: Looping Problem and some questions

Click to Expand / Collapse  Quote originally posted by WolfPack ...
No he has not missed the point. You have. Arguments can be used to pass things both in and out. Passing by reference can be used to get things out. It is also memory efficient because another copy of the variable is not created in the stack like it is done when you use passing by value.
I ABSOLUTELY agree that passing by reference is memory efficient!
However, in his Calculation function, the one value (x) , is not passed by reference, but by value! My point was that only ONE variable actually needed to be passed in and that was x, not all of the ones that were. Variables first, last, etc, could be just as well been set in global scope along with their respective initial values.

I disagree with the fine point about arguments by reference being a way to get things out.
That is the domain of a return.
Passing those addition variables inside was only putting them in SCOPE within the function
braces, which otherwise would not have survived when the function returned or givin an "undefined variable " error.

Since all those other variables were assigned by different operations from the x variable,
it was the only one needed. The others were passive place holders for results.
It worked, but it just was unnecessarily complicated...

In the immortal words of Forrest Gump..."and thats all I've got to say about that"!
;-)
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Jan 27th, 2008
0

Re: Looping Problem and some questions

I disagree with the part that you said x should pass by reference. I think passing x by value is a perfect thing to do, since it won't affect the real value of num in the main() function.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Homework Help
Next Thread in C++ Forum Timeline: Setting Array to Zero with Constructor





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


Follow us on Twitter


© 2011 DaniWeb® LLC