-3

Problem statement:

I want to write a program that reads in a positive integer n from standard input and creates an n-by-n integer array a[][] such that a[i][j] is the product of (i+1)th row with the (j+1)th column.

Current Code:

Scanner input = new Scanner(System.in);
String inputString;
char flag = 'y';
while (flag != 'q' && flag != 'Q') {
    System.out.print("Enter a positive integer:");
    int value = input.nextInt();
    System.out.printf("Enter q to quit or any other key to quit: ");
    input.nextLine();
    inputString = input.nextLine();
    flag = inputString.charAt(0);
}

For example

if the user enters 3 the matrix needs to be 3x3 and have the product of 1 multiplied by 1, 1 multiplied by 2 and 1 multiplied by 3. Then do the same thing for 2 and 3.

randers
  • 5,031
  • 5
  • 37
  • 64

2 Answers2

1

An algorithmic description of your problem should look like:

Step 1: The user enter a positive integer n
Step 2: Create an n x n matrix
Step 3: For each element (i,j), the value of the matrix at (i,j) is (i+1)*(j+1)

Implementation:

public class Snippet {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a positive integer:");
    int n = input.nextInt();

    int[][] a = new int[n][n];

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a.length; j++) {
            a[i][j] = (i + 1) * (j + 1);
        }
    }

    for (int[] is : a) {
        System.out.println(Arrays.toString(is));
    }
 }
}
Ayoub Falah
  • 484
  • 3
  • 19
-3
 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    String inputString;
    char flag = 'y';
    int matrix[][];
    while (flag != 'q' && flag != 'Q') {
        System.out.print("Enter a positive integer:");
        int value = input.nextInt();
        System.out.printf("Enter q to quit or any other key to quit: ");
        if(value > 0){
            matrix = new int[value][value];
            displayOutput(matrix);
        }

        input.nextLine();
        inputString = input.nextLine();
        flag = inputString.charAt(0);
    }
}

private static void displayOutput(int[][] matrix) {
    for (int i = 0; i < matrix.length; i++) {

        matrix[i][0] = i+1;
    }
    for (int j = 1; j < matrix.length; j++) {
        for (int i = 0; i < matrix.length; i++) {
            matrix[i][j] = matrix[i][0]*(j+1);
        }            
    }

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

}
Input: 3
Outputs: 1 2 3
         2 4 6
         3 6 9
raven
  • 2,381
  • 2
  • 20
  • 44
  • @AyoubFalah Big O is still O(n*n) just as yours – raven May 06 '16 at 20:26
  • Yes you are right, but let P1 and P2 be two programs, with P1 has instructions of the following form O(n*n) U O(n*n) U ... U O(n*n) and P2 has instructions of the following form U O(n*n). So, although P1 and P2 has the same order , P2 is faster than P1 – Ayoub Falah May 06 '16 at 20:37