user6681362
user6681362

Reputation:

Method doesn't work

For my assignment, I have to use methods to find the number of patterns in an array. The pattern is counted when the sum of adjacent numbers in an array is more than 7.

I have to use 2 methods, 1 being insertNumbers to create an array and another being computePattern to count the patterns.

However, the pattern printed out doesn't match the array printed out. Here is the code.

As this is an assignment, I would rather not get answers but answers on which part of my code is wrong, and how do I fix it.

EDIT: Here is a sample output.

Sample output #1: Array: 2 7 2 3 1 5 7 4 3 6

Number of patterns: 3

public static int[] insertNumbers()
{
    //Declaring the array.
    int randomArray[] = new int[10];

    //Setting random numbers into the array.
    for (int k = 0;k < randomArray.length;k++)
    {
        int i = (int)((Math.random()*9)+1);
        randomArray[k] = i;
    }

    //Returning array into other methods.
    return randomArray;
}

public static int computePattern()
{
    int a = 0;
    int b = 1;
    int pattern = 0;
    int[] randomArray = insertNumbers();
    //Computing the number of patterns.
    for (;a<=8 && b<=9;)
    {
        if (randomArray[a] + randomArray[b]>7)
        {
            pattern++;            
        }      
        a+=2;
        b+=2;
    }
    return pattern;
}

public static void main(String[] args) 
{
    int pattern = computePattern();
    int[] randomArray = insertNumbers();

    //Printing out the contents of the array.
    System.out.print("Array : " );
    for(int i = 0; i < 10; i++)
    {
        System.out.print(+randomArray[i] +" ");
    }

    System.out.println(" ");

    //Printing out the number of patterns.
    System.out.println("Number of patterns: "+pattern);
}

Upvotes: 0

Views: 89

Answers (3)

Shunan
Shunan

Reputation: 3254

You should do the compute pattern part this way

{

    int a;
    int pattern = 0;
    int[] randomArray = insertNumbers();
    //Computing the number of patterns.
    for (a=1;a<9;a++)
    {
        if (randomArray[a] + randomArray[a-1]>7)
        {
            pattern++;            
        }      

    }
    return pattern;
}

And you definitely calling insertNumbers Twice. You should call it once in main and send that array to computePattern.

int[] randomArray = insertNumbers();
int pattern = computePattern(randomArray);

Upvotes: 0

FallAndLearn
FallAndLearn

Reputation: 4135

Remove this line from your main function

int[] randomArray = insertNumbers();

You are calling the function insertNumbers again after computing the pattern.

Upvotes: 0

Giorgi Moniava
Giorgi Moniava

Reputation: 28654

You are computing pattern for a different array, and in main you are printing different array (you are calling insertNumbers twice basically). See here:

    int pattern = computePattern(); // First time computePatter generates one array
    int[] randomArray = insertNumbers(); // Another array is generated here

Also, doesn't seem your pattern counting is correct. Hint: does it compare elements with indexes 1 and 2?

Upvotes: 2

Related Questions