함수 array_sum()

(함수 sum()에서 넘어옴)
array_sum()
sum()

1 C[ | ]

#include<stdio.h>

int intArray_sum(int arr[], int len) {
    int result = 0;
    for(int i=0; i<len; i++) result += arr[i];
    return result;
}

void main() {
   int nums[] = {1,2,3,4};
   printf("sum = %i", intArray_sum(nums,4));
}

2 C++[ | ]

#include <iostream>
using namespace std;

int main() {
    int nums[] = {1,2,3,4};
    int sum = 0;
    for(const int& n: nums) sum += n;
    cout << sum; // 10
}
#include <iostream>
#include <numeric>
using namespace std;

int main() {
    int nums[] = {1,2,3,4};
    int sum = accumulate(nums, nums+4, 0);
    cout << sum; // 10
}

3 Excel[ | ]

=SUM(1,2,3,4)
// 10

4 Go[ | ]

package main

import "fmt"

func main() {
	nums := []int{1, 2, 3, 4, 5}
	sum := 0
	for _, num := range nums {
		sum += num
	}
	fmt.Println(sum) // 15
}

5 JavaScript[ | ]

console.log( [1,2,3,4].reduce((a,b)=>a+b) );
// 10
var arr = [1,2,3,4];
var sum = 0;
for(var i in arr) sum+=arr[i];
console.log( sum );
// 10

6 Perl[ | ]

use List::Util qw( sum );
print sum(1, 2, 3, 4) . "\n";
# 10

7 PHP[ | ]

$arr = [1,2,3,4];
echo array_sum($arr);
// 10

8 Python[ | ]

lst = [1,2,3,4]
print( sum(lst) )
// 10

9 R[ | ]

x <- c(1,2,3,4,5)
print( sum(x) )
# 15

10 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}