# Thread: write a program to know the related between two person | help | help | help | --

1. Member
Join Date
Mar 2011
Posts
36
Rep Power
0

## write a program to know the related between two person | help | help | help | --

A new race was identified to live on ZOLO, a newly discovered planet. The DNA of these people is much simpler than that of human beings. Each trace is coded by a square of size n x n where n is an even number and 2  n  50. Each cell of the square is either 0 (OFF) or 1 (ON). Scientists discovered that the relationship between two persons is based on the similarity between the cells of the person with the smaller square trace and the cells of an equal size square centered in the middle of the trace of the person with larger square trace. (the two traces can be of equal size). The relationship is based on the following findings:

• If the number of ones of the corresponding rows in the compared traces are equal (e.g. row#0 of first trace with row#0 of the second trace, row#1 of the first trace with row#1 of the second trace, and so on), then they are related from their mothers’ sides.
• If the number of ones of the corresponding columns in the compared traces are equal (e.g. column#0 of first trace with column#0 of the second trace, column#1 of the first trace with column#1 of the second trace, and so on), then they are related from their fathers’ sides.
• If the number of ones of the corresponding rows and the corresponding columns in the compared traces are equal, then they are related from both their mothers sides and fathers sides.

For example, suppose, two persons X and Y have the following traces, respectively:
0 1 1 1 1 0
0 0 0 0 0 0
1 1 1 1 1 0
1 1 1 0 0 0
1 1 1 0 1 1
1 1 1 1 1 1
1 1 0 1
1 1 1 1
1 0 0 0
0 1 0 0

We say X and Y are related from their fathers’ sides since the number of ones of the corresponding columns are equal.
Input
The input data is stored in file “digital.in”. The first line of the input file contains a single integer t (1  t  10), the number of test cases, subsequent lines correspond to the t test cases.
For each test case, the first line contains the number of rows in the small trace person. The subsequent line(s) contains data to fill the cells of that person row by row. Then, a line that contains the number of rows of the larger or equal size trace person. The subsequent line(s) contains data to fill the cells of the person with larger or equal size trace row by row.
Output
The output should be stored in file “digital.out”. The output should contain one line for each test case (as shown in the sample output below) indicating the type of relationship between the two persons as follows: Mothers’ sides, Father’s sides, Both sides, or Not related

Sample Input
The following input file contains 2 test cases. The first test case corresponds to 4x4 and 6x6 traces of two persons, and the second test case corresponds to 2x2 and 4x4 traces of another couple of persons.

2
4
0 0 0 0
1 1 1 1
1 1 0 0
1 1 1 1
6
0 1 1 1 1 0
0 0 0 0 0 0
1 1 1 1 1 0
1 1 1 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
2
1 1
0 1
4
1 1 1 1
1 1 1 1
1 0 0 0
0 0 0 1

Sample Output
Case 1: Both sides
Case 2: Not related

THIS IS MY ASSIGNMENT QUESTION !
Last edited by Andrew_2; 03-20-2011 at 04:49 AM.

2. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
(read file ) - done !
now i need write to method :
to sum ones in each row and col by these parameters
public static void colSum(int[][]a,int si, int en,int [] A)
public static void rowSum(int[][]b,int si, int en,int [] col B)
but i have no idea how i can write them !

with si ( start point ) = matrix2 (big one ) - matrix1(small one ) /2
and en ( end point ) = matrix2 - start point .

note that two matrixes are not equal .
and the small one will compared with largest one .. and will be in the middle so that we need start point and end point.
Last edited by Andrew_2; 03-19-2011 at 11:13 AM.

3. Have you tried it out at all? If needed re read the question until you fully understand it.

4. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
yeah ! i understand the question but my problem with these two methods and i wrote the code , but as i said i need help to write the methods only c'ause i have no idea.

5. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
please if any one have any idea help me i need that's as soon as possible.

* sorry for my weak English :(
Last edited by Andrew_2; 03-19-2011 at 11:14 AM.

6. Originally Posted by Andrew_2
For example, suppose, two persons X and Y have the following traces, respectively:
0 1 1 1 1 0
0 0 0 0 0 0
1 1 1 1 1 0
1 1 1 0 0 0
1 1 1 0 1 1
1 1 1 1 1 1

1 1 0 1
1 1 1 1
1 0 0 0
0 1 0 0

We say X and Y are related from their fathers’ sides since the number of ones of the corresponding columns are equal.
I don't understand your example, i.e. the number of ones in the first matrix columns are 4, 5, 5 and 3 respectively for the first four columns while those values are 3, 3, 1 and 2 respectively for the second matrix. Those values aren't equal.

kind regards,

Jos

7. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
emmm , the program should read the number of cases first which is two from the file.
then read the size of two matrices (4 and 6) in case one and ( 2 and 4 ) in case two and fill the elements in 2d array . i did that.

now i have to write two methods .
first method : should count the ( sum of ones in each row in the first matrix )
this one
0 0 0 0
1 1 1 1
1 1 0 0
1 1 1 1
and i have to use the same method to count the ones in each row of the second matrix
0 1 1 1 1 0
0 0 0 0 0 0
1 1 1 1 1 0
1 1 1 0 0 0
1 1 1 1 1 1
1 1 1 1 11

and compare two matrices if sum of ones are equal .. they are related from mother side
the same thing to columns , but if are equal they are related from father side
if both equal they related from both and else none.

but you noticed that the two matrices does not have same size so the lecturer said we need start point and end pint.
because the small matrix is in the middle of the big matrix. (based on the two equation )

start point which = matrixtwo-matrixone/2
and end pointtwo = matrixtwo-startpoint
i don't know how i use these two equations in my program . i can send to you my code
if you want.
Last edited by Andrew_2; 03-19-2011 at 01:18 PM.

8. Originally Posted by Andrew_2
but you noticed that the two matrices does not have same size so the lecturer said we need start point and end pint.
because the small matrix is in the middle of the big matrix. (based on the two equation )
Ah, ok, I must've missed that; so if one matrix has an even length (either rowwise or columnwise) and the other one has an odd size there is no middle part. Right? (e.g. sizes 7 and 4) If both sizes are odd/even then the starting point is:

Let n >= m where n and m are the matrix sizes
si= (n-m)/2 where si is the start index
ei= n-si where ei is the end index

You have to check the elements in the largest array in the index range [si, ei)
and all the elements in the smaller matrix. That isn't rocket science, just a bit of index fiddling. I'd construct two simple methods rowSum( ... ) and colSum( ... ) that adds the number of ones in a row or column respectively in a certain index range, e.g.

Java Code:
```int rowSum(int[][] a, int row, int si, int ei) {
int sum= 0;
for(; si < ei; si++)
sum+= a[row][si];
return sum;
}

int colSum(int[][] a, inr col, int si, int ei) {
int sum= 0;
for (; si < ei; si++)
sum+= a[si][col];
return sum;
}```
You should be able to take it from here ...

kind regards,

Jos

9. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
great , and thank you i'll try it now
but i need to put the result in 1D array so if i change the signature of method to

public static int[] sumRow(int[][] a, int row, int si, int ei)
{
int sum=0;
int[]B=new int[row];
for(si=0; si < ei; si++)
{
sum+= a[row][si];
B[row]=sum;
}
return B;

}

that will be fine ? and can you please explain to me why you did not put this statement to check if it's one or not , sorry am still beginner in java =\$
if(int[][] a ==1)

10. Originally Posted by Andrew_2
great , and thank you i'll try it now
but i need to put the result in 1D array so if i change the signature of method to

public static int[] sumRow(int[][] a, int row, int si, int ei)
{
int sum=0;
int[]B=new int[row];
for(si=0; si < ei; si++)
{
sum+= a[row][si];
B[row]=sum;
}
return B;

}

that will be fine ? and can you please explain to me why you did not put this statement to check if it's one or not , sorry am still beginner in java =\$
if(int[][] a ==1)
You're making things more complicated than needed again: my methods only calculate the sum of a single row/column while you are trying to do it for alle relevant rows/columns in one sweep. Don't do that but take clear, well defined baby steps.

If your matrixes contain zeros and ones only there's no need to test anything, i.e. simply add those values and the sum equals the number of ones in a row/column.

kind regards,

Jos

11. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
Ok , now i got the idea , and thank you but am still confused about put the result of sum in 1D array because that is required in the assignment so are my changes in your code are correct or not ?

12. Originally Posted by Andrew_2
Ok , now i got the idea , and thank you but am still confused about put the result of sum in 1D array because that is required in the assignment so are my changes in your code are correct or not ?
Don't change anything in those methods; they're simple and do what they have to do: calculate the number of ones in a range of a row/column. Better build a simple loop around them where you can store those sums in your 1D arrays.

kind regards,

Jos

13. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
I'll try that now ,
Thank you very much , and my all respect to you =)

14. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
this is my source code and my program still not working probably can you help me please

Java Code:
```    public static void main(String[] args) {

try {
Scanner sc = new Scanner(new File("filename"));
int caseOne = sc.nextInt();
int count = 0;
int[][] m2 = new int[0][0];
int[][] m1 = new int[0][0];
int r = 0;
int c = 0;
while (caseOne > count) {
System.out.println("Case " +(count+1)+ " : ");
int length1 = sc.nextInt();
System.out.println(length1);
m1 = new int[length1][length1];
// print array in rectangular form
for (r = 0; r < m1.length; r++) {
for (c = 0; c < m1[r].length; c++) {
m1[r][c] = sc.nextInt();
System.out.print(" " + m1[r][c]);
}
System.out.println("");
}

//case two
int length2 = sc.nextInt();
System.out.println(length2);
m2 = new int[length2][length2];
// print array in rectangular form
for (r = 0; r < m2.length; r++) {
for (c = 0; c < m2[r].length; c++) {
m2[r][c] = sc.nextInt();
System.out.print(" " + m2[r][c]);
}
System.out.println("");
}

// STRTPOINT
int sp = m2.length - m1.length / 2;
// endpoint
int ep = m2.length - sp;
// SUM ROW IN BIG MATRIX

if (sumRowInBigMatrixANDcheckIfSame(m1, m2,sp , ep ) == true && sumColANDcheckIfSame(m1, m2,sp , ep ) == true) {
System.out.print("related from both sides");
} else if (sumRowInBigMatrixANDcheckIfSame(m1, m2,sp , ep ) == true) {
System.out.print("mother side");
} else if (sumColANDcheckIfSame(m1, m2,sp , ep ) == true) {
System.out.print("father side");
} else  {
System.out.print("none");
}
count++;
}
// SUM ROW IN SMALL MATRIX

} catch (Exception e) {
System.out.println(e);
}
}

public static boolean isSameRows(int A[][], int RowB[]) {
int rowAsum[] = new int[A.length];
for (int a = 0; a < A.length; a++) {
for (int b = 0; b < A.length; b++) {
rowAsum[a] = rowAsum[a] + A[a][b]; // calculate the raw sum
}
}
int c = 0;
while (c < A.length) {
if (rowAsum[c] == RowB[c]) {
c++;
} else {
}
}

}

public static boolean isSmaeCol(int A[][], int ColB[]) {
int colAsum[] = new int[A.length];
for (int a = 0; a < A.length; a++) {
for (int b = 0; b < A.length; b++) {
colAsum[a] = colAsum[a] + A[b][a]; // calculate the raw sum
}
}
int c = 0;
while (c < A.length) {
if (colAsum[c] == ColB[c]) {
c++;
} else {
}
}

}

public static boolean sumRowInBigMatrixANDcheckIfSame(int m1[][], int m2[][], int si, int ep) {
int i = 0;
int a = m1.length;
int sumRowA[] = new int[a];
for (i = 0; i < a; i++) { // for loop for 1D array
int sum = 0;
for (si = 0; si < ep; si++) {
sum += m1[i][si];
}
sumRowA[i] = sum;
}
if (isSameRows(m2, sumRowA) == true) {
return true;
} else {
return false;
}
}

public static boolean sumColANDcheckIfSame(int m1[][], int m2[][], int si, int ep) {
int i = 0;
int a = m1.length;
int sumColA[] = new int[a];
for (i = 0; i < a; i++) { // for loop for 1D array
int sum = 0;
for (si = 0; si < ep; si++) {
sum += m1[si][i];
}
sumColA[i] = sum;
}
if (isSmaeCol(m2, sumColA) == true) {
return true;
} else {
return false;
}
}
}```
Last edited by Andrew_2; 03-19-2011 at 08:50 PM.

15. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
can any body help me out as soon as possible ,,

16. You haven't asked an answerable question in the last two posts, just a "here's my code, fix it for me". What are you currently having problems with?

17. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
I Did not say that !
i mentioned my problem with two methods earlier and that what i discussed with josAH , but am still stack in this problem !!! so i posted my code to know where i made mistake !! not to just fix it!!
Last edited by Andrew_2; 03-20-2011 at 03:36 AM.

18. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
this is my source code and my program still not working probably

Does this code (or the class from which it came) compile? If not, post the compiler messages And say what lines of your code they refer to. Posting this information will help people explain what the messages mean.

Does the code compile but behave at runtime in ways you do not expect or want? (including giving runtime errors) If so, describe the runtime behaviour and post any error messages. Say what behaviour you expected or intended. Posting this information will identify what the problem is.

19. Member
Join Date
Mar 2011
Posts
36
Rep Power
0
this is my all program code , my problem is the output is suppose give me two instructions
one tell me that two cases related from both side and case two are not related at all similarly to example that i posted earlier .. but what i got is that ..
run:
4
0 0 0 0
1 1 1 1
1 1 0 0
1 1 1 1
6
0 1 1 1 1 0
0 0 0 0 0 0
1 1 1 1 1 0
1 1 1 0 0 0
1 1 1 1 1 1
1 1 1 1 1 1
none2
1 1
0 1
4
1 1 1 1
1 1 1 1
1 0 0 0
0 0 0 1
none
BUILD SUCCESSFUL (total time: 1 second)
you can noticed that it add to the input file (none) which is wrong answer for case one !
so that i could not determine where is my mistake
Last edited by Andrew_2; 03-20-2011 at 04:01 AM.

20. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Thanks for that.

One thing I did notice is that you have

Java Code:
`return answer = true;`

in a couple of places. What this does is assign true to answer and then return true.

I think you meant

Java Code:
`return answer;`

or maybe remove the answer variable completely and say

Java Code:
`return true;`
-------

Note that the output you posted does not relate to the code you posted.
Last edited by pbrockway2; 03-20-2011 at 05:04 AM.

Page 1 of 3 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•