Results 1 to 10 of 10
Thread: Help with printing iterations
- 06-02-2011, 01:37 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Help with printing iterations
Ok not sure how to print the iterations for this program. Below is my code and what I have tried so far but im not sure im doing it right. Im supposed to be testing binary vs linear and printing the iterations.
Java Code:import java.io.*; import java.text.DecimalFormat; import java.util.*; class Search { public static int iterations; // seek target in the array of strings. // return the index where found, or -1 if not found. public static int seqSearch( int[] array, int searchItem ) { iterations = 0; for ( int j=0; j < array.length; j++ ){ iterations++; if ( array[j] == searchItem) return j ; // Target found. } return -1 ; // Target not found } public static int binSearch(int [] array, int searchItem) { iterations = 0; int start = 0; int end = array.length-1; int mid; while (start <= end) { iterations++; mid = (start+end)/2; if (searchItem == array[mid]) { return mid; } else if (searchItem < array[mid]) { end = mid-1; } else { start = mid+1; } } return -1; } static Scanner kb = new Scanner(System.in); public static void main(String []args) { int[] numList= new int[20]; numList[0]=5; numList[1]=10; numList[2]=15; numList[3]=20; numList[4]=25; numList[5]=30; numList[6]=35; numList[7]=40; numList[8]=45; numList[9]=50; numList[10]=55; numList[11]=60; numList[12]=65; numList[13]=70; numList[14]=75; numList[15]=80; numList[16]=85; numList[17]=90; numList[18]=95; numList[19]=100; int j= seqSearch(numList, 75); if(j != -1){ System.out.println("found at position" + j); } else { System.out.println("not found"); int j= binSearch(numList,75); if(j != -1){ System.out.println("found at position" + j); } else { System.out.println("not found"); } } } }
- 06-02-2011, 01:39 AM #2
The variable iterations has been declared as static, therefore all methods have access to it. If you want to print the value simply include the variable in a print statement.
- 06-02-2011, 01:44 AM #3
By the way the above can better written.Java Code:int[] numList= new int[20]; numList[0]=5; numList[1]=10; numList[2]=15; numList[3]=20; numList[4]=25; numList[5]=30; numList[6]=35; numList[7]=40; numList[8]=45; numList[9]=50; numList[10]=55; numList[11]=60; numList[12]=65; numList[13]=70; numList[14]=75; numList[15]=80; numList[16]=85; numList[17]=90; numList[18]=95; numList[19]=100;
That way you can configure value and step to work for any values. You could also configure the size of the array.Java Code:int[] numList= {5,10,15,20,25,.......,100}; // or even better int[] numList= new int[20]; int value = 5; int step = 5; for(int index = 0; index <numList.length; index++) { numList[index] = value; value += step; }
- 06-02-2011, 02:01 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Thank you, I am trying to run my new code but keep getting a "could not find the main class: iterations" error and it wont compile.
Here is my new code:
Java Code:import java.io.*; import java.text.DecimalFormat; import java.util.*; class Search { public static int iterations; // seek target in the array of strings. // return the index where found, or -1 if not found. public static int seqSearch( int[] array, int searchItem ) { iterations = 0; for ( int j=0; j < array.length; j++ ){ iterations++; if ( array[j] == searchItem) return j ; // Target found. } return -1 ; // Target not found } public static int binSearch(int [] array, int searchItem) { iterations = 0; int start = 0; int end = array.length-1; int mid; while (start <= end) { iterations++; mid = (start+end)/2; if (searchItem == array[mid]) { return mid; } else if (searchItem < array[mid]) { end = mid-1; } else { start = mid+1; } } return -1; } static Scanner kb = new Scanner(System.in); public static void main(String []args) { int[] numList= new int[20]; int value = 5; int step = 5; for(int index = 0; index <numList.length; index++) { numList[index] = value; value += step; } int x= seqSearch(numList, 75); if(x != -1){ System.out.println("found at position" + x + "iterations" + iterations); } else { System.out.println("not found"); int y= binSearch(numList,75); if(y != -1){ System.out.println("found at position" + y + "with" + iterations+ "iterations"); } else { System.out.println("not found"); } } } }
- 06-02-2011, 02:02 AM #5
You class is called Search not iterations
- 06-02-2011, 02:12 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Thank you! Ok, I fixed the Class name and I ran the program, I get the following; found at position 14, iterations 15. It did not provide my results for my second search.
- 06-02-2011, 02:17 AM #7
I don't know what your code on your computer looks like but the code you posted here lacks consistent indentation.
Can you see the problem?Java Code:if(x != -1){ System.out.println("found at position" + x + "iterations" + iterations); } else { System.out.println("not found"); int y= binSearch(numList,75); if(y != -1){ System.out.println("found at position" + y + "with" + iterations+ "iterations"); } else { System.out.println("not found"); } }
- 06-02-2011, 02:30 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Ok I see, sorry for the sloppy code.
- 06-03-2011, 02:58 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
I am trying to get the average iterations for each of the searches, my program compiles but the correct average is not being displayed. It is showing as zero for all averages. Not sure what im doing wrong here.
Java Code:import java.io.*; import java.text.DecimalFormat; import java.util.*; public class Search { public static int iterations; // seek target in the array of strings. // return the index where found, or -1 if not found. public static int seqSearch( int[] array, int searchItem ) { iterations = 0; for ( int j=0; j < array.length; j++ ){ iterations++; if ( array[j] == searchItem) return j ; // Target found. } return -1 ; // Target not found } public static int binSearch(int [] array, int searchItem) { iterations = 0; int start = 0; int end = array.length-1; int mid; while (start <= end) { iterations++; mid = (start+end)/2; if (searchItem == array[mid]) { return mid; } else if (searchItem < array[mid]) { end = mid-1; } else { start = mid+1; } } return -1; } static Scanner kb = new Scanner(System.in); public static void main(String []args) { int[] numList= new int[20]; int value = 5; int step = 5; int sum= 0; int average; sum = sum + iterations; for(int index = 0; index <numList.length; index++) { numList[index] = value; value += step; { int x= seqSearch(numList, 75); Arrays.sort(numList); if(x != -1) { System.out.println(" Seaquential found at position " + x + " with " + iterations + " iterations "); } else { System.out.println("not found"); } average= sum/x; { System.out.println("Average " + average); } int y= binSearch(numList,75); if(y != -1) { System.out.println(" Binary found at position " + y + " with " + iterations + " iterations "); } else { System.out.println("not found"); } average= sum/y; { System.out.println("Average " + average); } } } } }
- 06-03-2011, 03:58 AM #10
Similar Threads
-
Help in Printing
By kirly in forum Advanced JavaReplies: 3Last Post: 10-03-2011, 03:40 PM -
printing string backwards and printing every other
By droidus in forum New To JavaReplies: 22Last Post: 03-10-2011, 09:17 AM -
Printing
By zzpprk in forum AWT / SwingReplies: 0Last Post: 01-20-2010, 11:25 AM -
Threads don't start after few iterations
By gaurav2211 in forum Threads and SynchronizationReplies: 2Last Post: 12-18-2009, 09:34 AM -
Printing Help...
By chiragkini in forum AWT / SwingReplies: 1Last Post: 02-17-2009, 06:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks