944,126 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1991
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 10th, 2007
0

The greater number

Expand Post »
It works. I'm having a problem trying to display the greater number
to the output. For example what ever number is greater the
first or the second it still displays the first number.
Couse that is what is coded in the cout. But I dont know
what to put in it to display the greater number instead
of the fisrt number. Also I dont know how to connect the fisrt part of the code to the second. For example the void block of code is completlly independent of the main. The program will run the same with the void function in or out.




  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. void Max(int& x, int& y)
  7. { // start of function body
  8. int max;
  9. int other; // variable declaration
  10.  
  11. if (x >= y)
  12. { // find the maximum number
  13. max = x;
  14. }
  15. else
  16. {
  17. (x <= y);
  18.  
  19. max = y;
  20. }
  21.  
  22. }
  23.  
  24.  
  25. int main()
  26. {
  27. void Max( int& , int&); // function declaration (prototype)
  28. int firstnum, secnum;
  29.  
  30. cout << "\nEnter a number: ";
  31. cin >> firstnum;
  32. cout << "Great! Please enter a second number: ";
  33. cin >> secnum;
  34.  
  35. cout << "\nThe maximum of these two values is "
  36. << firstnum << endl; // the function is
  37. //called here
  38.  
  39. system("PAUSE");
  40. return 0;
  41. }
Last edited by WaltP; Mar 11th, 2007 at 3:07 pm. Reason: Fixed: 'language' means the language you are using -- like C or CPP...
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
nottoshabi is offline Offline
44 posts
since Mar 2007
Mar 10th, 2007
0

Re: The greater number

Your function's prototype should not be inside another function, as your are doing here. It should also be BEFORE the function definition.

Change the function's return type from void to int. The return the value of 'max'. All that's necessary is for you to call the function in the cout call, and the value will be printed out.

By the way, how come you declare the variable 'other' when you don't have any use for it?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 10th, 2007
0

Re: The greater number

you should just output the maximum of the two:
C++ Syntax (Toggle Plain Text)
  1. cout << "the max is " << max(firstnum, secondnum)) << endl;
that said, your max function is wrong. It finds the max but should return the result. Here's a cleaner version:
C++ Syntax (Toggle Plain Text)
  1. int max(int a, int b)
  2. {
  3. if(a > b)
  4. return a;
  5. else
  6. return b;
  7. }
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Mar 10th, 2007
0

Re: The greater number

All that's necessary is for you to call the function in the cout call, and the value will be printed out.
Click to Expand / Collapse  Quote originally posted by Infarction ...
you should just output the maximum of the two:
C++ Syntax (Toggle Plain Text)
  1. cout << "the max is " << max(firstnum, secondnum)) << endl;
That's what I meant to say. Really. :o
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 10th, 2007
0

Re: The greater number

I will try to switch that around. About the other I dont know. I'm new to this. It all looks like chinese to me right now. I get the code from the professor I just need to tweek it to his specifications. If you ask me why I do certain things i dont know. thats what i'm trying to figure out.

Thanks for the help guys.
Reputation Points: 10
Solved Threads: 1
Light Poster
nottoshabi is offline Offline
44 posts
since Mar 2007
Mar 11th, 2007
0

Re: The greater number

Click to Expand / Collapse  Quote originally posted by Infarction ...
Here's a cleaner version:
C++ Syntax (Toggle Plain Text)
  1. int max(int a, int b)
  2. {
  3. if(a > b)
  4. return a;
  5. else
  6. return b;
  7. }
Except multiple returns from a single function is frowned upon. Best to keep your original function with the return changes mentioned above, and with one other minor change:
  1. if (x >= y)
  2. { // find the maximum number
  3. max = x;
  4. }
  5. else
  6. {
  7. // (x <= y); remove this line, it does nothing
  8. max = y;
  9. }
Last edited by WaltP; Mar 11th, 2007 at 3:14 pm.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,748 posts
since May 2006
Mar 11th, 2007
0

Re: The greater number

Click to Expand / Collapse  Quote originally posted by WaltP ...
Except multiple returns from a single function is frowned upon.
Hmm...now that is some news

The statement is not absolute as such. It all depends on the problem under consideration.
Last edited by ~s.o.s~; Mar 11th, 2007 at 4:15 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 11th, 2007
0

Re: The greater number

Click to Expand / Collapse  Quote originally posted by WaltP ...
Except multiple returns from a single function is frowned upon.
That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as
C++ Syntax (Toggle Plain Text)
  1. int max(int a, int b) {
  2. return (a > b ? a : b);
  3. }
but I didn't want to post that one for clarity's sake.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Mar 12th, 2007
0

Re: The greater number

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
The statement is not absolute as such. It all depends on the problem under consideration.
Which is why I stated it as I did -- not as an absolute.

Click to Expand / Collapse  Quote originally posted by Infarction ...
That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as
C++ Syntax (Toggle Plain Text)
  1. int max(int a, int b) {
  2. return (a > b ? a : b);
  3. }
but I didn't want to post that one for clarity's sake.
Very good choice!!

Purists frown on multiple returns. They can be harder to debug because sometimes a return gets lost in complex or long functions. IMO, you should try to use only one return point if the code to support it does not make the code overly complex. But don't make the code difficult to read to accomplish it.

For this piece of code I'd probably use:
  1. int max(int a, int b)
  2. {
  3. int rtn = a;
  4. if(a < b) rtn = b;
  5. return rtn;
  6. }
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is online now Online
7,748 posts
since May 2006
Mar 12th, 2007
0

Re: The greater number

I guess it will depend on the coding standards of the company you are working for. Here are two implementations of the function which finds the index of an element in the array and returns -1 if not found.

  1. // MULTIPLE RETURNS
  2.  
  3. int indexOf2 (int array[], int length, int element)
  4. {
  5. for (int i = 0; i < length; ++i)
  6. {
  7. if (array [i] == element)
  8. return i ;
  9. }
  10. return -1 ;
  11. }
  12.  
  13. // SINGLE RETURN
  14.  
  15. int indexOf1 (int array[], int length, int element)
  16. {
  17. int index = -1 ;
  18.  
  19. for (int i = 0; i < length; ++i)
  20. {
  21. if (array [i] == element)
  22. {
  23. index = i ;
  24. break ; // btw even break and continue are frowned upon
  25. }
  26. }
  27. return index ;
  28. }

Take your pick...
Last edited by ~s.o.s~; Mar 12th, 2007 at 12:49 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

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: shouldn't this work??
Next Thread in C++ Forum Timeline: How to controll I/O in soundcard





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


Follow us on Twitter


© 2011 DaniWeb® LLC