-1

I am having trouble getting the program to run correctly. I've used the debugger and after my program checks the 9 numbers in table 1 it tries to recount table 1 and shoots to false when table 1 does have all numbers 1-9.

Here are the instructions from the assignment:

Write a program that does the following:

Declare a 2 dimensional array of ints that is 3 rows and 3 columns. Fill the array up with numbers between 1 and 9. There is no user input. You can just copy/paste these lines:

int[][]  table1 = {  { 3, 2, 5}, {1,9,6}, {7, 8 4}  };  // this one is true
int[][]  table2 = {  { 3, 3, 5}, {1,9,6}, {3, 8 4}  };  // this one is false
int[][]  table3 = {  { 3, 2, 5}, {1,9,6}, {3, 8 4}  };  // this one is false
int[][]  table4 = {  { 3, 2, 1}, {10,4,6}, {5,7,8}  };  // this one is false
int[][]  table5 = {  { 4, 1, 5}, {1,9,6}, {7, 8 4}  };  // this one is false

Write a static method that will check to see if the array contains all of the numbers 1 through 9. (Each must appear one time only.) Pass the array to the method. The method must return a boolean, true if all the numbers appear in the array, and false otherwise.

calling your method should look like this:

if( allNinePresent(table1)) {
 syso( “All 9 are there in table 1”);
} else {
Syso(“ All 9 are NOT there in table 1 “);
}

============================

Your method header should look like this:

private static boolean allNinePresent(int[][] array){  

========================

There are many strategies for solving this. You can choose whatever one makes most sense for you. Here are a few options.

Write a method that converts the 2d array to a 1d array. That’s easier to work with. Sort the 1d array, then loop through looking for the numbers 1-9.

OR

This is my favorite. Write a method that returns a boolean. It will take a number and a 2d array. static boolean contains(int a, int[][] array) It returns true if ‘a’ is found in the array. You can call that method 9 times, to verify that each number 1 – 9 is in the array.

OR

Make a loop that looks for each of the numbers from 1-9. When one of them is not found, return false. After all are found return true.

OR

Use one of the Collection classes, and the contains() methods to see if they are all there.

Here is my code:

public static void main(String[] args) {

    int[][] table1 = { { 3, 2, 5 }, { 1, 9, 6 }, { 7, 8, 4 } }; // this one
                                                                // is true

    int[][] table2 = { { 3, 3, 5 }, { 1, 9, 6 }, { 3, 8, 4 } }; // this one
                                                                // is false

    int[][] table3 = { { 3, 2, 5 }, { 1, 9, 6 }, { 3, 8, 4 } }; // this one
                                                                // is false

    int[][] table4 = { { 3, 2, 1 }, { 10, 4, 6 }, { 5, 7, 8 } }; // this one
                                                                    // is
                                                                    // false

    int[][] table5 = { { 4, 1, 5 }, { 1, 9, 6 }, { 7, 8, 4 } }; // this one
                                                                // is false
    {

        if (allNinePresent(table1)) {
            System.out.println(" All 9 are there in table 1 ");
        } else {
            System.out.println(" All nine are NOT there in table 1 ");
        }

    }
}

private static boolean allNinePresent(int[][] table1) {

    int[] oneDArray = new int[9];

    for (int r = 0; r < table1.length; r++) {
        for (int c = 0; c < table1[r].length; c++) {
            if (table1[r][c] == oneDArray[c]) {
                return true;
            }
        }
    }
    return false;
}

Any help would be much appreciated.

  • 7
    Do you seriously believe that by asking us to make your homework one day you'll be able to write your own code? – Gavriel Jan 23 '16 at 22:24
  • 1
    Which one of the above strategies does your code implement? – Gavriel Jan 23 '16 at 22:26
  • 1
    remember that int[] oneDArray = new int[9]; initialize the array to all 0. moreover, considering what i said, you return true as soon as table1 contains a 0. This is my hint, but i won't do your homework – Tommaso Pasini Jan 23 '16 at 22:40
  • Not sure why the lecturer's "favourite" is his favourite; it certainly wouldn't be mine. Why would you deliberately select an O(n²) algorithm when an O(n) algorithm exists with code that is just as clear? – Klitos Kyriacou Jan 23 '16 at 23:26
  • http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value – Dave S Sep 09 '16 at 19:23
  • This posting is plagiarism as well as copyright violation. The poster does not acknowledge the source of the text. I have already failed 5 students who copied from this posting into their assignments. It it your responsibility, as the moderator, to remove posts that are plagiarized, and in copyright violation. – DrA Sep 11 '16 at 13:03
  • 2
    @DrA: please file a DCMA takedown request; see the [*Reporting Copyright Infringements* section of the *legal* section](http://stackexchange.com/legal). Moderators are not in a position to verify copyright infringement claims. – Martijn Pieters Sep 11 '16 at 13:33
  • No. It's hopeless. 1000 students posting 40-60 different assignments each semester. Fill out a report for each one? If the students want to cheat, they will cheat.... there are lots of places that will help them including StackOverflow. It is no longer possible to give assignments that have points. I am moving to closed-book proctored final exams. – DrA Sep 12 '16 at 11:43

3 Answers3

1

Here are two hints. One is from the comments.

"remember that int[] oneDArray = new int[9]; initialize the array to all 0. moreover, considering what i said, you return true as soon as table1 contains a 0. This is my hint, but i won't do your homework – Tommaso Pasini"

Second hint is to make sure you have read this thoroughly:

"This is my favorite. Write a method that returns a boolean. It will take a number and a 2d array. static boolean contains(int a, int[][] array) It returns true if ‘a’ is found in the array. You can call that method 9 times, to verify that each number 1 – 9 is in the array."

You can look into using a tutor as well if you need more help :)

z7r1k3
  • 718
  • 1
  • 5
  • 20
0

Since I don't want to hand the complete solution, here's my strategy advice:

Use an array of 9 booleans, which tells if you already found each of the numbers.

(E.g: found[0] == true tells you already found number 1)

Loop through your table and check if each number is between 1 - 9 and not already found, using and updating your array.

Bifz
  • 403
  • 2
  • 9
-3

You could use recursion to check every value.

private static boolean allNinePresent(int[][] table) {
    int size = 0; //used to initialize the size of the array
    for (int[] i : table) {
        size = size + i.length; //calculate the total size of the 2d array
    }

    if (size == 9) { //check that the array contains exactly 9 elements
        return recursiveCheck(table, 1); 
    }
    else {
        return false;
    }
}

/**
 * Recursively check the int array for the control number.
 * @param test - The array that needs to be tested.
 * @param control - The control number to be tested against.
 * @return the result of the recursive check.
 */
private static boolean recursiveCheck(int[][] value, int control) {
    for (int[] iArr : value) { //Extract every int array in the 2d array
        for (int i : iArr) { //Test every digit in the int array
            if (i == control) {
                if (control != 9) { //recursive check if control haven't reached 9
                    return recursiveCheck(value, control + 1);
                }
                else {
                    return true;
                }
            }
        }
    }
    return false; //this will only be reached if all values in the int array  
                  // is tested false
}
Simon Jensen
  • 488
  • 1
  • 3
  • 19