Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: my assignment..not able to complete it...please help...

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default my assignment..not able to complete it...please help...

    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.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: my assignment..not able to complete it...please help...

    You've posted your assignment but have yet to ask an answerable question. If you have a question that we can answer, please ask it, and please show any code that you've created so far.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    d4divya2005 (December 7th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: my assignment..not able to complete it...please help...

    import java.util.*;
    import java.io.*;
    class assignment{
    public static void main(String args[])
    throws Exception{
    FileReader f1 = new FileReader("c://java/2.txt");
    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();
    FileReader f2 = new FileReader("c://java/3.txt");
    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]);}}}

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: my assignment..not able to complete it...please help...

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Also provide the questions about the problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: my assignment..not able to complete it...please help...

    import java.util.*;
    import java.io.*;
    class assignment{
    public static void main(String args[]) 
    throws Exception{
    FileReader f1 = new FileReader("c://java/2.txt"); 
    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();
    FileReader f2 = new FileReader("c://java/3.txt"); 
    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

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: my assignment..not able to complete it...please help...

    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.

    . 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.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    d4divya2005 (December 7th, 2012)

  9. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: my assignment..not able to complete it...please help...

    two files contain million integers.. m not getting the output

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: my assignment..not able to complete it...please help...

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    d4divya2005 (December 7th, 2012)

  12. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: my assignment..not able to complete it...please help...

    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..

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: my assignment..not able to complete it...please help...

    .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.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: my assignment..not able to complete it...please help...

    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[]

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: my assignment..not able to complete it...please help...

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #13
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: my assignment..not able to complete it...please help...

    its just printing 0000000000

  17. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: my assignment..not able to complete it...please help...

    Please format and post the complete program so it can be tested.
    Also post the small test files used for input.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2017, 11:11 PM
  2. Need help to complete my assignment.
    By wildwhitecat in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 8th, 2012, 05:19 PM
  3. Complete newbie keybinding
    By Peperono in forum AWT / Java Swing
    Replies: 4
    Last Post: November 11th, 2011, 06:05 PM
  4. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  5. How to complete code
    By Shay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2010, 01:59 PM

Tags for this Thread