max function

Finding the Maximum of 6 integers without using if-statement

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 33427 Views - Last Post: 15 February 2008 - 11:10 PM Rate Topic: -----

#1 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

max function

Post icon  Posted 15 February 2008 - 01:32 PM

I am supposed to use int max() function to calculate the max of 6 integers without using any if-else statements.
Like : int max(4,69,56,23,58,90) ..?
How can I do it without using if else statement but by using int max() function..??

Thanks,
Is This A Good Question/Topic? 0
  • +

Replies To: max function

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: max function

Posted 15 February 2008 - 01:35 PM

But you can use if statements inside the function, yes?

All you have to do is compare all the values and return the greatest one. Easy is it not? :)

and yes

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:
Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

Re: max function

Posted 15 February 2008 - 01:38 PM

no I can't use if statement inside function. I can't use if -else at all. That is why this assignment is tricky but I can import max function from maths library from C++but this is only for 2 integers. I am supposed to compare 6 integers.
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: max function

Posted 15 February 2008 - 01:47 PM

well than that's easy declare five(you could do it with less but nevermind) new variables:
pseudo code
var1= max(4,69)
var2= max(56,23)
var3= max(58,90)
var4= max(var1,var2)
var5=max(var4,var3)

return var5;

Was This Post Helpful? 0
  • +
  • -

#5 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

Re: max function

Posted 15 February 2008 - 01:52 PM

I am supposed to write six functions
one for int max (int a, int b , int c)
one for int max(int a,int b , int c, int d)
one for int max(int a, int b, int c ,int d, int e)
onr for int max( inta, int b, int c, int d, int e, int f)
that pseducode gonna work for all of 'them?
Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: max function

Posted 15 February 2008 - 01:57 PM

No, no, the max function I use is the imported function from a library, that code should be inside the max function you need to write.
Was This Post Helpful? 0
  • +
  • -

#7 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

Re: max function

Posted 15 February 2008 - 01:59 PM

ok but I am supposed to write 6 functions(individually)
what about the function with 3,4 5 parametes?
U have done the function of 6 parameters!
Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: max function

Posted 15 February 2008 - 02:03 PM

OK you got me lost here, if you are supposed to write six functions among which there will be the ones with 3, 4, 5 arguments then follow the same logic I gave you. I give the pseudo code for 6 parameters, I can't see what is the trouble here?
Was This Post Helpful? 0
  • +
  • -

#9 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

Re: max function

Posted 15 February 2008 - 02:05 PM

ok I got it.
I will attach actual code if it doen't work later!!!
Was This Post Helpful? 0
  • +
  • -

#10 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: max function

Posted 15 February 2008 - 02:06 PM

Please instead of attaching the code post the code like this :code:
Was This Post Helpful? 0
  • +
  • -

#11 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

Re: max function

Posted 15 February 2008 - 02:29 PM

OK..here is my header file and class file. I am yet to create driver for this program,

Header file
#include <iostream>
#include <cmath>


class Maximum
{
public:
	  //Maximum(int);
  
	int max(int, int, int);
	int max(int, int, int, int);
	int max( int, int, int, int, int);
	int max( int, int, int, int, int, int);
}



here is my source file

#include <iostream>
#include <cmath>
//using std::cout;
//using std::cin;
using std::endl;

#include "Maximum.h"


int max (int a, int b , int c);
{
  int var1 =  max( a ,  b);
	var2 =  max(var1,int c);
	return var2;
}

int max(int a, int b, int c, int d);
{
  int var1 =  max( a,  b);
	int var2 =  max ( c, d);
	int var3 =  max(var1, var2);
	return var3;
}

int max (int a, int b, int c, int d, int e);
{
  int var1 =  max(  a,  b);
  int var2 =  max( c, d);
  int var3 =  max(var1, var2);
	int var4 =  max(var3,  e);
	return var4;
}

int max ( int a, int b, int c, int d, int e, int f);
{
  int var1 =  max( a,  b);
  int var2 =  max( c,  d);
  int var3 =  max( e,  f);
  int var4 =  max(var1, var2);
  int var5 =  max(var4, var3);
	return var5;
}


It is giving me errors?? what exactly is the problem??

*Mod Edit: added code tags: :code:
Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: max function

Posted 15 February 2008 - 02:36 PM

for one you need to add:
#include <algorithm>
to your code so that it knows about the max function.
Was This Post Helpful? 0
  • +
  • -

#13 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

Re: max function

Posted 15 February 2008 - 02:41 PM

nope still doesn't work!

it is saying now "missing function header" and "in line 10 maximum followed by int in illegal"
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: max function

Posted 15 February 2008 - 02:49 PM

well if all else fails you can try removing all of the semi-colons at the end of your function definitions. :)

You need them in the header file for the function declaration, but not in the definitions.
Was This Post Helpful? 0
  • +
  • -

#15 upant  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 15-February 08

Re: max function

Posted 15 February 2008 - 02:52 PM

yeah I tried that and it still doesn't work, any suggestions will be highly appreciated!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2