java - Program won't print out the intersection or difference! Any suggestions? -
import java.util.scanner; public class setpractice { public static scanner kbd; public static final int maxsize = 20; public static void main(string[] args) { kbd = new scanner(system.in); int[] seta = new int[maxsize]; int[] setb = new int[maxsize]; int[] intersect = new int[maxsize]; int[] difference = new int[maxsize]; int sizea, sizeb, intersize, diffsize; system.out.print("how many numbers in 1st set: "); sizea = kbd.nextint(); while (sizea > maxsize) { system.out .print("error: set size large. re-enter set size: "); sizea = kbd.nextint(); } system.out.println("enter list of integers 1st set: "); getdata(seta, sizea); sort(seta, sizea); system.out.println("the ascending order 1st is:"); print(seta, sizea); system.out.print("how many numbers in 2nd set: "); sizeb = kbd.nextint(); while (sizeb > maxsize) { system.out .print("error: set size large. re-enter set size: "); sizeb = kbd.nextint(); } system.out.println("enter list of integers 2nd set: "); getdata(setb, sizeb); sort(setb, sizeb); system.out.println("the ascending order 2nd set is:"); print(setb, sizeb); intersize = intersection(seta, setb, sizea, sizeb, intersect); system.out.print("the intersection of 2 sets is: "); (int x = 0; x < intersize; x++) { system.out.print(intersect[x] + " "); } diffsize = difference(seta, sizea, setb, sizeb, intersect); system.out.print("\n\nthe difference of a-b is: "); (int x = 0; x < diffsize; x++) { system.out.print(difference[x] + " "); } } public static void getdata(int[] set, int size) { (int x = 0; x < size; x++) { int num = kbd.nextint(); int count = search(set, size, num); if (count == 0) set[x] = num; else x--; } } public static int search(int[] set, int size, int num) { int count = 0; (int x = 0; x < size; x++) { if (num == set[x]) count++; } return count; } public static int difference(int[] seta, int sizea, int[] setb, int sizeb, int[] resultset) { int y = 0; (int x = 0; x < sizea; x++) { int num = seta[x]; int found = search(setb, sizeb, num); if (found == 0) { resultset[y] = num; y++; } } return y; } public static void sort(int[] nums, int size) { int temp; (int = 0; < nums.length - 1; i++) { (int j = 0; j < nums.length - - 1; j++) { if (nums[j] > nums[j + 1]) { temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } } public static void print(int[] nums, int size) { (int = 0; < nums.length; i++) { if (nums[i] != 0) { system.out.println(nums[i]); } } } public static int intersection(int[] seta, int[] setb, int size, int sizeb, int[] resultset) { int count = 0; (int = 0; < seta.length; i++) { (int j = 0; j < setb.length; j++) { if (seta[i] == setb[j]) { count++; break; } } } resultset = new int[count]; count = 0; (int = 0; < seta.length; i++) { (int j = 0; j < setb.length; j++) { if (seta[i] == setb[j]) { resultset[count++] = seta[i]; break; } } } return count; } }
the requirements must use methods , loops reach solution. intersection , difference methods must return int part of assignment instructions!
thank in advance!
test input:
how many numbers in 1st set: 3 enter list of integers 1st set: 34 2 56 ascending order 1st is: 2 34 56 how many numbers in 2nd set: 4 enter list of integers 2nd set: 56 2 33 6 ascending order 2nd set is: 2 6 33 56 intersection of 2 sets is: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 difference of a-b is:
i think found errors:
you use
set.length
(orseta.length
or ...) instead of method parameter (size
orsizea
or ...) multiple times.especially in
sort
method problematic: sorted arrays start0
s. don't recognize this, since ignore0
s inprint
method. print way print intersection , difference , you'll see error.you pass
intersect
parameterdifference
method instead ofdifference
.you create new array in
intersection
method. replaces local array, not 1 created inmain
method. (int
arrays initialized0
s)
Comments
Post a Comment