Looping Problem and some questions

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Looping Problem and some questions

 
0
  #11
Jan 26th, 2008
Wow... I didn't realise that I missed out so many things.
I'm going to try get this code right. Thanks all!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 233
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Looping Problem and some questions

 
0
  #12
Jan 26th, 2008
Originally Posted by WolfPack View Post
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.
"I like beating by head against the wall because it feels so good when I stop"
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Looping Problem and some questions

 
0
  #13
Jan 26th, 2008
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...

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Looping Problem and some questions

 
0
  #14
Jan 26th, 2008
Hi all, I just solved my assignment with help from here and there.
Thanks for the help! Thought I'll post the result here.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 233
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Looping Problem and some questions

 
1
  #15
Jan 26th, 2008
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.
"I like beating by head against the wall because it feels so good when I stop"
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Looping Problem and some questions

 
2
  #16
Jan 26th, 2008
Originally Posted by JRM View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Looping Problem and some questions

 
1
  #17
Jan 26th, 2008
I don't feel that your code works 100% correctly. What's happen when the first digit is number 9? 9 < 9 == false .
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Looping Problem and some questions

 
0
  #18
Jan 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 233
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Looping Problem and some questions

 
0
  #19
Jan 27th, 2008
Originally Posted by WolfPack View Post
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"!
;-)
"I like beating by head against the wall because it feels so good when I stop"
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Looping Problem and some questions

 
0
  #20
Jan 27th, 2008
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.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2516 | Replies: 20
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC