Results 21 to 40 of 44
 03202011, 05:06 AM #21Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
 03202011, 05:14 AM #22Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
 03202011, 05:18 AM #23Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
i did not understand what do you mean the output it does not related to code.
The code says
Java Code:while (caseOne > count) { System.out.println("Case " +(count+1)+ " : ");
But I see nothing like "Case 1 : " in the output you posted.
 03202011, 05:24 AM #24Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
run:
Case 1 :
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
noneCase 2 :
2
1 1
0 1
4
1 1 1 1
1 1 1 1
1 0 0 0
0 0 0 1
noneBUILD SUCCESSFUL (total time: 1 second)
c'ause it just a head of case and i think it does not make any sense ,
sorry for that ,
 03202011, 05:49 AM #25Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
First Jos suggested using simple methods to calculate the row sum and column sum for a matrix between a start and end point. Think very seriously about doing that.
Basically you need to make sure that all the values you calculate are what you expect them to be. You can conveniently do this by printing values to the console. A good place to begin would be the calculation of sp and ep.
Java Code:// STRTPOINT int sp = m2.length  m1.length / 2; // endpoint int ep = m2.length  sp; // SUM ROW IN BIG MATRIX [color=blue]System.out.printf("sp=%d ep=%d%n", sp, ep);[/color]
Do you expect these values? In case 1 I see "sp=4 ep=2" which strikes me as strange because the start point should be less than the end point.

Another thing is the sumRowInBigMatrixANDcheckIfSame() method. You pass it a value si but you never use the value you pass. That's because you assign another value to that variable in the inner for loop. Now the value of si is actually important so by disregarding that value it won't be possible to arrive at the correct answer.
 03202011, 05:58 AM #26Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
if we calculate the start point in the case one it should give us one
and end point should give us 5 !
first i though to put it in loop but it also does not give me the correct answer !
and it will change the loop process .
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;
}
}
or i can just use one loop ! .. sorry maybe my question are little bit stupid but am still beginnerLast edited by Andrew_2; 03202011 at 06:15 AM.
 03202011, 06:47 AM #27Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
if we calculate the start point in the case one it should give us one
and end point should give us 5 !
I agree. So you had better change the lines that calculate sp and ep. Jos gave you formulas earlier in the thread for this  but notice that he used parentheses. Make sure you understand the formulas you are using. (Ask if Jos' formulas are not clear)

What are the values in sumRowA supposed to be?
You can check that they are being correctly calculated by again printing some output:
Java Code: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; } [color=blue]System.out.print("sumRowA: "); for(int val :sumRowA) { System.out.print(val + " "); } System.out.println();[/color] if (isSameRows(m2, sumRowA) == true) { return true; } else { return false; } }
 03202011, 08:37 AM #28Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
sorry for late , i went to my lecture !
so sum row A is the result of calculate the number of ones in each row in BOTH matrix.
can explain more why you made these changes
 03202011, 08:42 AM #29
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,044
 Blog Entries
 7
 Rep Power
 23
The only person who got everything done by Friday was Robinson Crusoe.
 03202011, 09:05 AM #30Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
 03202011, 09:06 AM #31Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
si and sp are the start point !
ep is the end point.
but can you explain to me why are not = 0 ?
what i understand until now that in small matrix we do not need to use start point and end point we need that only for big matrix
 03202011, 09:14 AM #32Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
The changes are only there so you can see what values sumRowA ends up. This lets you can compare them with what you expect.
It's your method, but I think you mean to calculate the 1's in the middle portion of the larger matrix. And in that case it is not clear why you use m1 which is the first (smaller) matrix. Or why the for loop limits are as they are.
 03202011, 09:18 AM #33Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
