7 Replies - 7158 Views - Last Post: 18 November 2010 - 10:24 PM Rate Topic: -----

#1 geraldined  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-November 10

sorting min and max values in an array

Posted 18 November 2010 - 05:17 PM

I don't understand what this portion of my code means can anybody tell me the parts to it???

 
		int min = Integer.MAX_VALUE, minIndex = 0;
		int max = Integer.MIN_VALUE, maxIndex = 0;



and this is the entire code if anyone needs it to help tell me what it is...


public class MinMaxReverse {

	public static void main(String[] args) {
		int[] arr = {1, 2, -3000, 4, 5, -100, 3, 50000, 700, 2, 4, 6, 8000};
		int[] rev_arr = new int [arr.length];

		int min = Integer.MAX_VALUE, minIndex = 0;
		int max = Integer.MIN_VALUE, maxIndex = 0;

		for (int i = 0; i < arr.length; i++) {
			if (arr[i] < min)
				min=arr[i];
			if (arr[i] < max)
				max=arr[i];
			/* Fill in your code here to find min/max
			 * There's only one loop needed
			 */	
		}

		System.out.println("Min number is " + min + " found at index " + minIndex);
		System.out.println("Max number is " + max + " found at index " + maxIndex);

		for (int i = 0; i < arr.length; i++) {
			if (rev_arr[i] > min)
				max=i;
			if (rev_arr[i] < max)
				min=i;
			/* Fill in your code here 
			 * To reverse the arr into rev_arr
			 */	
		}

		// Printing out the results
		System.out.println("\nPrinting out both array contents and they should be the same");
		for (int i = arr.length; i > 0; i--) {
			System.out.print("Arr[" + (arr.length-i) + "]=" + arr[arr.length-i]);
			System.out.println("\tsame as \tRev_Arr[" + (i-1) + "]=" + rev_arr[i-1]);
		}

	}

}



I am aware that the places where I'm supposed to fill in the code are wrong. I'm hoping that by understanding what the part I had in question means will help me figure out what code to put in.

Is This A Good Question/Topic? 0
  • +

Replies To: sorting min and max values in an array

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: sorting min and max values in an array

Posted 18 November 2010 - 05:23 PM

Quote

I don't understand what this portion of my code means can anybody tell me the parts to it???

hmmm... your code, and you don't know what it means? weird...
Anyways...
from Java API:
Integer.MAX_VALUE:
A constant holding the maximum value an int can have, 2^31 -1.
Integer.MIN_VALUE:
A constant holding the minimum value an int can have, -2^31.

int min = Integer.MAX_VALUE, minIndex = 0;
int max = Integer.MIN_VALUE, maxIndex = 0;

you declare two int variables, min and max, and assign them the minimu and maximum values an int variable can hold.
Was This Post Helpful? 0
  • +
  • -

#3 geraldined  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-November 10

Re: sorting min and max values in an array

Posted 18 November 2010 - 05:29 PM

Well technically it's for a lab I have and our task is to fill in the rest of the code. I should have put "code I'm working with" Sorry.>.<
so what does the minIndex=0 and maxIndex=0 part mean?
Was This Post Helpful? 0
  • +
  • -

#4 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 222
  • Joined: 05-August 09

Re: sorting min and max values in an array

Posted 18 November 2010 - 05:31 PM

View Postgeraldined, on 19 November 2010 - 09:17 AM, said:

I don't understand what this portion of my code means can anybody tell me the parts to it???

 
		int min = Integer.MAX_VALUE, minIndex = 0;
		int max = Integer.MIN_VALUE, maxIndex = 0;



ok well int min = Integer.MAX_VALUE declares the Integer variable min to be the maximum value an Integer can have which is 2147483647. Now when you continue the same line with , minIndex = 0; it means that you are also declaring minIndex as an Integer but of value 0.

The part of the next line int max = Integer.MIN_VALUE declares the Integer variable max to be the minimum value an Integer can have which is -2147483648. Now when you continue the same line with , maxIndex = 0; it means that you are also declaring maxIndex as an Integer but of value 0.

This post has been edited by Travis1012: 18 November 2010 - 06:08 PM

Was This Post Helpful? 1
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: sorting min and max values in an array

Posted 18 November 2010 - 05:33 PM

minIndex and maxIndex are more logic variables declared by "you" to store the index in the array of the min value (stored in minIndex) and the max value (stored in maxIndex).
They are initialized to 0 as default (0 is the value the compiler initialize global variables to), or perhaps because that is the first index in arrays. (Both options are logic, and rational).
Was This Post Helpful? 0
  • +
  • -

#6 geraldined  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-November 10

Re: sorting min and max values in an array

Posted 18 November 2010 - 05:36 PM

Oh okay thank you!!
Was This Post Helpful? 0
  • +
  • -

#7 geraldined  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-November 10

Re: sorting min and max values in an array

Posted 18 November 2010 - 10:15 PM

Could someone tell me why the min and max are set to Integer.MAX_VALUE and Integer.MIN_VALUE??? What's the point?
Was This Post Helpful? 0
  • +
  • -

#8 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: sorting min and max values in an array

Posted 18 November 2010 - 10:24 PM

View Postgeraldined, on 18 November 2010 - 09:15 PM, said:

Could someone tell me why the min and max are set to Integer.MAX_VALUE and Integer.MIN_VALUE??? What's the point?

Well, unless you know the range of your values, how will you ever know what to set the initial min/max at? 0? What if there's a 1 million min? Zero certainly isnt the min... For practicality and reusability sake, you want to set aside any hardcoding done...setting the values as you said accomplishes that:

The min is never going to be higher than the max integer value and the max is never going to be lower than the min value possible, right?
Alright, so the max integer value is 2.147million something. Now, all this does is allows you to start at a point which you know has to be the extremest case.
So for example...for max:
Start with -2.147million
You see a 10.
Assign 10.
New Max is 10.
Keep repeating...
Guarantees that you will get it right every time versus
Start with 0.
You see nothing but a -10.
Max is still 0 under the IF conditions.
UH OH...:o!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1