|
Program using one-dimensional array
The program reads 12 integers into an one-dimensional array and sorts them into ascending order using Selection Sort and prints out the sorted data. Next, the Binary Search method is to be used to locate a particular integer or output a warning message if it is not present on the list, otherwise outputting the location of the integers first occurrance (index).
I feel as though I understand this but I am sure I am doing it very wrong. Any hints would be greatly appreciated!
Thank you so much!
My code:
/*This program takes a group of numbers inputted by the user and stores them in an array, sorts them via a selection sort, and searches for a particular value via a Binary Search*/
import java.util.*;
public class LynchA9
{
static Scanner console = new Scanner(System.in);
inputData(int[] intList) //calling method, InputData
{
int[] intList = new int[12]; //declare array of 12
int listLength = 12;
System.out.println("Enter 12 integers."); //user inputs data
for (int i=0; i < listLength; i++)
{
intList[i] = console.nextInt(); //storing pre-sorted data
}
selectionSort(int[] intList, int listLength) //calling method selection sort
{
int i; //initializing variables for selection sort
int smallestIndex ;
int minIndex;
int temp;
for (i = 0; i < listLength - 1; i++) //loop for selection sort
{
smallestIndex = i;
for (minIndex = i + 1; minIndex < listLength; minIndex++)
{
if (intList[minIndex] < intList[smallestIndex])
smallestIndex = minIndex;
temp = intList[smallestIndex];
intList[smallestIndex] = intList[index];
intList[index] = temp;
}
} //end loop
System.out.print(intList[i] + "");
outputData(int[] sortedData) //calling method, OutputData
{
System.out.println("Data after selection Sort:" + sortedData); //output of sorted data
binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
{
System.out.println("Enter the search item:"); //user inputs search item
int first = 0;
int last = listLength -1;
int mid;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) /2;
if (intList[mid] == searchItem)
The errors I am getting:
----jGRASP exec: javac -g LynchA9.java
LynchA9.java:8: invalid method declaration; return type required
inputData(int[] intList) //calling method, InputData
^
LynchA9.java:22: '.class' expected
selectionSort(int[] intList, int listLength) //calling method selection sort
^
LynchA9.java:22: ';' expected
selectionSort(int[] intList, int listLength) //calling method selection sort
^
LynchA9.java:22: ';' expected
selectionSort(int[] intList, int listLength) //calling method selection sort
^
LynchA9.java:44: '.class' expected
outputData(int[] sortedData) //calling method, OutputData
^
LynchA9.java:44: ';' expected
outputData(int[] sortedData) //calling method, OutputData
^
LynchA9.java:48: '.class' expected
binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
^
LynchA9.java:48: ';' expected
binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
^
LynchA9.java:48: <identifier> expected
binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
^
LynchA9.java:48: not a statement
binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
^
LynchA9.java:48: ';' expected
binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
^
LynchA9.java:78: reached end of file while parsing
}//end
^
12 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Last edited by Jdiz; 05-04-2011 at 10:43 AM..
|