Scaling a 2D array in Java

3 posts / 0 new
Last post
heartin
Scaling a 2D array in Java

 

You are given a 2-Dimensional array (matrix) and a scale factor. You need to write a method to scale the array according to the scale factor. The signature of the method should be 

public static int[][] scale1(int[][] arr, int scale)

For example, if you are given a 2*3 array and scale factor is 3, the output array will be 6*9; the input and output arrays will be as follows:

Input

1 2 3 

4 5 6 

Output

1 1 1 2 2 2 3 3 3 

1 1 1 2 2 2 3 3 3 

1 1 1 2 2 2 3 3 3 

4 4 4 5 5 5 6 6 6 

4 4 4 5 5 5 6 6 6 

4 4 4 5 5 5 6 6 6 

Tags: 
Was it useful?
Arindam Ghosh
Solution to Scaling a 2D array in Java problem

package com.jee.heartin.problem;

import java.util.Scanner;

 

public class Scale2DArray {
   
    public static int[][] scaleArray(int[][] arr, int scale) {
        int[][] resultArr;
        int rowCount = 0;
        int columnCount = 0;
        boolean check = true;

        rowCount = arr.length;
        if (rowCount == 0)
            check = false;

        if (check) {
            columnCount = arr[0].length;

            System.out.println("Row count = " + rowCount);
            System.out.println("Column count = " + columnCount);

            int resultRow = rowCount * scale;
            int resultCol = columnCount * scale;

            resultArr = new int[resultRow][resultCol];

            int r = 0;
            int count = 0;
            int c;

            for (int i = 0; i < resultRow; i++) {
                c = 0;
                for (int j = 0; j < columnCount; j++) {
                    for (int k = 0; k < scale; k++) {
                        resultArr[i][c] = arr[r][j];
                        c++;
                    }
                }
                count++;
                if (count >= scale) {
                    r++;
                    count = 0;
                }

            }

            return resultArr;
        }

        else {
            System.out.println("Parent array does not have any element!!");
            return null;
        }

    }

 

    //Tester method
    public static void main(String[] args) {

        int[][] arr;

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the no. of rows for the 2D array : ");
        int r = scan.nextInt();
        System.out.println("Enter the no. of columns for the 2D array : ");
        int c = scan.nextInt();

        arr = new int[r][c];

        System.out.println("Enter the elements into the 2D array : ");
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                System.out.print("Enter integer for row " + i + " col " + j
                        + ": ");
                arr[i][j] = scan.nextInt();
            }
        }
        System.out.println("The 2D array:");
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                System.out.print(" " + arr[i][j]);
            }
            System.out.println("");
        }

        System.out.println("Enter the scale factor:");
        int scale = scan.nextInt();

        int[][] scaledArray = scaleArray(arr, scale);

        if (scaledArray == null) {
            System.out.println("The array cannot be scaled!!");
        } else {
            System.out.println("The resulting scaled array:");
            for (int i = 0; i < scaledArray.length; i++) {
                for (int j = 0; j < scaledArray[i].length; j++) {
                    System.out.print(" " + scaledArray[i][j]);
                }
                System.out.println("");
            }
        }
    }

}

Was it useful?
shams.tabrez.9
shams.tabrez.9's picture
 that was huge..........but

 that was huge..........but interesting !! thanx 

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)