Cheryl Minor
Cheryl Minor

Reputation: 1

Using Arrays to compare ordered pairs and determine mathematical systems

I am writing a program that takes ordered pairs and determines if they are Reflexive, Symmetric and Transitive... Given these points: (1,1)(1,2)(2,2)(4,4)(2,1)(3,3) Reflexive : all these are present: (1,1)(2,2)(3,3)(4,4) Symmetric: if (a, b) is present then (b, a) is present Transitive: if (a, b) and (b, c) is present then (a,c) must also be present... I am having problems because I started by using linked lists but decided that arrays would be easier. I was told to use the Point[] package, that it would make it easier than parallel arrays... this is what I have and i am not sure if it is even right?? i can't even seem to get numbers to store into my array!! Help please!!!

/****************************************************************
* Name: Cheryl Minor        Date: March 8, 2011
*
* Program: Write a program that checks whether a relation R
*   is an equivalence relation. If R is an equivalence relation
*   the program outputs the equivalence classes of R.
****************************************************************/
import java.io.*;
import java.util.*;

public class Problem17 
{
static class Point{
    int x;
    int y;
}
public static void main(String []args)
{
    Point[] line = new Point[6];
    for(int i = 0; i<line.length; i++)
    {
        line[i] = new Point();
    }

    line[0].x = 1;
    line[1].x = 1;
    line[2].x = 2;
    line[3].x = 4;
    line[4].x = 2;
    line[5].x = 3;

    line[0].y = 1;
    line[1].y = 2;
    line[2].y = 2;
    line[3].y = 4;
    line[4].y = 1;
    line[5].y = 3;  

    System.out.println(line);   
   }
}

Upvotes: 0

Views: 2079

Answers (2)

lukastymo
lukastymo

Reputation: 26809

You can write something like this. This algorithm can be improve but now is more readable:

Point[] list = new Point[5];
...
for (int i=0; i<list.length; i++) {
    // checking Reflexive
    if (list[i].x == list[i].y) System.out.println("Reflexive" + list[i]);
    for (int j=i+1; j<list.length; j++) {
        // checking Symmetric
        if (list[i].x == list[j].y && list[i].y == list[j].x) ...
        for (int k=j+1; k<list.length; k++) {
            // checking Transitive
            if (list[i].x == list[k].x && list[i].y == list[j].x && ...
        }
    }
}

Small advice:

  • add constructor Point(x,y)
  • add toString in Point()

Upvotes: 0

user unknown
user unknown

Reputation: 36269

There is one universal solution to all computer programs (aka: the golden bullet): divide et impera (invented by the romans).

Solve your problem step by step.

... a program that takes ordered pairs ... Given these points: (1,1)(1,2)(2,2)(4,4)(2,1)(3,3)

I have a first dissonance here. :)

Upvotes: 0

Related Questions