Results 1 to 4 of 4
- 10-29-2008, 06:06 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 11
- Rep Power
- 0
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];
}
}
- 10-29-2008, 07:05 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 10-29-2008, 07:16 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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]); } } }
- 10-30-2008, 02:39 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Need another program with and if-else, array, and for loop
By Zebra in forum New To JavaReplies: 2Last Post: 05-05-2008, 01:56 PM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
Array program help
By adelgado0723 in forum New To JavaReplies: 2Last Post: 04-16-2008, 01:19 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
Need help with my 1st array program
By Phobos0001 in forum New To JavaReplies: 5Last Post: 03-22-2008, 06:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks