Results 21 to 40 of 44
- 03-20-2011, 04:06 AM #21
Member
- Join Date
- Mar 2011
- Posts
- 36
- Rep Power
- 0
- 03-20-2011, 04:14 AM #22
Member
- Join Date
- Mar 2011
- Posts
- 36
- Rep Power
- 0
- 03-20-2011, 04:18 AM #23
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 03-20-2011, 04:24 AM #24
Member
- Join Date
- Mar 2011
- Posts
- 36
- Rep Power
- 0
this is my original output as it showed to me , in first time i just removed it from the post !!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 ,
- 03-20-2011, 04:49 AM #25
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 03-20-2011, 04:58 AM #26
Member
- 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 .
i understand your point! but still confused do you want to reverse the order of loop !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; 03-20-2011 at 05:15 AM.
- 03-20-2011, 05:47 AM #27
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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; } }
- 03-20-2011, 07:37 AM #28
Member
- 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
- 03-20-2011, 07:42 AM #29
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-20-2011, 08:05 AM #30
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
- 03-20-2011, 08:06 AM #31
Member
- 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
- 03-20-2011, 08:14 AM #32
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 03-20-2011, 08:18 AM #33
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.)
- 03-20-2011, 08:28 AM #34
Member
- 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; 03-20-2011 at 08:32 AM.
- 03-20-2011, 08:40 AM #35
Member
- 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)
- 03-20-2011, 08:50 AM #36
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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:
I already gave that code to you when I showed you my sumRow and sumCol methods; don't be sloppy.Java Code:for (int i= si; i < ei; i++) ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-20-2011, 09:08 AM #37
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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; }
- 03-20-2011, 09:11 AM #38
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
don't be sloppy
Yeah, there's an element of that ;)
Good luck!
- 03-20-2011, 12:01 PM #39
Member
- 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
- 03-20-2011, 12:14 PM #40
Member
- 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: 02-15-2011, 01:27 PM -
[Free] Looking for a skilled person
By JohnnyL in forum Jobs OfferedReplies: 0Last Post: 02-08-2011, 02:55 PM -
how can write program for gui
By pothireddy in forum Advanced JavaReplies: 9Last Post: 11-04-2010, 06:17 AM -
Trying to write my own program : /
By jdicerch in forum New To JavaReplies: 50Last Post: 09-26-2010, 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: 08-07-2007, 07:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks