본문 바로가기

국비교육/국비교육 복습

Day08_array2d_Test04 : 2차원 배열에서의 합계 (★)

(Q) 사용자에게 줄 수와 칸 수를 입력받아 해당하는 크기만큼의 2차원 배열을 생성하고
좌측 상단에 1부터 채워넣어 끝까지 1씩 증가하며 숫자가 채워지도록 구현하세요
구현 후 배열을 출력하세요
ex) 줄 수 : 3, 칸 수 : 4
      1 2 3 4
      5 6 7 8
      9 10 11 12
(추가) 줄별로 합계를 구하세요

 

 

1) 반복수 i, j로 배열의 위치 array[i][j]를 지정하는 경우

package day08;

import java.util.Scanner;

public class Day08_array2d_Test04 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		// 행과 열 입력
		int row = sc.nextInt();
		int column = sc.nextInt();
		
		// 배열 생성
		int[][] array = new int[row][column];
		
		// 배열에 숫자 대입
		int count = 1;
		for(int i  = 0 ; i < array.length ; i ++) {
			for(int j = 0 ; j < array[i].length ; j ++) {
				array[i][j] = count;
				count ++;
			}
		}
		
		// 배열 출력
		for(int i = 0 ; i < array.length ; i ++) {
			for(int j = 0 ; j < array[i].length ; j ++) {
				System.out.print(array[i][j] + "\t");
			}
			System.out.println();
		}
		System.out.println();
		
		// (추가) 줄별 합계 및 출력
		for(int i = 0 ; i < array.length ; i ++) {
			int rowSum = 0;
			for(int j = 0 ; j < array[i].length ; j ++) {
				rowSum += array[i][j];
			}
			System.out.println(i + "번째 행의 합계 : " + rowSum);
		}
	}
}

 

- 반복문의 count 활용을 이용하여 각 배열에 1부터 숫자 대입

- 행별 합계를 출력할 때 매 행이 바뀔 때마다(i가 바뀔때마다) 행 합계(rowSum)을 0으로 초기화한다

 

 

 

2) 위치 변수 x, y를 직접 만들어 array[x][y]를 지정하는 경우

package day08;

import java.util.Scanner;

public class Day08_array2d_Test04_2 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		// 행과 열 입력
		int row = sc.nextInt();
		int column = sc.nextInt();
		
		// 배열 생성
		int[][] array = new int[row][column];
		
		// 배열에 숫자 대입
		int x = 0;
		int y = 0;
		for(int i = 1 ; i <= row * column ; i ++) {
			array[x][y] = i;
			if(y < column - 1) {
				y ++;
			}
			else {
				y = 0;
				x ++;
			}
		}
		
		// 배열 출력
		for(int i = 0 ; i < array.length ; i ++) {
			for(int j = 0 ; j < array[i].length ; j ++) {
				System.out.print(array[i][j] + "\t");
			}
			System.out.println();
		}
		System.out.println();
		
		// 줄별 합계 및 출력
		for(int i = 0 ; i < array.length ; i ++) {
			int rowSum = 0;
			for(int j = 0 ; j < array[i].length ; j ++) {
				rowSum += array[i][j];
			}
			System.out.println(i + "번째 행의 합계 : " + rowSum);
		}
	}
}

 

 

 

 

 

3)  위치 변수 x, y를 직접 만들어 array[x][y]를 지정하는 경우 + count 이용

package day08;

import java.util.Scanner;

public class Day08_array2d_Test04_2 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		// 행과 열 입력
		int row = sc.nextInt();
		int column = sc.nextInt();
		
		// 배열 생성
		int[][] array = new int[row][column];
		
		// 배열에 숫자 대입
		int x = 0;
		int y = 0;
		int count = 0;
		for(int i = 0 ; i < row * column ; i ++) {
			if(y < column - 1) {
				count ++;
				array[x][y] = count;
				y ++;
			}
			else {
				count ++;
				array[x][y] = count;
				y = 0;
				x ++;				
			}
		}
		
		// 배열 출력
		for(int i = 0 ; i < array.length ; i ++) {
			for(int j = 0 ; j < array[i].length ; j ++) {
				System.out.print(array[i][j] + "\t");
			}
			System.out.println();
		}
		System.out.println();
		
		// 줄별 합계 및 출력
		for(int i = 0 ; i < array.length ; i ++) {
			int rowSum = 0;
			for(int j = 0 ; j < array[i].length ; j ++) {
				rowSum += array[i][j];
			}
			System.out.println(i + "번째 행의 합계 : " + rowSum);
		}
	}
}