13 Replies - 1209 Views - Last Post: 18 February 2013 - 06:18 PM Rate Topic: -----

#1 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Array Operations problem.

Posted 05 February 2013 - 12:17 PM

I am working on his project and I am having trouble understanding the actual questions. Here they are...

ArrayOperations:
This class has no field variables. Donít add any!!
Add a static method that performs a sequential search for a given number and returns the subscript.
Add a static method that performs a sort of the array.
Add a static method that performs a binary search of the sorted array and returns the subscript.
Develop the above methods for both int and double data types. Do not develop methods for float and long data types.
Overload each method with a different array data type.
NOTE: Inside of the above methods do not code the size of the array. The array size must be determined by the demo program.
Use the correct data types!!
ArrayOperationsDemo:
Initialize the arrays in code.
Use 5, 2, 4, 3, 1, 6 as the integer array.
Use 8.0, 1.0, 7.0, 2.0, 6.0, 3.0, 5.0, 4.0 as the double array.
Display your initial array.
Your demo class should use each method in the ArrayOperations class at least once.
For each search method, search for a value that is in the array and a value that is not in the array.
Display the result of each method you use.

This is the basic problem in the book that includes the above.

Write a class name ArrayOperations with the following static methods.

getTotal, accepts 1-Dimentional array as argument and return total of values in array. Overload Int,Double.
getAverage, accepts 1-Dimentional array as argument and returns average of values in array. Overload Int,Double.
getHighest, accepts 1-Dimentional array as argument and returns Highest values in array. Overload Int, Double.
getLowest, accepts 1-Dimentional array as argument and returns Lowest values in array. Overload Int, Double.

^ What should these look like compared to mine.

Demonstrate the class in complete program with test data stored in arras of various data types.

This is the class I am working on so far.


public class ArrayOperations {

	//Sequential Search

