954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

The greater number

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.

#include <iostream>
using namespace std;


void Max(int& x, int& y)
{                    // start of function body
  int max; 
  int other;       // variable declaration

  if (x >= y) 
  {       // find the maximum number
    max = x;
  }
  else
  {
    (x <= y);
    
    max = y;
  }

}


int main()
{
  void Max( int& , int&);  // function declaration (prototype)
  int firstnum, secnum;

  cout << "\nEnter a number: ";
  cin  >> firstnum;
  cout << "Great! Please enter a second number: ";
  cin  >> secnum;

  cout << "\nThe maximum of these two values is "
       << firstnum << endl; // the function is 
                                             //called here

  system("PAUSE");
  return 0;
}
nottoshabi
Light Poster
44 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

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?

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

you should just output the maximum of the two:

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:

int max(int a, int b)
{
  if(a > b)
    return a;
  else
    return b;
}
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 
All that's necessary is for you to call the function in the cout call, and the value will be printed out.

you should just output the maximum of the two:

cout << "the max is " << max(firstnum, secondnum)) << endl;

That's what I meant to say. Really. :o

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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.

nottoshabi
Light Poster
44 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

Here's a cleaner version:

int max(int a, int b)
{
  if(a > b)
    return a;
  else
    return b;
}


Except multiple returns from a single function is frowned upon. Best to keep your original function with thereturn changes mentioned above, and with one other minor change:

if (x >= y) 
{       // find the maximum number
    max = x;
}
else
{
//    (x <= y);    remove this line, it does nothing
    max = y;
}
WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
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

int max(int a, int b) {
  return (a > b ? a : b);
}


but I didn't want to post that one for clarity's sake.

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 
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. ;)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

int max(int a, int b) {
  return (a > b ? a : b);
}


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 shouldtry 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:

int max(int a, int b)
{
    int rtn = a;
    if(a < b)  rtn = b;
    return rtn;
}
WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

// MULTIPLE RETURNS

int indexOf2 (int array[], int length, int element)
{
    for (int i = 0; i < length; ++i)
    {
        if (array [i] == element)
            return i ;
    }
    return -1 ;
}

// SINGLE RETURN

int indexOf1 (int array[], int length, int element)
{
    int index = -1 ;

    for (int i = 0; i < length; ++i)
    {
        if (array [i] == element)
        {
            index = i ;
            break ;     // btw even break and continue are frowned upon
        }
    }
    return index ;
}


Take your pick... ;)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Why are these things frowmed upon?

nottoshabi
Light Poster
44 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

>Why are these things frowmed upon?
Reread WaltP's post:
Purists frown on multiple returns. They can be harder to debug because sometimes a return gets lost in complex or long functions.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Ok got it thanks.

nottoshabi
Light Poster
44 posts since Mar 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You