Please help! The purpose of this exercise is to practice with arrays and static
ID: 3701962 • Letter: P
Question
Please help! The purpose of this exercise is to practice with arrays and static methods in Java
/* returns the smallest item found in A */
public static int smallest(int A[]) {
return 0;
}
/* returns the index of the smallest item found in A */
public static int indexOfSmallest(int A[]) {
return 0;
}
/* returns the index of the smallest odd number
* in A[] or -1 if A[] contains no odd numbers */
public static int indexOfSmallestOdd(int A[]) {
return -1;
}
/* removes the element in A[] at the given index,
* by shifting all remaining elements to the left
* if necessary and filling in the right-hand
* side with 0s.
*
* returns the element removed.
*
* For example, if we call remove(A, 1)
* on the array:
*
* |---+---+----+----+-----+-----+-----+---+---+---|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
* |---+---+----+----+-----+-----+-----+---+---+---|
* | 5 | 9 | 32 | 97 | 101 | 142 | 157 | 0 | 0 | 0 |
* |---+---+----+----+-----+-----+-----+---+---+---|
*
* the array becomes:
*
* |---+----+----+-----+-----+-----+---+---+---+---|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
* |---+----+----+-----+-----+-----+---+---+---+---|
* | 5 | 32 | 97 | 101 | 142 | 157 | 0 | 0 | 0 | 0 |
* |---+----+----+-----+-----+-----+---+---+---+---|
*
*
* and we return the value 9.
*
* It is up to the caller to make sure that the
* index passed to the function is valid.
* */
public static int remove(int A[], int index) {
return 0;
}
public static int[] chunk(int A[], int s, int e) {
return null;
}
/* returns a new array consisting of the same elements in
* A[] repeated numTimes
*
* for example, if A[] refers to the array:
*
* |----+----+----|
* | 0 | 1 | 2 |
* |----+----+----|
* | 11 | 22 | 33 |
* |----+----+----|
*
* and numTimes is 3, the function returns:
*
* |----+----+----+----+----+----+----+----+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* |----+----+----+----+----+----+----+----+----|
* | 11 | 11 | 11 | 22 | 22 | 22 | 33 | 33 | 33 |
* |----+----+----+----+----+----+----+----+----|
*
*
* if numTimes <=1, the method returns a copy of A[]
* */
public static int[] repeat(int A[], int numTimes) {
return null;
}
/* reverses the ordering of the elements of each
* row of the two-dimensionl array to which A refers.
*
* For example, if A refers to the array:
*
*
* |----+----+----|
* | 10 | 20 | 30 |
* |----+----+----+----|
* | 40 | 50 | 60 | 70 |
* |----+----+----+----|
* | 80 | 90 |
* |----+----+
*
*
* the method returns:
*
* |----+----+----+
* | 30 | 20 | 10 |
* |----+----+----+----|
* | 70 | 60 | 50 | 40 |
* |----+----+----+----|
* | 90 | 80 |
* |----+----+
*
* */
public static void reverseAll(int A[][]) {
}
/* scores contains a collection of quiz grades ranging
* from 0 to maxScore inclusive. The method returns
* a new array where the value at index i is the number
* of times that grade i appeared in scores.
*
* For example, if scores refers to the array:
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
* | 9 | 3 | 8 | 10 | 7 | 9 | 8 | 9 | 7 | 6 | 8 | 7 | 8 | 9 | 6 |
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
*
* The method returns the array:
* |---+---+---+---+---+---+---+---+---+---+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
* |---+---+---+---+---+---+---+---+---+---+----|
* | 0 | 0 | 0 | 1 | 0 | 0 | 2 | 3 | 4 | 4 | 1 |
* |---+---+---+---+---+---+---+---+---+---+----|
*
* */
public static int[] counts(int scores[], int maxScore) {
return null;
}
/* Extra credit:
*
* Returns a new array consisting of all of the
* elements of A, but with no duplicates. For example,
* if A[] is {10,20,5,32,5,10,9,32,8}, the method returns
* the array {10,20,5,32,9,8} */
public static int[] uniques(int A[]) {
return null;
}
}
Explanation / Answer
Hi friend, Question is long.
Except count and unique, i have answered all.
public class ArrayPractice{
/* returns the smallest item found in A */
public static int smallest(int A[]) {
int min = A[0];
for(int i=1; i<A.length; i++)
if(min > A[i])
min = A[i];
return min;
}
/* returns the index of the smallest item found in A */
public static int indexOfSmallest(int A[]) {
int minIndex = 0;
for(int i=1; i<A.length; i++)
if(A[minIndex] > A[i])
minIndex = i;
return minIndex;
}
/* returns the index of the smallest odd number
* in A[] or -1 if A[] contains no odd numbers */
public static int indexOfSmallestOdd(int A[]) {
int minOddIndex = -1;
int i=0;
// finding first odd
while(i < A.length) {
if(A[i]%2 == 1) {
minOddIndex = i;
break;
}
}
while(i < A.length) {
if(A[i]%2 == 1 && A[i] < A[minOddIndex]) {
minOddIndex = i;
}
}
return minOddIndex;
}
/* removes the element in A[] at the given index,
* by shifting all remaining elements to the left
* if necessary and filling in the right-hand
* side with 0s.
*
* returns the element removed.
*
* For example, if we call remove(A, 1)
* on the array:
*
* |---+---+----+----+-----+-----+-----+---+---+---|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
* |---+---+----+----+-----+-----+-----+---+---+---|
* | 5 | 9 | 32 | 97 | 101 | 142 | 157 | 0 | 0 | 0 |
* |---+---+----+----+-----+-----+-----+---+---+---|
*
* the array becomes:
*
* |---+----+----+-----+-----+-----+---+---+---+---|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
* |---+----+----+-----+-----+-----+---+---+---+---|
* | 5 | 32 | 97 | 101 | 142 | 157 | 0 | 0 | 0 | 0 |
* |---+----+----+-----+-----+-----+---+---+---+---|
*
*
* and we return the value 9.
*
* It is up to the caller to make sure that the
* index passed to the function is valid.
* */
public static int remove(int a[], int index) {
int item = a[index];
// creating new array
for(int i=index; i<a.length-1; i++)
a[i] = a[i+1];
a[a.length-1] = 0;
return item;
}
public static int[] chunk(int a[], int s, int e) {
if(s >= e)
return null;
int[] b = new int[e-s];
int k = 0;
for(int i=s; i<e; i++)
b[k++] = a[i];
return b;
}
/* returns a new array consisting of the same elements in
* A[] repeated numTimes
*
* for example, if A[] refers to the array:
*
* |----+----+----|
* | 0 | 1 | 2 |
* |----+----+----|
* | 11 | 22 | 33 |
* |----+----+----|
*
* and numTimes is 3, the function returns:
*
* |----+----+----+----+----+----+----+----+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* |----+----+----+----+----+----+----+----+----|
* | 11 | 11 | 11 | 22 | 22 | 22 | 33 | 33 | 33 |
* |----+----+----+----+----+----+----+----+----|
*
*
* if numTimes <=1, the method returns a copy of A[]
* */
public static int[] repeat(int a[], int numTimes) {
int[] b = new int[a.length*numTimes];
int k = 0;
for(int i=0; i<a.length; i++){
b[k++] = a[i];
b[k++] = a[i];
b[k++] = a[i];
}
return b;
}
/* reverses the ordering of the elements of each
* row of the two-dimensionl array to which A refers.
*
* For example, if A refers to the array:
*
*
* |----+----+----|
* | 10 | 20 | 30 |
* |----+----+----+----|
* | 40 | 50 | 60 | 70 |
* |----+----+----+----|
* | 80 | 90 |
* |----+----+
*
*
* the method returns:
*
* |----+----+----+
* | 30 | 20 | 10 |
* |----+----+----+----|
* | 70 | 60 | 50 | 40 |
* |----+----+----+----|
* | 90 | 80 |
* |----+----+
*
* */
public static void reverseAll(int a[][]) {
for(int i=0; i<a.length; i++) {
int j = 0, k = a[i].length-1;
while(j < k) {
int t = a[i][j];
a[i][j] =a[i][k];
a[i][k] = t;
}
}
}
/* scores contains a collection of quiz grades ranging
* from 0 to maxScore inclusive. The method returns
* a new array where the value at index i is the number
* of times that grade i appeared in scores.
*
* For example, if scores refers to the array:
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
* | 9 | 3 | 8 | 10 | 7 | 9 | 8 | 9 | 7 | 6 | 8 | 7 | 8 | 9 | 6 |
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
*
* The method returns the array:
* |---+---+---+---+---+---+---+---+---+---+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
* |---+---+---+---+---+---+---+---+---+---+----|
* | 0 | 0 | 0 | 1 | 0 | 0 | 2 | 3 | 4 | 4 | 1 |
* |---+---+---+---+---+---+---+---+---+---+----|
*
* */
public static int[] counts(int scores[], int maxScore) {
return null;
}
/* Extra credit:
*
* Returns a new array consisting of all of the
* elements of A, but with no duplicates. For example,
* if A[] is {10,20,5,32,5,10,9,32,8}, the method returns
* the array {10,20,5,32,9,8} */
public static int[] uniques(int A[]) {
return null;
}
public static int[] copyAll(int[] a, int []b) {
int arr[] = new int[a.length+b.length];
int k = 0;
for(int i=0; i<a.length; i++)
arr[k++] = a[i];
for(int i=0; i<b.length; i++)
arr[k++] = b[i];
return arr;
}
}