	public static int sequentialSearch(int [] array, int value) {
		int index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;

	}
}
	//Sequential Double

	public static double sequentialSearch(double [] array, double value) {
		double index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;

	}
}

	//Sort Selection

	public static void selectionSort(int[] array) {
		int startScan, index, minIndex, minValue;
	for(startScan, = 0; startScan < (aray.length-1); startScann++) {
		minIndex = startScan;
		minValue = array[startScan];
		
		for(index = startScan + 1; index < array.length; index++) {
			if(arra[index] <minValue) {
				minValue = array[index];
				minIndex = index;
				}
			}
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}
}
	//Sort Double

	public static void selectionSort(double[] array) {
		double startScan, index, minIndex, minValue;
	for(startScan, = 0; startScan < (array.length-1); startScan++) {
		minIndex = startScan;
		minValue = array[startScan];
		
		for(index = startScan + 1; index < array.length; index++) {
			if(array[index] <minValue) {
				minValue = array[index];
				minIndex = index;
				}
			}
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}
}

	//Binary Search

	public static int binarySearch(int[] array, int value) {
		int first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(arra[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	return position;
}

	//Binary Double
	public static double binarySearch(double[] array, double value) {
		double first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(arra[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	return position;
}	

public static int getTotal() {
		
	}

public static double getTotal() {

	}

public static int getAverage() { 

	}

public static double getAverage() {

	}

public static int getHighest() {

	}

public static double getHighest() {

	}
	
public static int getLowest() {

	}

public static double getLowest() {

	}



Is This A Good Question/Topic? 0
  • +

Replies To: Array Operations problem.

#2 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 05 February 2013 - 02:07 PM

For the gets, what would I initially be calling to calculate the Total, Average, Highest, and Lowest numbers?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,700
  • Joined: 19-March 11

Re: Array Operations problem.

Posted 05 February 2013 - 02:12 PM

Well, to calculate the total of the elements of an array, you'd want to look up each element of the array and add that value to a running total.

So if you start with zero, and add the value of the first element to that, and then add the value of the next element to that, and so forth, you should be in good shape.

The average is easy if you know the total and the number of elements (which is not hard to get)

As for the max and min - well, you seem to have access to a sort method. Would that help you find the max and min?
Was This Post Helpful? 0
  • +
  • -

#4 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 06 February 2013 - 03:39 PM

Could anyone check this and tell me if in going in the right direction. Also I was wondering about this portion of the assignment.

ArrayOperationsDemo:
Initialize the arrays in code.
Use 5, 2, 4, 3, 1, 6 as the integer array.
Use 8.0, 1.0, 7.0, 2.0, 6.0, 3.0, 5.0, 4.0 as the double array.
Display your initial array.
Your demo class should use each method in the ArrayOperations class at least once.
For each search method, search for a value that is in the array and a value that is not in the array.
Display the result of each method you use.

How do I run the 2 arrays through the methods correctly. Also in the searching in or out of the arrays?

This is what I have.




public class ArrayOperations {

	//Sequential Search

	public static int sequentialSearch(int [] array, int value) {
		int index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;
}

	//Sequential Double

	public static double sequentialSearch(double [] array, double value) {
		int index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;

	}

	//Sort Selection

	public static void selectionSort(int[] array) {
		int startScan, index, minIndex, minValue;
	for(startScan = 0; startScan < (array.length-1); startScan++)
		minIndex = startScan;
		minValue = array[startScan];
		
		for(index = startScan + 1; index < array.length; index++) {
			if(array[index] < minValue) {
				minValue = array[index];
				minIndex = index;
				}
			}
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}

	//Sort Double

	public static void selectionSort(double[] array) {
		int startScan, index, minIndex, minValue;
	for(startScan = 0; startScan < (array.length-1); startScan++) {
		minIndex = startScan;
		minValue = array[startScan];
		
		for(index = startScan + 1; index < array.length; index++) {
			if(array[index] <minValue) {
				minValue = array[index];
				minIndex = index;
				}
			}
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}
}

	//Binary Search

	public static int binarySearch(int[] array, int value) {
		int first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(arra[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	return position;
	}
}
	//Binary Double
	public static double binarySearch(double[] array, double value) {
		double first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(arra[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	return position;
	}	
}
public static int getTotal() {
		int total = 0;
		
		for(int value : units)
		total += value;
	}

public static double getTotal1() {
		double  total = 0;
		
		for(int value : units)
		total += value;
	}

public static int getAverage() {
	double total = 0;
	double average;
	
	for(int index = 0; index < scores.length; index++)
		total += scores[index];
		average = total / scores.length; 
	
}
public static double getAverage1() {
	double total = 0;
	double average;
	
	for(int index = 0; index < scores.length; index++)
		total += scores[index];
		average = total / scores.length; 

}

public static int getHighest() {
	int highest = numbers[0];
	
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] > highest)
		highest = numbers[index];
	}
}

public static double getHighest1() {
	double highest = numbers[0];
	
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] > highest)
		highest = numbers[index];
	}
}

public static int getLowest() {
	int lowest = numbers[0];
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] < lowest)
			lowest = numbers[index];
	}
}

public static double getLowest1() {
	double lowest = numbers[0];
		for(int index = 1; index < numbers.length; index++){
			if(numbers[index] < lowest)
				lowest = numbers[index];
	}
}

	
	}



Was This Post Helpful? 0
  • +
  • -

#5 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 06 February 2013 - 04:01 PM

View Postjon.kiparsky, on 05 February 2013 - 02:12 PM, said:

Well, to calculate the total of the elements of an array, you'd want to look up each element of the array and add that value to a running total.

So if you start with zero, and add the value of the first element to that, and then add the value of the next element to that, and so forth, you should be in good shape.

The average is easy if you know the total and the number of elements (which is not hard to get)

As for the max and min - well, you seem to have access to a sort method. Would that help you find the max and min?



Yes, but it is practicing different techniques of finding it and displaying it.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,700
  • Joined: 19-March 11

Re: Array Operations problem.

Posted 06 February 2013 - 05:33 PM

	public static int getTotal() {
	        int total = 0;
	         
	        for(int value : units)
	        total += value;
	    }
	 



This is the right idea it "units" existed, but it doesn't. It's not a field - you're not allowed to have any of those. It's not a local variable - you can see it's not declared here. So where does it come from? Solve that, and this will return the correct answer.
Was This Post Helpful? 0
  • +
  • -

#7 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 06 February 2013 - 08:15 PM

