Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 44
  1. #21
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    yeah but when i returned to just answer , it tell me that i have initialized , and when i did that no thing changed !! it gave me the same output
    Last edited by Andrew_2; 03-20-2011 at 04:09 AM.

  2. #22
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    it same thing even if i did these modifications ,
    i did not understand what do you mean the output it does not related to code.

  3. #23
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  4. #24
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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)
    this is my original output as it showed to me , in first time i just removed it from the post !!
    c'ause it just a head of case and i think it does not make any sense ,
    sorry for that ,

  5. #25
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  6. #26
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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;
    }
    }
    i understand your point! but still confused do you want to reverse the order of loop !
    or i can just use one loop ! .. sorry maybe my question are little bit stupid but am still beginner
    Last edited by Andrew_2; 03-20-2011 at 05:15 AM.

  7. #27
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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;
        }
    }

  8. #28
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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

  9. #29
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    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;
        }
    I think si and sp are the startpoint and the enpoint of the row to sum so it shouldn't be set to zero at the start of the loop that sums the array elements. I don't understand why the OP mutilated my original methods ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #30
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by JosAH View Post
    I don't understand why the OP mutilated my original methods ...

    I have already recommended the OP give serious consideration to using the methods you provided as you provided them...

  11. #31
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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

  12. #32
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Andrew_2 View Post
    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

    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.

  13. #33
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.)

  14. #34
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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.

  15. #35
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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)

  16. #36
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    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++)
       ...
    I already gave that code to you when I showed you my sumRow and sumCol methods; don't be sloppy.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #37
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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;
    }

  18. #38
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    don't be sloppy

    Yeah, there's an element of that ;)

    Good luck!

  19. #39
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default


    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

  20. #40
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    any way thanks for every one who helped me !

    i really appreciated and i learned a lot of things here

    thanks to pbrockway2 and josAH.

    and forgive my weak English ^_^ .

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. trying to write a program for this
    By durdanto in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 01:27 PM
  2. [Free] Looking for a skilled person
    By JohnnyL in forum Jobs Offered
    Replies: 0
    Last Post: 02-08-2011, 02:55 PM
  3. how can write program for gui
    By pothireddy in forum Advanced Java
    Replies: 9
    Last Post: 11-04-2010, 06:17 AM
  4. Trying to write my own program : /
    By jdicerch in forum New To Java
    Replies: 50
    Last Post: 09-26-2010, 01:44 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:34 AM

Posting Permissions

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