Make an array with this series (java challenge)

In summary, the Java challenge requires creating an array with a given series and outputting only a summary of the content without any additional information.
  • #1
Arnoldjavs3
191
3

Homework Statement


Given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups).

Homework Equations

The Attempt at a Solution


Java:
public int[] squareUp(int n) {
  int length = n*n;
  int[] completeArry = new int[length];
 
  for(int subArray=1;subArray<n;subArray++) {
    int beginPoint = length- (n*(n-subArray)+1);
    int end = beginPoint+n-1;
    int elements = 1;
    for(int j=beginPoint;j>=end;j--) {
    
      if(elements<=subArray) {
        completeArry[j] = elements;
        elements++;
      }
    
    }
  
  }
  return completeArry;
}

Why is my array just full of zeroes? it doesn't even do anything. All it does it create an array full of zeroes with length n*n.

The idea is that there will be N sub arrays, so i initialize subArray to start at the first sub array.\

The beginning point of the array is where my second nested loop will start and then the endpoint is obviously where it ends. Elements represents the integer that it should add up to(which it will never go beyond the subarray value. So why isn't this algorithm doing anything?
 
Last edited:
Physics news on Phys.org
  • #2
Turns out the line

Code:
end=beginPoint+n-1

should be - n, not + n.
 
  • #3
Code:
for(int j=beginPoint;j>=end;j--)

If end>beginpoint, this loop will be executed 0 times.
 
  • #4
Arnoldjavs3 said:

Homework Statement


Given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups).
It's not at all obvious to me how this pattern would look like for different values of n. Presumably that's not what your question was about.
Arnoldjavs3 said:

Homework Equations

The Attempt at a Solution


Java:
public int[] squareUp(int n) {
  int length = n*n;
  int[] completeArry = new int[length];
 
  for(int subArray=1;subArray<n;subArray++) {
    int beginPoint = length- (n*(n-subArray)+1);
    int end = beginPoint+n-1;
    int elements = 1;
    for(int j=beginPoint;j>=end;j--) {
  
      if(elements<=subArray) {
        completeArry[j] = elements;
        elements++;
      }  
    }
  }
  return completeArry;
}
It's very misleading to have a loop control variable named subArray. That name implies that it would be an array of some kind, not a loop control variable. For this purpose, i would do just fine.

Are you supposed to fill a two dimensional array? If that's the case, then thinking about subarrays makes sense. Here each subarray would be a row of the larger n x n matrix.
It would be helpful if you posted the complete problem description.
Arnoldjavs3 said:
Why is my array just full of zeroes? it doesn't even do anything. All it does it create an array full of zeroes with length n*n.

The idea is that there will be N sub arrays, so i initialize subArray to start at the first sub array.\

The beginning point of the array is where my second nested loop will start and then the endpoint is obviously where it ends. Elements represents the integer that it should add up to(which it will never go beyond the subarray value. So why isn't this algorithm doing anything?
 
  • #5
willem2 said:
Code:
for(int j=beginPoint;j>=end;j--)

If end>beginpoint, this loop will be executed 0 times.

Mark44 said:
It's not at all obvious to me how this pattern would look like for different values of n. Presumably that's not what your question was about.
It's very misleading to have a loop control variable named subArray. That name implies that it would be an array of some kind.

Are you supposed to fill a two dimensional array? If that's the case, then thinking about subarrays makes sense. Here each subarray would be a row of the larger n x n matrix.
It would be helpful if you posted the complete problem description.

That actually is the full problem description. Here's the link for more examples:
http://codingbat.com/prob/p155405

I fixed the issue though. Like the quote above had stated, endpoint was greater than beginpoint so I had to fix that. The idea of 'subarray' was more or less just parts of the array divided equally for every N elements. Kinda like substring.

new code:
Java:
public int[] squareUp(int n) {
  int length = n*n;
  int[] completeArry = new int[length];
 
  for(int subArray=1;subArray<=n;subArray++) {
    int beginPoint = length- (n*(n-subArray)+1);
    int end = beginPoint-n+1;
    int elements = 1;
    for(int j=beginPoint;j>=end;j--) {
    
      if(elements<=subArray) {
        completeArry[j] = elements;
        elements++;
      }
  
    }
 
  }
  return completeArry;
}

I imagine from your perspective it's a lot harder to pick up on how my algorithm works here... But the idea is that it works from right to left starting at the end of the 'subarray' in the initial for loop.
 
  • #6
Arnoldjavs3 said:
I imagine from your perspective it's a lot harder to pick up on how my algorithm works here... But the idea is that it works from right to left starting at the end of the 'subarray' in the initial for loop.
Which is exactly why we use comments. One that would be useful here is to explain that you algorithm fills the array from the end to the beginning. Another would be an explanation that the inner loop starts off where a previous iteration ended.
 

1. What is an array in Java?

An array in Java is a data structure that allows you to store a collection of elements of the same data type in a contiguous memory location. It is a fixed-size container that can hold a specific number of elements.

2. How do I declare an array in Java?

To declare an array in Java, you need to specify the data type of the elements followed by square brackets and the name of the array. For example, to declare an array of integers with a size of 5, you would write: int[] myArray = new int[5];

3. How do I initialize an array in Java?

To initialize an array in Java, you can use either the shorthand or the traditional method. In the shorthand method, you declare and initialize the array in a single line of code. For example: int[] myArray = {1, 2, 3, 4, 5}; In the traditional method, you declare the array and then assign values to each element using a for loop or individually. For example: int[] myArray = new int[5]; for (int i = 0; i < myArray.length; i++) { myArray[i] = i + 1; }

4. How do I access elements in an array in Java?

You can access elements in an array in Java by using their index number. The index starts at 0, so the first element is at index 0, the second element is at index 1, and so on. To access an element, you can use the array name followed by square brackets containing the index. For example: int element = myArray[2]; This will assign the value of the third element in the array to the variable 'element'.

5. How do I add or remove elements from an array in Java?

In Java, arrays have a fixed size, which means you cannot add or remove elements once the array is initialized. However, you can create a new array with a different size and copy the elements from the original array to the new one using a for loop or the System.arraycopy() method. Alternatively, you can use other data structures like ArrayList that allow you to add or remove elements dynamically.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top