From the array in my main program where it is declared and called in different methods to display desired information.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,700
  • Joined: 19-March 11

Re: Array Operations problem.

Posted 06 February 2013 - 08:25 PM

But how does the method know about it? Your main class is a foreign country. It's possible to make that object visible from other classes - make it public and use a dot reference - but you don't want to do that.

Think about the methods in java.utils.Arrays - how do those methods know about the array you're want them to work on?
Was This Post Helpful? 0
  • +
  • -

#9 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 07 February 2013 - 05:14 AM

When I create it in the program portion and call it within the methods I've made?
Was This Post Helpful? 0
  • +
  • -

#10 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 07 February 2013 - 07:29 PM

Here is the Demo I've started to maybe clear up some previous posts. I could also use some input on binary searches. I still feel like I'm off somewhere.


public class ArrayDemo {

	public static void main(String[] arg) {
	
		int[] value = {5,2,4,3,1};
		int result;
		double results;
		
			System.out.println("Array Integer");
			for (int index = 0; index < value.length; index++)
			System.out.println(values[index] + " ");
			System.out.println();
			
			ArrayOperations.selectionSort(value);
			
			System.out.println("Sorted Integer");
			for (int index = 0; index < value.length; index++)
			System.out.println(values[index] + " ");
			
			result = ArrayOperations.sequentialSearch(tests, 5);
			
			if(result == -1) {
			System.out.println("5 was not found.");
			}
			else{
			System.out.println("5 was found in" +(result + 1));
			}
	
			
		double[] values = {8.0,1.0,7.0,2.0,6.0,3.0,5.0,4.0};
		
		
			System.out.println("Array Double");
			for (double index = 0; index < values.length; index++)
			System.out.println(values[index] + " ");
			System.out.println();
			
			ArrayOperations.selectionSort1(values);
			
			System.out.println("Sorted Double");
			for (int index = 0; index < values.length; index++)
			System.out.println(values[index] + " ");
			
			results = ArrayOperations.sequentialSearch(tests, 8.0);
			
			if(results == -1) {
			System.out.println("8.0 was not found.");
			}
			else{
			System.out.println("8.0 was found in" +(results + 1));
			}
		}



Was This Post Helpful? 0
  • +
  • -

#11 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 11 February 2013 - 07:26 AM

Would I need to make more variables for the binary search to work properly in my demo?
Was This Post Helpful? 0
  • +
  • -

#12 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 14 February 2013 - 10:06 AM

I think I'm headed in the right direction. I get these 2 errors on my class, and I'm still having trouble with the direction my demo should be going in.


ArrayOperations.java:76 error: possible loss of precision

MinValue = array[value]

Same problem on line 80.


public class ArrayOperations {

	//Sequential Search

	public static int sequentialSearch(int [] array, int value) {
		int index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;
}

	//Sequential Double

	public static double sequentialSearch(double [] array, double value) {
		int index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;

	}

	//Sort Selection

	public static void selectionSort(int[] array, int value) {
		int startScan, index, minIndex, minValue;
	for(startScan = 0; startScan < (array.length-1); startScan++) {
		minIndex = startScan;
		minValue = array[startScan];
		
		for(index = startScan + 1; index < array.length; index++) {
			if(array[index] < minValue) {
				minValue = array[index];
				minIndex = index;
				}
			}
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}
}
	//Sort Double

	public static void selectionSort(double[] array, double value) {
		int startScan, index, minIndex, minValue;
	for(startScan = 0; startScan < (array.length-1); startScan++) {
		minIndex = startScan;
		minValue = array[startScan];
		
		for(index = startScan + 1; index < array.length; index++) {
			if(array[index] <minValue) {
				minValue = array[index];
				minIndex = index;
				}
			}
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}
}

	//Binary Search

