8 Replies - 7459 Views - Last Post: 17 February 2012 - 10:36 PM Rate Topic: -----

#1 cbennett   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

How to find maximum value of set of variables

Posted 15 February 2012 - 05:18 PM

Hello,

I was wondering if anyone could help me find the maximum value of a set of variables and assign them to another variable. Here is a snippet of my code that may help with understanding what I am talking about.

// Ask for quarter values.
		System.out.println("What is the value of the first quarter?");
		firstQuarter = input.nextDouble();
		
		System.out.println("What is the value of the second quarter?");
		secondQuarter = input.nextDouble();
		
		System.out.println("What is the value of the third quarter?");
		thirdQuarter = input.nextDouble();
		
		System.out.println("What is the value of the fourth quarter?");
		fourthQuarter = input.nextDouble();
		
		//Tell client the maximum value/price of the stock during the year.		
		//maxStock = This is where I need help 
		System.out.println("The maximum price of a stock share in the year is: $" + secondQuarter + ".");




Is This A Good Question/Topic? 0
  • +

Replies To: How to find maximum value of set of variables

#2 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: How to find maximum value of set of variables

Posted 15 February 2012 - 06:01 PM

That is very simple. You need to create a variable with initial value 0. Then using the control structure 'if' check if the first value is greater than the value in that variable, of course the first time it will be, so store that value in the variable. Then check if the second value is greater than the value in the variable and do the same and so on.
something like:
int myVar = 0;
if(myValue1 > myVar){
  myVar = myValue1;
}
if(myValue2 > myVar){
  myVar = myValue2;
}
........
.......


This post has been edited by guido-granobles: 15 February 2012 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to find maximum value of set of variables

Posted 15 February 2012 - 08:19 PM

or just store the valued of firstQuarter into maxQuarter
every time you read a new quarter check if that value > maxQuarter
if it is the case replace maxQuarter with that new value
Was This Post Helpful? 0
  • +
  • -

#4 cbennett   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

Re: How to find maximum value of set of variables

Posted 15 February 2012 - 10:25 PM

This is what I ended up doing:

		//Tell client the maximum value/price of the stock during the year.		
		maxStock = Math.max( firstQuarter, Math.max( secondQuarter, Math.max( thirdQuarter, fourthQuarter ) ) );
		System.out.println("The maximum price of a stock share in the year is: $" + maxStock + ".");
		

Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to find maximum value of set of variables

Posted 15 February 2012 - 10:33 PM

kind of using a Cadillac to deliver a pizza but, if you can afford it :)
Was This Post Helpful? 1
  • +
  • -

#6 cbennett   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

Re: How to find maximum value of set of variables

Posted 15 February 2012 - 11:06 PM

View Postpbl, on 15 February 2012 - 10:33 PM, said:

kind of using a Cadillac to deliver a pizza but, if you can afford it :)



What exactly do you mean?
Was This Post Helpful? 0
  • +
  • -

#7 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: How to find maximum value of set of variables

Posted 16 February 2012 - 04:58 AM

View Postcbennett, on 16 February 2012 - 06:06 AM, said:

View Postpbl, on 15 February 2012 - 10:33 PM, said:

kind of using a Cadillac to deliver a pizza but, if you can afford it :)


What exactly do you mean?


Well it's a lot of function calls to do something simple. But I think any reasonable java implementation will inline this at some point during optimization.

You could save one max call/comparison though, this is equivalent:

maxStock = Math.max( Math.max(firstQuarter, secondQuarter), Math.max(thirdQuarter, fourthQuarter) );


Was This Post Helpful? 0
  • +
  • -

#8 cbennett   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

Re: How to find maximum value of set of variables

Posted 17 February 2012 - 10:32 PM

Out of curiosity, would what I did be considered the "Bubble Sort Algorithm"?
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to find maximum value of set of variables

Posted 17 February 2012 - 10:36 PM

View PostKarel-Lodewijk, on 16 February 2012 - 06:58 AM, said:

Well it's a lot of function calls to do something simple. But I think any reasonable java implementation will inline this at some point during optimization.

In C/C++ or in Fortran yes, but not in Java :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1