Results 1 to 10 of 10
  1. #1
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default 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");
    
    }
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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;
    By the way the above can better written.
    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;
    }
    That way you can configure value and step to work for any values. You could also configure the size of the array.

  4. #4
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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");
    
    }
    }
    }
    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You class is called Search not iterations

  6. #6
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I don't know what your code on your computer looks like but the code you posted here lacks consistent indentation.
    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");
        }
    }
    Can you see the problem?

  8. #8
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok I see, sorry for the sloppy code.

  9. #9
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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);
    		}
    
    
    
    	}
    }
    }
    }

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 04:40 PM
  2. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 10:17 AM
  3. Printing
    By zzpprk in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2010, 12:25 PM
  4. Threads don't start after few iterations
    By gaurav2211 in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-18-2009, 10:34 AM
  5. Printing Help...
    By chiragkini in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2009, 07:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •