본문 바로가기
프로그래머스코딩테스트연습풀이/C언어

[프로그래머스/C언어] 특별한 이차원 배열 1

by 코코쵸마 2023. 8. 14.

문제

정수 n이 매개변수로 주어질 때, 다음과 같은 n × n 크기의 이차원 배열 arr를 return 하는 solution 함수를 작성해 주세요.

arr[i][j] (0 ≤ i, j < n)의 값은 i = j라면 1, 아니라면 0입니다.

솔루션

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int** solution(int n) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int** answer = (int**)malloc(n * n * sizeof(int*));
    for(int i = 0; i < n; i++)
        answer[i] = (int*)malloc(n * sizeof(int));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++){
            if(i == j)
                answer[i][j] = 1;
            else
                answer[i][j] = 0;
        }
    return answer;
}

https://school.programmers.co.kr/learn/courses/30/lessons/181833

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr