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 -
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 will be in the 1st set: ");
-
sizeA = kbd.nextInt();
-
while (sizeA > MAXSIZE) {
-
System.out
-
.print("Error: Set size is too large. Re-enter set size: ");
-
sizeA = kbd.nextInt();
-
}
-
System.out.println("Enter list of integers for 1st set: ");
-
getData(setA, sizeA);
-
sort(setA, sizeA);
-
System.out.println("The ascending order for 1st is:");
-
print(setA, sizeA);
-
-
System.out.print("How many numbers will be in the 2nd set: ");
-
sizeB = kbd.nextInt();
-
while (sizeB > MAXSIZE) {
-
System.out
-
.print("Error: Set size is too large. Re-enter set size: ");
-
sizeB = kbd.nextInt();
-
}
-
System.out.println("Enter list of integers for 2nd set: ");
-
getData(setB, sizeB);
-
sort(setB, sizeB);
-
System.out.println("The ascending order for the 2nd set is:");
-
print(setB, sizeB);
-
-
interSize = intersection(setA, setB, sizeA, sizeB, intersect);
-
System.out.print("The intersection of the two sets is: ");
-
for (int x = 0; x < interSize; x++) {
-
System.out.print(intersect[x] + " ");
-
}
-
-
diffSize = difference(setA, sizeA, setB, sizeB, difference);
-
System.out.print("\n\nThe difference of A-B is: ");
-
for (int x = 0; x < diffSize; x++) {
-
System.out.print(difference[x] + " ");
-
}
-
}
-
-
public static void getData(int[] set, int size) {
-
-
for (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;
-
-
for (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 count = 0;
-
boolean flag = true;
-
for(int i = 0; i<sizeA ; i++){
-
for(int j = 0; j<sizeB ; j++){
-
if(setA[i] == setB[j]){
-
flag =true;
-
break;
-
}
-
if(flag){
-
resultSet[count++] =setA[i];
-
}
-
}
-
}
-
return count;
-
}
-
-
public static void sort(int[] nums, int size) {
-
int temp;
-
for (int i = 0; i < nums.length - 1; i++) {
-
for (int j = 0; j < nums.length - i - 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) {
-
for (int i = 0; i < nums.length; i++) {
-
if (nums[i] != 0) {
-
System.out.println(nums[i]);
-
}
-
}
-
}
-
-
public static int intersection(int[] setA, int[] setB, int sizeA,
-
int sizeB, int[] resultSet) {
-
int count = 0;
-
for(int i = 0; i <sizeA ; i++){
-
for(int j=0; j< sizeB ; j++){
-
if(setA[i] == setB[j]){
-
resultSet[count++]=setA[i];
-
break;
-
}
-
}
-
}
-
return count;
-
-
}
-
}
-
-
0 887 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.)
...
|
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...
|
by: Vasileios Zografos |
last post by:
Hello,
can anyone please tell me if there is any difference between the two:
double Array1;
and
|
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...
|
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...
|
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
|
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
...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
| |