Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default 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



    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];

    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If you are not good with Java arrays, I think it's better to refer some materials before do this assignment.

    Here is a link.

    Follows all explanations and examples on that above page. It's really helpful to you.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is a simple example for the first part.

    Java Code:
    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]);
            }
            
        }
    
    }

  4. #4
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 05-05-2008, 01:56 PM
  2. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  3. Array program help
    By adelgado0723 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Need help with my 1st array program
    By Phobos0001 in forum New To Java
    Replies: 5
    Last Post: 03-22-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •