0

Hello i have this program and i wanted to know what do you think i can do to change the first row and the last row

For example:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Changed to:

4 2 3 1

8 6 7 5

12 10 11 9

16 14 15 13

This is what i have so far

import java.io.*;
import java.util.Scanner;
public class DynamicMatrix {

    public static void main(String args[]){

        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows and colomns:");
        int row=sc.nextInt();
        int col=sc.nextInt();
        int arr[][]=new int[row][col];
        System.out.println("Enter the numbers for the matrix:");
        for(int i=0;i<row;i++)
            for(int j=0;j<col;j++)
                arr[i][j]=sc.nextInt();

    }

}
Barney
  • 2,355
  • 3
  • 22
  • 37
  • What have you tried? Give it a shot. If you can't figure it out I can post how to do it and explain why it works. –  Mar 14 '13 at 01:15
  • Write out the top row on paper. Look at it. Think about where each number needs to move to. Any time you 'operate' on the row, write out the new row again. Write the contents of any temporary variable you might need. Think about how you would achieve this in a loop. Have a crack at it. Post your code if you run into trouble. – paddy Mar 14 '13 at 01:19
  • Thanks for your comments, i tried doing a Swap but i wasn't sure how to do a correct Swap with two rows, i don't know how to do it when someone has a import java.io.* – Christy Jacobs Mar 14 '13 at 01:28

2 Answers2

1
// display your matrix:

 for(int i=0;i<row;i++){
   System.out.println("");
   for(int j=0;j<col;j++){
       System.out.print(arr[i][j]);
   }
  }

// reversing your matrix

int temp = 0 ;
 for(int i=0;i<row/2;i++){
   for(int j=0;j<col/2;j++){
       temp = arr[i][j];
       arr[i][j] = arr[i][col-j-1]; 
       arr[i][col-j-1] = temp ;
   }
  }

// display the reversed one

     for(int i=0;i<row;i++){
       System.out.println("");
       for(int j=0;j<col;j++){
           System.out.print(arr[i][j]);
       }
      }

UPDATE

The whole thing altogether should look like this :

import java.io.*;
import java.util.Scanner;
public class DynamicMatrix {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the number of rows and colomns:");
   int row=sc.nextInt();
   int col=sc.nextInt();
   int arr[][]=new int[row][col];
     System.out.println("Enter the numbers for the matrix:");
     for(int i=0;i<row;i++)
     for(int j=0;j<col;j++)
     arr[i][j]=sc.nextInt();
// display your matrix:

     for(int i=0;i<row;i++){
       System.out.println("");
       for(int j=0;j<col;j++){
           System.out.print(arr[i][j]);
       }
      }

    // reversing your matrix

    int temp = 0 ;
     for(int i=0;i<row/2;i++){
       for(int j=0;j<col/2;j++){
           temp = arr[i][j];
           arr[i][j] = arr[i][col-j-1]; 
           arr[i][col-j-1] = temp ;
       }
      }

    // display the reversed one

         for(int i=0;i<row;i++){
           System.out.println("");
           for(int j=0;j<col;j++){
               System.out.print(arr[i][j]);
           }
          }
    }
}

UPDATE :

I updated the code it should work fine now. I was redoing the inverse twice because I was iterating throw all col and all row instead of iterating half of them.

Output:

Enter the number of rows and colomns:
2
2
Enter the numbers for the matrix:
1
2
3
4

12
341 will be replacing 2
2

21
34
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
0

Try the code below...

public static void main(String[] args){
    Integer array[][] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
    printArray(array);
    System.out.println();
    printArray(reverseRows(array));
}

public static Integer[][] reverseRows(Integer[][] array){
    Integer[][] arrayToReturn = new Integer[array.length][array[0].length];
    for(int i = 0 ; i < array[1].length; i ++){
        arrayToReturn[i] = Arrays.copyOf(array[i], array[i].length);
        Integer newFirst = arrayToReturn[i][arrayToReturn[i].length - 1];
        Integer newLast = arrayToReturn[i][0];
        arrayToReturn[i][0] = newFirst;
        arrayToReturn[i][arrayToReturn[i].length - 1] = newLast;
    }
    return arrayToReturn;
}

public static void printArray(Integer[][] array){
    for(int i = 0 ; i < array.length ; i ++){
        System.out.println(Arrays.toString(array[i]));
    }
}

It will output

[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]

[5, 2, 3, 4, 1]
[10, 7, 8, 9, 6]
[15, 12, 13, 14, 11]
[20, 17, 18, 19, 16]
[25, 22, 23, 24, 21]

Which I think is what you are looking for. This assumes that you have the whole array in memory already.

nattyddubbs
  • 2,085
  • 15
  • 24
  • thanks nattyddubbs but for some reason it didnt work i got this: – Christy Jacobs Mar 14 '13 at 02:21
  • run: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: at DynamicMatrix.printArray(DynamicMatrix.java:24) at DynamicMatrix.main(DynamicMatrix.java:6) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds) } – Christy Jacobs Mar 14 '13 at 02:22
  • your program is doing what i am lookin for, but i wanted to try and make it dynamic so the user can enter how many rows and colums they need – Christy Jacobs Mar 14 '13 at 02:23
  • I didn't include any user input portion. I wanted to point out that you can do the switching and printing in a cleaner fashion than the accepted answer. – nattyddubbs Mar 14 '13 at 02:46
  • oh okay thanks too, your program is just what i need to :) thanks sooo much natty – Christy Jacobs Mar 14 '13 at 02:50