2 Replies - 4480 Views - Last Post: 04 November 2010 - 11:45 PM Rate Topic: -----

#1 ticedb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-November 10

Intersection of Two Arrays

Posted 04 November 2010 - 02:42 PM

Hi I need help calculating the intersection of two arrays. I have basically all of the coding done but it's still not working. I'm positive my readArray method works and pretty sure the displayArray method works. I think my main issue is in the computeIntersection method. The computeIntersection method should store the intersection of array0 with size0 and array1 with size1 in array intersection and the method should return the size. Here is what I have for my coding so far:

import java.util.*;

public class Intersection
{
  public static int readArray(int[] array)
  {
    //variables
    int values;
    int size = 0;
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter values (enter -999 to end): ");

    //getting numbers
    for(int i = 0; i < 100; i++)
    {
      values = input.nextInt();
      
      if(values != -999)
      {
        array[i] = values;
        size++;
      }
      else
      {
        break;
      }
    }
    return size;
  }
  public static int computeIntersection(int[] array0, int size0, int[] array1, int size1, int[] intersection)
  {
    //variables
    int size = 0;
    int [] tempArray = new int[100];
    int i;
    int j;
    
    //calculating intersection
    for(i = 0; i < size0; i++);
    {
      for(j = 0; j < size1; j++);
      {
        if(array0[i] == array1[j])
        {
          tempArray[i] = array0[i];
          size++;

        }
      }
    }
    return size;
  }
  public static void displayArray(int[] array, int size)
  {
    //display intersection elements
    for(int i = 0; i < size; i++)
    {
      System.out.print(array[i]);
    }
  }
  public static void main(String[] args)
  {
    //variables
    int[] array0 = new int[100];
    int[] array1 = new int[100];
    int[] intersection = new int [100];
    
    //reading arrays
    int size0 = readArray(array0);
    int size1 = readArray(array1);
    
    //finding intersection
    System.out.println("Intersection is: ");
    int sizeIntersection = computeIntersection(array0, size0, array1, size1, intersection);
    displayArray(intersection, sizeIntersection);
  }
}





Here is a sample run of how the program should work but I'm not getting that:

Enter values: 1 2 3 2 -999
Enter values: 2 3 1 5 7 9 -999
Intersection is: 1 2 3

Is This A Good Question/Topic? 0
  • +

Replies To: Intersection of Two Arrays

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Intersection of Two Arrays

Posted 04 November 2010 - 04:17 PM

You have extra semi-colons:

...
for (i = 0; i < size0; i++);
...
for (j = 0; j < size1; j++);
...



You are not using the intersection array:

...
int[] tempArray = new int[100];
...


This post has been edited by n8wxs: 04 November 2010 - 04:21 PM

Was This Post Helpful? 1
  • +
  • -

#3 fallenreaper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 240
  • Joined: 19-June 10

Re: Intersection of Two Arrays

Posted 04 November 2010 - 11:45 PM

My guess is that your "intersection" problem records the elements in which are in BOTH arrays only.

I am pretty sure your logic is wrong.

Quote

//calculating intersection
40 for(i = 0; i < size0; i++);
41 {
42 for(j = 0; j < size1; j++);
43 {
44 if(array0[i] == array1[j])
45 {
46 tempArray[i] = array0[i];
47 size++;
48
49 }
50 }
51 }


tempArray at that location is set to the element.what the if statement should look like is something like the following:

if ( array0[i] == array1[j]){
  tempArray[size] = array0[i];
  size++;
}



instead of returning the size of it, what you should do is return tempArray and make slight adjustments in your method header.

that way, after exiting the method, you could say something along the lines of
int i = 0;
while (tempArray[i] != null){  //null may have to be adjusted as i forget if an int[] is auto set to all 0's
  System.out.print(tempArray[i]+" ");
  i++;
}
System.out.println("");



Does this help at all?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1