473,320 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Intersection and difference of two arrays will not print

If anyone could let me know how to get my intersection and difference methods to print properly. I have to use iterative loops and methods to complete this assignment so NO in built methods are to be used!Thanks

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class setPractice {
  4.     public static Scanner kbd;
  5.  
  6.     public static final int MAXSIZE = 20;
  7.  
  8.     public static void main(String[] args) {
  9.         kbd = new Scanner(System.in);
  10.         int[] setA = new int[MAXSIZE];
  11.         int[] setB = new int[MAXSIZE];
  12.         int[] intersect = new int[MAXSIZE];
  13.         int[] difference = new int[MAXSIZE];
  14.         int sizeA, sizeB, interSize, diffSize;
  15.  
  16.         System.out.print("How many numbers will be in the 1st set: ");
  17.         sizeA = kbd.nextInt();
  18.         while (sizeA > MAXSIZE) {
  19.             System.out
  20.                     .print("Error: Set size is too large. Re-enter set size: ");
  21.             sizeA = kbd.nextInt();
  22.         }
  23.         System.out.println("Enter list of integers for 1st set: ");
  24.         getData(setA, sizeA);
  25.         sort(setA, sizeA);
  26.         System.out.println("The ascending order for 1st is:");
  27.         print(setA, sizeA);
  28.  
  29.         System.out.print("How many numbers will be in the 2nd set: ");
  30.         sizeB = kbd.nextInt();
  31.         while (sizeB > MAXSIZE) {
  32.             System.out
  33.                     .print("Error: Set size is too large. Re-enter set size: ");
  34.             sizeB = kbd.nextInt();
  35.         }
  36.         System.out.println("Enter list of integers for 2nd set: ");
  37.         getData(setB, sizeB);
  38.         sort(setB, sizeB);
  39.         System.out.println("The ascending order for the 2nd set  is:");
  40.         print(setB, sizeB);
  41.  
  42.         interSize = intersection(setA, setB, sizeA, sizeB, intersect);
  43.         System.out.print("The intersection of the two sets is: ");
  44.         for (int x = 0; x < interSize; x++) {
  45.             System.out.print(intersect[x] + " ");
  46.         }
  47.  
  48.         diffSize = difference(setA, sizeA, setB, sizeB, difference);
  49.         System.out.print("\n\nThe difference of A-B is: ");
  50.         for (int x = 0; x < diffSize; x++) {
  51.             System.out.print(difference[x] + " ");
  52.         }
  53.     }
  54.  
  55.     public static void getData(int[] set, int size) {
  56.  
  57.         for (int x = 0; x < size; x++) {
  58.             int num = kbd.nextInt();
  59.             int count = search(set, size, num);
  60.             if (count == 0)
  61.                 set[x] = num;
  62.             else
  63.                 x--;
  64.         }
  65.     }
  66.  
  67.     public static int search(int[] set, int size, int num) {
  68.  
  69.         int count = 0;
  70.  
  71.         for (int x = 0; x < size; x++) {
  72.             if (num == set[x])
  73.                 count++;
  74.         }
  75.         return count;
  76.     }
  77.  
  78.     public static int difference(int[] setA, int sizeA, int[] setB, int sizeB,
  79.             int[] resultSet) {
  80.             int count = 0;
  81.             boolean flag = true;
  82.             for(int i = 0; i<sizeA ; i++){
  83.                 for(int j = 0; j<sizeB ; j++){
  84.                     if(setA[i] == setB[j]){
  85.                         flag =true;
  86.                         break;
  87.                     }
  88.                     if(flag){
  89.                         resultSet[count++] =setA[i];
  90.                     }
  91.                 }
  92.             }
  93.             return count;
  94.     }
  95.  
  96.     public static void sort(int[] nums, int size) {
  97.         int temp;
  98.         for (int i = 0; i < nums.length - 1; i++) {
  99.             for (int j = 0; j < nums.length - i - 1; j++) {
  100.                 if (nums[j] > nums[j + 1]) {
  101.                     temp = nums[j];
  102.                     nums[j] = nums[j + 1];
  103.                     nums[j + 1] = temp;
  104.  
  105.                 }
  106.             }
  107.         }
  108.  
  109.     }
  110.  
  111.     public static void print(int[] nums, int size) {
  112.         for (int i = 0; i < nums.length; i++) {
  113.             if (nums[i] != 0) {
  114.                 System.out.println(nums[i]);
  115.             }
  116.         }
  117.     }
  118.  
  119.     public static int intersection(int[] setA, int[] setB, int sizeA,
  120.             int sizeB, int[] resultSet) {
  121.         int count = 0;
  122.         for(int i = 0; i <sizeA ; i++){
  123.             for(int j=0; j< sizeB ; j++){
  124.                 if(setA[i] == setB[j]){
  125.                     resultSet[count++]=setA[i];
  126.                     break; 
  127.                 }
  128.             }
  129.         }
  130.         return count;
  131.  
  132.     }
  133. }
  134.  
  135.  
Nov 23 '14 #1
0 887

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Randell D. | last post by:
Folks, A ng poster recently questioned their usage/creation of arrays and their correct syntax. I got the idea to performance test from a recent (excellent) PHP Tutorial article that was in Linux...
3
by: Mickel Grönroos | last post by:
Hi! Are there any standard list methods for getting the intersection and difference of two lists? (The union is easy ("list1.extend(list2)"), unless you want it to contain unique values.) ...
5
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
1
khalidbaloch
by: khalidbaloch | last post by:
hi every one, how are you folf , hope fine dear Friends i want to get values of multi-dimensional arrays using foreach loop and after that print out the html using an other while loop , i tried...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
2
by: surendersharma | last post by:
pls send me answer of above question on my email id .this code should be only in c#.et window base not web base. thank u ...
16
by: lisadean | last post by:
Dear Group I have the database set up, and the query, which lists my name and addresses. Each time I attempt to create a report I fail to create it right. I want a simple layout, a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.