If you mean sumRowA to be the sum of the 1's in the rows of the small matrix, that's OK but you will still have to get the limits of the for loop right.
(And don't forget that the values of the start and end point where wrong before this method even gets called.)
 03202011, 09:28 AM #34Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
emmmm ! i made several modifications based on your comments !
public class Main {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("newfile"));
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) {
//read the first matrix size.
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
int sumrowA[] = sumRow(m2, sp,ep);
int sumrowB[] = sumRowSmall(m1);
int sumcolA[] = sumCol(m2, sp,ep);
int sumcolB[] = sumColSmall(m1);
boolean answer = true;
if (isSameRows(sumrowB, sumrowA) == true)
{
System.out.print("mother side");
}
else if(isSameRows(sumrowB, sumrowA) == true && isSmaeCol(sumcolB, sumcolA) == true )
{
System.out.print("both side");
}
else if(isSmaeCol(sumcolB, sumcolA) == 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 RowA[], int RowB[]) {
boolean answer ;
int c = 0;
while (c < RowA.length) {
if (RowA[c] == RowB[c]) {
c++;
} else {
return answer = false;
}
}
return answer = true;
}
public static boolean isSmaeCol(int ColA[], int ColB[]) {
boolean answer ;
int c = 0;
while (c <ColA.length) {
if (ColA[c] == ColB[c]) {
c++;
} else {
return answer = false;
}
}
return answer = true;
}
public static int [] sumRowSmall(int m1[][])
{
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 (int j = 0; j<a ; j++)
{
sum += m1[i][j];
}
sumRowA[a] = sum;
// System.out.print("sumRowA: ");
// for(int val :sumRowA) {
// System.out.print(val + " ");
}
return sumRowA;
}
public static int [] sumRow(int m1[][], 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 (int j=si; j <= ep; j++)
{
sum += m1[i][j];
}
sumRowA[i] = sum;
System.out.print("sumRowA: ");
for(int val :sumRowA) {
System.out.print(val + " ");
}
}
return sumRowA;
// if (isSameRows(m2, sumRowA) == true) {
// return true;
// } else {
// return false;
// }
}
public static int [] sumColSmall(int m1[][])
{
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 (int j = 0; j<a ; j++)
{
sum += m1[j][i];
}
sumRowA[a] = sum;
// System.out.print("sumRowA: ");
// for(int val :sumRowA) {
// System.out.print(val + " ");
}
return sumRowA;
}
public static int [] sumCol(int m1[][], 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 (int j=si; j <= ep; j++)
{
sum += m1[j][i];
}
sumColA[i] = sum;
System.out.print("sumRowA: ");
for(int val :sumColA) {
System.out.print(val + " ");
}
}
return sumColA;
// if (isSmaeCol(m2, sumColA) == true) {
// return true;
// } else {
// return false;
}
}
* i wrote two new methods for small one .
but still it does not give me the correct answer.Last edited by Andrew_2; 03202011 at 09:32 AM.
 03202011, 09:40 AM #35Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
Sorry my output 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
sumRowA: 4 0 0 0 0 0 sumRowA: 4 0 0 0 0 0 sumRowA: 4 0 4 0 0 0 sumRowA: 4 0 4 2 0 0 sumRowA: 4 0 4 2 5 0 sumRowA: 4 0 4 2 5 5 java.lang.ArrayIndexOutOfBoundsException: 4
BUILD SUCCESSFUL (total time: 1 second)
 03202011, 09:50 AM #36
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,044
 Blog Entries
 7
 Rep Power
 23
Carefully reread my previous replies; si is the start index and ei is the end index plus one, so you should sum all values in the range [si, ei) (excluding ei itself), so your loop should look like:
Java Code:for (int i= si; i < ei; i++) ...
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03202011, 10:08 AM #37Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
I have to log off now. But maybe the following makes some sense... (There is still a way to go!)
Java Code:public static boolean isSmaeCol(int ColA[], int ColB[]) { [color=blue]//boolean answer;[/color] int c = 0; while (c < ColA.length) { if (ColA[c] == ColB[c]) { c++; } else { [color=blue]//return answer = false;[/color] return false; } } [color=blue]//return answer = true;[/color] return true; }
and
Java Code:public static int[] sumRow(int m1[][], int si, int ep) { int i = 0; int a = m1.length; int sumRowA[] = new int[a]; for (i = [color=blue]si[/color]; i < ep; i++) { // for loop for 1D array int sum = 0; for (int j = si; j [color=blue]<[/color] ep; j++) { sum += m1[i][j]; } sumRowA[[color=blue]i  si[/color]] = sum; } return sumRowA; }
 03202011, 10:11 AM #38Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
don't be sloppy
Yeah, there's an element of that ;)
Good luck!
 03202011, 01:01 PM #39Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
public static int[] sumRow(int m1[][], int si, int ep) {
int i = 0;
int a = m1.length;
int sumRowA[] = new int[a];
for (i = si; i < ep; i++) { // for loop for 1D array
int sum = 0;
for (int j = si; j < ep; j++) {
sum += m1[i][j];
}
sumRowA[i  si] = sum;
}
return sumRowA;
}
i made changes it add correctly now either by your way or as i did
public static int[] sumRow(int m1[][], int si, int ep) {
int i = 0,m=0;
int a = m1.length;
int sumRowA[] = new int[a];
for (i = si; i < ep; i++) { // for loop for 1D array
int sum = 0;
for (int j = si; j < ep; j++) {
sum += m1[i][j];
}
sumRowA[m] = sum;
m++;
}
return sumRowA;
}
but the problem now is tell me
java.lang.ArrayIndexOutOfBoundsException: 4
even it's sum rows and coulmns correctly
 03202011, 01:14 PM #40Member
 Join Date
 Mar 2011
 Posts
 36
 Rep Power
 0
Similar Threads

trying to write a program for this
By durdanto in forum New To JavaReplies: 5Last Post: 02152011, 02:27 PM 
[Free] Looking for a skilled person
By JohnnyL in forum Jobs OfferedReplies: 0Last Post: 02082011, 03:55 PM 
how can write program for gui
By pothireddy in forum Advanced JavaReplies: 9Last Post: 11042010, 07:17 AM 
Trying to write my own program : /
By jdicerch in forum New To JavaReplies: 50Last Post: 09262010, 01:44 AM 
Error: Cannot delete a.txt:It is being used by another person or program
By trill in forum Advanced JavaReplies: 1Last Post: 08072007, 07:34 AM
Bookmarks