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