	public static int binarySearch(int[] array, int value) {
		int first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(array[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	}
	return position;
}
	//Binary Double
	public static double binarySearch(double[] array, double value) {
		int first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(array[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	}	
	return position;
}
public static int getTotal(int[] units) {
		int total = 0;
		
		for(int value : units)
		total += value;
		return total;
	}

public static double getTotal(double[] units) {
		double  total = 0.0;
		
		for(double value : units)
		total += value;
		return total;
	}

public static int getAverage(int[] scores) {
	int total = 0;
	int average;
	
	for(int index = 0; index < scores.length; index++)
		total += scores[index];
		average = total / scores.length;
			return average;
	
}
public static double getAverage(double[] scores) {
	double total = 0.0;
	double average;
	
	for(int index = 0; index < scores.length; index++)
		total += scores[index];
		average = total / scores.length; 
		return average;
}

public static int getHighest(int[] numbers) {
	int highest = numbers[0];
	
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] > highest)
		highest = numbers[index];
	}
	return highest;
}

public static double getHighest(double[] numbers) {
	double highest = numbers[0];
	
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] > highest)
		highest = numbers[index];
	}
	return highest;
}

public static int getLowest(int[] numbers) {
	int lowest = numbers[0];
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] < lowest)
			lowest = numbers[index];
	}
	return lowest;
}

public static double getLowest(double[] numbers) {
	double lowest = numbers[0];
		for(int index = 1; index < numbers.length; index++){
			if(numbers[index] < lowest)
				lowest = numbers[index];
	}
	return lowest;
}

	
	}



Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Array Operations problem.

Posted 15 February 2013 - 07:17 PM

Your array is an array of double
you store one of the array element int a int

so if the value is 3.14159
you store the value in a min which is a int so will save 3
so you lost the precision of .14159
Was This Post Helpful? 1
  • +
  • -

#14 DtP1337  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 15-October 12

Re: Array Operations problem.

Posted 18 February 2013 - 06:18 PM

Thank you pbl !! I've come a little further with that help. Now I'm recieving an error that one of a my variables may not have been initialized at line 109, all that remains is calling each arry through the methods I made and finish this one up. This is what I have now.


import java.util.Scanner;

public class ArrayDemo {

	public static void main(String[] arg) {
	
		int[] value = {5,2,4,3,1};
		int result;
		int searchValue;
		String again;
		
			System.out.println("Array Integer");
			for (int index = 0; index < value.length; index++)
			System.out.println(value[index] + " ");
			System.out.println();
			
			ArrayOperations.selectionSort(value);
			
			System.out.println("Sorted Integer");
			for (int index = 0; index < value.length; index++)
			System.out.println(value[index] + " ");
			System.out.println();
			
			result = ArrayOperations.sequentialSearch(value, 5);
			
			if(result == -1) {
			System.out.println("5 was not found.");
			}
			else{
			System.out.println("5 was found in " + result);
			}
		System.out.println();
		
		result = ArrayOperations.sequentialSearch(value, 6);
			
			if(result == -1) {
			System.out.println("6 was not found.");
			}
			else{
			System.out.println("6 was found in " + result);
			}
		System.out.println();

		Scanner binary = new Scanner(System.in);

		do{
		System.out.println("Enter a whole number between 1 and 5.");
		searchValue = binary.nextInt();
		
		result = ArrayOperations.binarySearch(value, searchValue);
		
		if (result == -1) {
		System.out.println(searchValue + " was not found");
		}
		else {
		System.out.println(searchValue + " was found in " + result);
		}
		binary.nextLine();
		
		System.out.print("Would you like to search for another number? Y or N ");
		again = binary.nextLine();
		}
		while (again.charAt(0) == 'y' || again.charAt(0) == 'Y');
		

		double[] values = {8.0,1.0,7.0,2.0,6.0,3.0,5.0,4.0};
		double results;
		double searchValue1;
		String again1;
	
			System.out.println("Array Double");
			for (int index = 0; index < values.length; index++)
			System.out.println(values[index] + " ");
			System.out.println();
			
			ArrayOperations.selectionSort(values);
			
			System.out.println("Sorted Double");
			for (int index = 0; index < values.length; index++)
			System.out.println(values[index] + " ");
			
			results = ArrayOperations.sequentialSearch(values, 8.0);
			System.out.println();
			
			if(results == -1) {
			System.out.println("8.0 was not found.");
			}
			else{
			System.out.println("8.0 was found in " +results);
			}
			System.out.println();
			
			results = ArrayOperations.sequentialSearch(values, 9.0);
			
			if(results == -1) {
			System.out.println("9.0 was not found.");
			}
			else{
			System.out.println("9.0 was found in " +results);
			}
			System.out.println();

			Scanner binary1 = new Scanner(System.in);

		do{
		System.out.println("Enter a Decimal number between 1.0 and 8.0.");
		searchValue = binary1.nextInt();
		
		results = ArrayOperations.binarySearch(values, searchValue1);
		
		if (results == -1) {
		System.out.println(searchValue1 + " was not found");
		}
		else {
		System.out.println(searchValue1 + " was found in " + results);
		}
		binary1.nextLine();
		
		System.out.print("Would you like to search for another number? Y or N ");
		again1 = binary1.nextLine();
		}
		while (again1.charAt(0) == 'y' || again1.charAt(0) == 'Y');

				}
			
		}

