본문 바로가기

C_study

[C] 프로그래머스 월간 코드 챌린지 시즌 1 - 내적

각각의 배열에 저장되어있는 요소들을 인덱스가 같은것끼리 곱해서 더하는 문제입니다.

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

// a_len은 배열 a의 길이입니다.
// b_len은 배열 b의 길이입니다.
int solution(int a[], size_t a_len, int b[], size_t b_len) {
    int answer = 0;
    for(int i = 0 ; i < a_len ; i++)
    {
        answer += a[i]*b[i];
    }

    return answer;
}

for문을 사용해 배열 요소의 개수만큼 반복해서 곱해주면서 answer에 더해주었습니다.

출처: 프로그래머스 코딩 테스트 연습, 코딩테스트 연습 - 내적 | 프로그래머스 (programmers.co.kr)