9560. Two-dimensional arrayinput, output

 

A two-dimensional array of size n * m is given. Read the elements of the array, then print them.

 

Input. The first line contains two integers n and m (n, m ≤ 100) – the number of rows and columns in the array. The next n lines each contain m integers, the elements of the array. All numbers have an absolute value not exceeding 100.

 

Output. Print n rows, each containing m integers – the elements of the array.

 

Sample input

Sample output

4 5

1 3 2 4 5

4 2 7 6 5

9 2 3 5 1

7 8 1 7 2

1 3 2 4 5

4 2 7 6 5

9 2 3 5 1

7 8 1 7 2

 

 

SOLUTION

two-dimensional array

 

Algorithm analysis

Read the input matrix into a two-dimensional array. Then print it.

 

Algorithm implementation

Declare the two-dimensional array.

 

int a[101][101];

 

Read the sizes of the array n and m.

 

scanf("%d %d", &n, &m);

 

Read the input array.

 

for (i = 1; i <= n; i++)

for (j = 1; j <= m; j++)

  scanf("%d", &a[i][j]);

 

Print the array.

 

for (i = 1; i <= n; i++)

{

  for (j = 1; j <= m; j++)

    printf("%d ", a[i][j]);

  printf("\n");

}

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int m = con.nextInt();

    int a[][] = new int[n+1][m+1];

   

    for (int i = 1; i <= n; i++)

    for (int j = 1; j <= m; j++)

       a[i][j] = con.nextInt();

 

    for (int i = 1; i <= n; i++)

    {

      for (int j = 1; j <= m; j++)

        System.out.print(a[i][j] + " ");

      System.out.println();

    }

  }

}

 

Python implementation

Read the sizes of the array n and m.

 

n, m = map(int,input().split())

 

Read the input array.

 

a = [[] for i in range(n)]

for i in range(n):

  a[i] = list(map(int, input().split()))

 

Print the array.

 

for i in range(n):

  for j in range(m):

    print(a[i][j],end = " ")

  print()

 

Python implementation – second solution

Read the sizes of the array n and m.

 

n, m = map(int,input().split())

 

Read the input array.

 

a = [[int(j) for j in input().split()] for i in range(n)]

 

Print the array.

 

for _ in a:

  for x in _:

    print(x,end=" ")

  print()

 

Python implementation – third solution

Read the input data.

 

n, m = map(int,input().split())

a = [list(map(int, input().split())) for _ in range(n)]

 

Print the array.

 

for _ in a:

  for x in _:

    print(x,end=" ")

  print()