______________________________________________________________________________________________

public class ArrayOperations {

	//Sequential Search

	public static int sequentialSearch(int [] array, int value) {
		int index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;
}

	//Sequential Double

	public static int sequentialSearch(double [] array, double value) {
		int index,
		start;
		boolean found;

		index = 0;

		start = -1;
		found = false;
	while (!found && index < array.length) {
		
		if (array[index] == value) {
			
			found = true;
			start = index;
		}
	index++;
	}
	return start;

	}

	//Sort Selection

	public static void selectionSort(int[] value) {
		int startScan, index, minIndex, minValue;
	for(startScan = 0; startScan < (value.length-1); startScan++) {
		minIndex = startScan;
		minValue = value[startScan];
		
		for(index = startScan + 1; index < value.length; index++) {
			if(value[index] < minValue) {
				minValue = value[index];
				minIndex = index;
				}
			}
	value[minIndex] = value[startScan];
	value[startScan] = minValue;
	}
}
	//Sort Double

	public static void selectionSort(double[] array) {
		int startScan, index, minIndex, minValue;
	for(startScan = 0; startScan < (array.length-1); startScan++) {
		minIndex = startScan;
		minValue = (int) array[startScan];
		
		for(index = startScan + 1; index < array.length; index++) {
			if(array[index] <minValue) {
				minValue = (int) array[index];
				minIndex = index;
				}
			}
	array[minIndex] = array[startScan];
	array[startScan] = minValue;
	}
}

	//Binary Search

	public static int binarySearch(int[] array, int value) {
		int first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(array[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	}
	return position;
}

	//Binary Double

	public static int binarySearch(double[] array, double value) {
		int first,
		last,
		middle,
		position;
		boolean found;

	first = 0;
	last = array.length -1;
	position = -1;
	found = false;
	
	while(!found && first <= last) {
		middle = (first + last) / 2;
		
		if(array[middle] == value) {
			found = true;
			position = middle;
		}
		
		else if(array[middle] > value)
			last = middle - 1;
		else{
		first = middle + 1;
		}
	}	
	return position;
}

public static int getTotal(int[] units) {
		int total = 0;
		
		for(int value : units)
		total += value;
		return total;
	}

public static double getTotal(double[] units) {
		double  total = 0.0;
		
		for(double value : units)
		total += value;
		return total;
	}

public static int getAverage(int[] scores) {
	int total = 0;
	int average;
	
	for(int index = 0; index < scores.length; index++)
		total += scores[index];
		average = total / scores.length;
			return average;
	
}
public static double getAverage(double[] scores) {
	double total = 0.0;
	double average;
	
	for(int index = 0; index < scores.length; index++)
		total += scores[index];
		average = total / scores.length; 
		return average;
}

public static int getHighest(int[] numbers) {
	int highest = numbers[0];
	
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] > highest)
		highest = numbers[index];
	}
	return highest;
}

public static double getHighest(double[] numbers) {
	double highest = numbers[0];
	
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] > highest)
		highest = numbers[index];
	}
	return highest;
}

public static int getLowest(int[] numbers) {
	int lowest = numbers[0];
	for(int index = 1; index < numbers.length; index++){
		if(numbers[index] < lowest)
			lowest = numbers[index];
	}
	return lowest;
}

public static double getLowest(double[] numbers) {
	double lowest = numbers[0];
		for(int index = 1; index < numbers.length; index++){
			if(numbers[index] < lowest)
				lowest = numbers[index];
	}
	return lowest;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1