PDA

View Full Version : assignment due today and can't find what's wrong


lostgoat
07-06-2009, 05:46 PM
// This assignment is due today. Two arrays with random #'s. Then sort each. then merge sorted. When I run the program I get no random #'s. Please help.



import java.util.*;
import java.io.*;

public class PGM2
{
public static void main(String[] args) throws IOException
{
String fname;
Scanner xyz = new Scanner(System.in);
System.out.println("Output filename? ");
fname = xyz.nextLine();
PrintWriter myOutputFile = new PrintWriter(fname);

int nums1 [], nums2 [], nums3 [], sz1, sz2;

System.out.print("What's the size of the first array? ");
sz1 = xyz.nextInt();
nums1 = new int[sz1];
fillArray(nums1);
System.out.println();
System.out.println("\n\nUnsorted Array #1: ");
writeNums(nums1, myOutputFile);
IntBubbleSorter.bubbleSort(nums1);
System.out.println();
System.out.println("\n\nSorted Array #1: ");
System.out.println();
writeNums(nums1, myOutputFile);
System.out.println();
System.out.println("What's the size of the second array? ");
sz2 = xyz.nextInt();
nums2 = new int[sz2];
fillArray(nums2);
System.out.println();
System.out.println("\n\nUnsorted Array #2: ");
writeNums(nums2, myOutputFile);

IntBubbleSorter.bubbleSort(nums2);

System.out.println();
System.out.println("\n\nSorted Array #2: ");
writeNums(nums2, myOutputFile);

nums3 = new int[sz1+sz2];

mergeArrays(nums1, nums2, nums3);

System.out.println();
writeNums(nums3, myOutputFile);

myOutputFile.close();
}
public static void fillArray(int x[])
{
Random rd = new Random(System.currentTimeMillis());
for(int i = 0; i < x.length; i++)
x[i] = rd.nextInt(10000);
}

public static void writeNums(int x[], PrintWriter y)
{
for (int i = 0; i < x.length; i++)
{ y.printf("%8d", x[i]);
if (i%5 == 4)
y.println(); }
}

public static void mergeArrays(int x[], int y[], int z[])
{

IntBubbleSorter.bubbleSort(z);
System.out.println();
System.out.println("\n\nMerged Array: ");
for (int i=0; i < z.length; i++)
{ System.out.printf("%8d", z[i]); if (i%5 == 4) System.out.println();}
}

public static void bubbleSort(int[] array)
{
int maxElement; // Marks the last element to compare
int index; // Index of an element to compare
int temp; // Used to swap to elements

// The outer loop positions maxElement at the last element
// to compare during each pass through the array. Initially
// maxElement is the index of the last element in the array.
// During each iteration, it is decreased by one.
for (maxElement = array.length - 1; maxElement >= 0; maxElement--)
{
// The inner loop steps through the array, comparing
// each element with its neighbor. All of the elements
// from index 0 thrugh maxElement are involved in the
// comparison. If two elements are out of order, they
// are swapped.
for (index = 0; index <= maxElement - 1; index++)
{
// Compare an element with its neighbor.
if (array[index] > array[index + 1])
{
// Swap the two elements.
temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
}
}
}
}

Fou-Lu
07-06-2009, 05:52 PM
Primitive data cannot be passed by reference in Java.
Promote from int[] to Integer[], and that should allow you to populate via reference instead of needing a return result.
I assumed you're issue is with the fillArray method.

BTW, this should happen anywhere you attempt to use a by-reference approach.

lostgoat
07-06-2009, 05:59 PM
It is either with the fillArray method or writeNums. When running no numbers are shown.

Fou-Lu
07-06-2009, 06:06 PM
It will happen anywhere you're using a primitive int[] and trying to alter the values. You need to use an object based class to make use of the object reference in java, so you need to use an Integer[] instead. I expect this behaviour from at least the fillArray, mergeArrays and bubbleSort.
Of course, you can always choose to return an array instead and do what you need to do. I'm about 80% off hand that the array is not stack allocated with the new keyword in java, so you shouldn't lose the value of an array created. This is IMO the superior option.


Y'know what... I'm thinking that even primitive arrays are by reference in java (or at least how java handles them). If this is the case, than you don't need to use an Integer[]. However, that means you'll need to pull out you're debugger and find where the problem lies.