• December 7th, 2012, 07:14 AM
d4divya2005
Problem statement: Write a program in java, to compute the union of two large sets.(1 million in each set)

Details: Two sets, A and B, will be provided in the form of two text files 'A.txt' and 'B.txt'. Write a program to read these files into memory, and compute their union. The union of these sets should be written to another file, 'union.txt'. It may be noted that the input sets, A and B, are very large sets containing over a million elements each. Enough thought should be put into the choice of data structures for this program, as it will have a bearing on the efficiency of the program. Also, the design of the program should be well thought out--for example, minimizing the number of passes for reading the input, minimizing the number of times a particular value is computed, etc.

Deliverables:
* the code
* total execution time (measured in terms of CPU time, and not wallclock time)
* a short design document explaining: (a) the algorithm, and (b) the main data structures, along with a justification for their choice.
• December 7th, 2012, 07:23 AM
curmudgeon
• December 7th, 2012, 08:29 AM
d4divya2005
import java.util.*;
import java.io.*;
class assignment{
public static void main(String args[])
throws Exception{
Scanner src= new Scanner(f1);
int a[];
a=new int[1000000];
int i,j;
j=0;
while(src.hasNext())
{if(src.hasNextInt()){
i=src.nextInt();
a[j]=i;

j++;
}

}
f1.close();
Scanner src1= new Scanner(f2);
int b[];
b=new int[1000000];
int k,l;
l=0;
while(src1.hasNext())
{if(src1.hasNextInt()){
k=src1.nextInt();
b[l]=k;
l++;
}

}

f2.close();
int n=a.length+b.length;
int c[]=new int[10];
int p=a.length;
for(i=0;i<a.length;i++)
{
c[i]=a[i];
}
for(i=0;i<b.length;i++)
{
for(j=0;j<a.length;j++)
{
if(b[i]!=c[j])
{continue;}
else{if(b[i]==c[j])break;}}
if(j==10)
c[p++]=b[i];
}
for(i=0;i<n;i++)
{System.out.println(c[i]);}}}
• December 7th, 2012, 09:16 AM
Norm
[code=java]
[/code]
to get highlighting and preserve formatting.

Also provide the questions about the problems you are having.
• December 7th, 2012, 09:25 AM
d4divya2005
Code java:

import java.util.*;
import java.io.*;
class assignment{
public static void main(String args[])
throws Exception{
Scanner src= new Scanner(f1);
int a[];
a=new int[1000000];
int i,j;
j=0;
while(src.hasNext())
{if(src.hasNextInt()){
i=src.nextInt();
a[j]=i;

j++;
}

}
f1.close();
Scanner src1= new Scanner(f2);
int b[];
b=new int[1000000];
int k,l;
l=0;
while(src1.hasNext())
{if(src1.hasNextInt()){
k=src1.nextInt();
b[l]=k;
l++;
}

}

f2.close();
int n=a.length+b.length;
int c[]=new int[10];
int p=a.length;
for(i=0;i<a.length;i++)
{
c[i]=a[i];
}
for(i=0;i<b.length;i++)
{
for(j=0;j<a.length;j++)
{
if(b[i]!=c[j])
{continue;}
else{if(b[i]==c[j])break;}}
if(j==10)
c[p++]=b[i];
}
for(i=0;i<n;i++)
{System.out.println(c[i]);}}}

--- Update ---

the problem is.. its not printing the union of two arrays.. some problem with the union algorithm for two arrays
• December 7th, 2012, 09:33 AM
Norm
Please edit the post and format the code. Statements should be indented when inside of {}s to show logic levels. All statements should NOT start in the first column.

Quote:

. its not printing the union of two arrays
Please post the program's output, explain what is wrong with it and show what it should be.

Also you need to post the contents of the input files used for testing.
• December 7th, 2012, 09:43 AM
d4divya2005
two files contain million integers.. m not getting the output
• December 7th, 2012, 09:57 AM
Norm
What does the program output?

Have you tested the code with smaller inputs? Did it work?

Please edit the post and format the code. Statements should be indented when inside of {}s to show logic levels. All statements should NOT start in the first column.
• December 7th, 2012, 10:03 AM
d4divya2005
m sorry... i started java just 2 days back.. m totally new...
i tried with smaller inouts..but not wrking..i just need a program from union of two arrays...i feel my logic is wrong for the union..
• December 7th, 2012, 10:07 AM
Norm
Quote:

.i feel my logic is wrong for the union..
Its better to start the testing with smaller files before trying with large files.
Did the program work with small files? If so did you try increasing the size of the files to find the smallest size that the program fails with?

You need to do the following, no one wants to work with unformatted code:

Please edit the post and format the code. Statements should be indented when inside of {}s to show logic levels. All statements should NOT start in the first column.
• December 7th, 2012, 10:20 AM
d4divya2005
Code java:

int n=a.length+b.length;

int c[]=new int[10];

int p=a.length;

for(i=0;i<a.length;i++)
{
c[i]=a[i];
}
for(i=0;i<b.length;i++)
{
for(j=0;j<a.length;j++)
{
if(b[i]!=c[j])
{continue;}
else{if(b[i]==c[j])break;}}
if(j==10)
c[p++]=b[i];
}
for(i=0;i<n;i++)
{System.out.println(c[i]);}}}

--- Update ---

i googled about indenting a program.. i dint know what it was.. i m sorry for that.. m totally new... is my logic right for the union of two arrays a[] and b[]
• December 7th, 2012, 10:35 AM
Norm
Quote:

is my logic right for the union of two arrays a[] and b[]
What does the code do with small test input files?

You need to properly format the code.
• December 7th, 2012, 10:37 AM
d4divya2005