# Help with an Array program please?

• 10-29-2008, 07:06 AM
EternalSolitude
Help with an Array program please?
I have to write this Array program a long certain specifications but I am currently stuck, if someone can help that would be great. What I have so far is posted below. What Still need to do is:
-Write a function called selsort that takes an integer array, which is guaranteed to have only positive integers, and sorts it in descending order.
-Inside selsort, create a new array. Call getMaxIndex repeatedly, and insert the max element into the new array. Set that element in the original array to such a number so that it does not come up as a max number anymore.

- and some other stuff with BagOfFruit, but I can easily finish that myself, i just need help with the above two instructions

If you haven't noticed I am no good with Arrays:o
Sorry

Quote:

class BagOfFruit
{
public int numberOfApples;
public int numberOfOranges;
}

public class Lab9
{
public static int[] getFibonacci(int Size)
{
int[] arr = new int[Size];
arr[0] = 1;
arr[1] = 1;
for(int i = 2; i < Size; i++)
{
arr[i] = arr[i-1] + arr[i - 2];
}
return arr;

}

public static int getMaxIndex(int[] arr)
{
int maxIndex = 0;
for(int i = 1; i < arr.length; i++)
{
if ( arr[i] > arr[maxIndex] ) maxIndex = i;
}
return maxIndex;
}

public static int selsort(int[] arr)
{

}

public static void main(String[] args)
{
int[] a = getFibonacci(10);
System.out.println(java.util.Arrays.toString(a));

int[] BagOfFruit = new int[3];

}
}
• 10-29-2008, 08:05 AM
Eranga
If you are not good with Java arrays, I think it's better to refer some materials before do this assignment.

```public class WorkingWithArrays {     public static void main(String[] args) {         int[] arr = new int[10]; // Initialization and declaration of an array                 // Adding elements(populating)         arr[0] = 2;         arr[1] = 4;         arr[2] = 6;         arr[3] = 1;         arr[4] = 0;         arr[5] = 9;         arr[6] = 7;         arr[7] = 8;         arr[8] = 3;         arr[9] = 9;                 // Check for negative values         for(int i = 0; i < arr.length; i++) {             if(arr[i] < 0) {                 System.out.println("Negative value is found. " + arr[i]);                 System.exit(0);             }                        }                 // Call the selsort method         new WorkingWithArrays().selsort(arr);     }         private void selsort(int[] arr) {                 java.util.Arrays.sort(arr); // Sort the array using Java API                 // Display the result to check         for(int i = 0; i < arr.length; i++) {             System.out.println(arr[i]);         }             } }```