Jump to content

overloaded functions.....

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Well im reading up on overloaded functions now, and i'm struggling on this practice problem.

Quote

Can you write overloaded min functions. One function will return the minimum of two integers, the second function will return the minimum of 3 integer values.


*****Hint: see if you can use the two argument min function for the three argument min function******



Notes: This means you need two functions called min

well for someone reason, im not sure on how to fund the min on the values? are if statements the best?

done one similar,
#include <iostream>


using namespace std;


int addEm(int a, int b, int c);

int addEm(int a, int b);


int main()

{

int x,y,z;


   cout << "Enter 3 integers " << endl;

   cin >> x >> y >> z;


   cout << "The sum of the first two is " << addEm(x, y) << endl;

   cout << "The sum of all Three is " << addEm(x, y, z) << endl;


}


int addEm(int a, int b)

{


   return a + b;


}


int addEm(int a, int b, int c)

{


   return a + b + c;


} 

 

so im just not sure what you think the best way is to find the mins are?

thanks guys for any help, rep will be given!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You will have to use if statements to determine which is smallest.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
Why are you adding values, it says find the minimum...

int min;
min = a < b ? a : b;
return min;

if-else shorthand.

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
int min(int a, int b, int c)
{
   return min(min(a,b), c);
}


#5
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
oh i know i have to find the min, i was just showing another SIMILAR program that was done, i mean, has the same idea behind it.....

thanks for the help so far!

#6
Hawk1

Hawk1

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
so does something like this seem right? is this what the question is asking for.. im sort of confused

#include <iostream>


using namespace std;


int min(int a, int b);

int min(int a, int b, int c);

int small;

int smaller;


int main()

{

	int x,y,z;


	cout << "Enter 3 integers " << endl;

	cin >> x >> y >> z;


	cout << "The minimum of the first two is " << min(x, y) << endl;

	cout << "The minimum of all Three is " << min(x, y, z) << endl;


}


int min(int a, int b)

{


	small = a < b ? a : b;


	return small;

}


int min(int a, int b, int c)

{

	small = a < b ? a : b;


	if( a < b && a < c)

	{

		return a;

	}

		smaller = b < c ? b : c;

	return smaller;

}










whats that tip mean? try and use the 2 argument min for the 3 argument?

#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
It's easy enough to try. And there is no need for globals (in fact, you should prefer to avoid them).

#include <iostream>

int min(int a, int b)
{
   return a < b ? a : b;
}

int min(int a, int b, int c)
{
   return min(min(a,b), c);
}

int main()
{
   int x = 5, y = 3, z = 7;
   std::cout << min(x,y,z) << '\n';
   return 0;
}