0

Hello guys, i'm having trouble with creating a sequential search algorithm for a two dimensional int array. not sure exactly how to go about augmenting the while loop so it works , the example i'm using does it exactly how i wrote it out, as you can see my compiler is complaining about the incompatibility with the way its written out!

  import java.util.*;
      import javax.swing.*;
      public class pencilneck
  {
    public static void main(String []alex)
  {
  int ROWS = 6;
  int COLS = 3;
  int[][] chargeAcc = new int[ROWS][COLS];
  Scanner keyboard = new Scanner(System.in);

  for(int row = 0; row < ROWS; row++)
     {
        for(int col = 0; col < COLS; col++)
        {
           System.out.print("Enter Account");
           chargeAcc[row][col] = keyboard.nextInt();
        }
     }

     System.out.print("Enter an account to be Charged");
     int input = keyboard.nextInt();
     int results = SequentialSearch(chargeAcc,input);

     if(results ==-1)
     {
        System.out.println("that is an invalid #");
     }
     else
     {
        System.out.println("the # is valid");
     }

}

  public static int SequentialSearch(int[][] array, int value)
  {
     int index1 = 0;
     int element = -1;
     boolean found = false;

        while(!found && index1 == value)
        {
           if(array[index] == value)
           {
              found = true;
              element = index;
           }
           index++;

        }
     return element;
  } 

}

4
  • You wrote that your compiler was complaining about your code but what was the exact message? Commented Jul 19, 2016 at 23:12
  • 1
    You are ... seriously ... asking ... the web community at large ... to debug ... a ... sequential ... search ... algorithm? Really? ... Commented Jul 19, 2016 at 23:16
  • @MikeRobinson to be fair, the community exists so why not? The response or lack thereof will be a lesson in itself. Commented Jul 19, 2016 at 23:17
  • 1
    "Yup!" We all started somewhere, did we not? Commented Jul 20, 2016 at 1:48

2 Answers 2

5

Your question doesn't quite make sense, but the bug in your code is easy enough to find.

while(!found && index1 == value)

This says in plain english, do the stuff in the loop while these conditions are both true:

  1. Found is false
  2. index (index of array) is equal to value (number you want to find in array)

At the start of the loop, index == 0. Since value is likely nonzero, the second condition is false and the loop never runs, causing SequentialSearch to return -1 immediately.

Now that you know what the problem is, I'll leave it to you to take time to understand what you did wrong and figure out how to fix it.

Sign up to request clarification or add additional context in comments.

Comments

0

Array[index] is not an int, is an array.

i think what you are looking for is something like this:

enter code here
import java.util.*;
  import javax.swing.*;
  public class pencilneck
{
public static void main(String []alex)
{
  int ROWS = 6;
  int COLS = 3;
  int[][] chargeAcc = new int[ROWS][COLS];
  Scanner keyboard = new Scanner(System.in);

  for(int row = 0; row < ROWS; row++)
  {
    for(int col = 0; col < COLS; col++)
    {
       System.out.print("Enter Account");
       chargeAcc[row][col] = keyboard.nextInt();
    }
 }

 System.out.print("Enter an account to be Charged");
 int input = keyboard.nextInt();
 int results = SequentialSearch(chargeAcc,input);

 if(results ==-1)
 {
    System.out.println("that is an invalid #");
 }
 else
 {
    System.out.println("the # is valid");
 }

}

public static int SequentialSearch(int[][] array, int value)
{
 int rowIndex = 0;
 int element = -1;
 boolean found = false;

    while(!found && index1 != value)
    {
       for(int i =0;i<array[index].length;i++)
       {
          if(array[index][i] == value)
          {
            found = true;
            element = index;
          }
       }
       index++;
    }
 return element;

}

for each row you search in all of it's cols. (you could break the for loop if found, to gain efficiency.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.