The greater number

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 44
Reputation: nottoshabi is an unknown quantity at this point 
Solved Threads: 1
nottoshabi's Avatar
nottoshabi nottoshabi is offline Offline
Light Poster

The greater number

 
0
  #1
Mar 10th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: The greater number

 
0
  #2
Mar 10th, 2007
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?
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: The greater number

 
0
  #3
Mar 10th, 2007
you should just output the maximum of the two:
  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:
  1. int max(int a, int b)
  2. {
  3. if(a > b)
  4. return a;
  5. else
  6. return b;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: The greater number

 
0
  #4
Mar 10th, 2007
Originally Posted by joeprogrammer View Post
All that's necessary is for you to call the function in the cout call, and the value will be printed out.
Originally Posted by Infarction View Post
you should just output the maximum of the two:
  1. cout << "the max is " << max(firstnum, secondnum)) << endl;
That's what I meant to say. Really. :o
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 44
Reputation: nottoshabi is an unknown quantity at this point 
Solved Threads: 1
nottoshabi's Avatar
nottoshabi nottoshabi is offline Offline
Light Poster

Re: The greater number

 
0
  #5
Mar 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: The greater number

 
0
  #6
Mar 11th, 2007
Originally Posted by Infarction View Post
Here's a cleaner version:
  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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,630
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: The greater number

 
0
  #7
Mar 11th, 2007
Originally Posted by WaltP View Post
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: The greater number

 
0
  #8
Mar 11th, 2007
Originally Posted by WaltP View Post
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
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: The greater number

 
0
  #9
Mar 12th, 2007
Originally Posted by ~s.o.s~ View Post
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.

Originally Posted by Infarction View Post
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
  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. }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,630
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: The greater number

 
0
  #10
Mar 12th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC