- #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: