알고리즘 6 - Sum of positive

jabae·2021년 10월 4일
0

알고리즘

목록 보기
6/97

Q.

You get an array of numbers, return the sum of all of the positives ones.

Example [1,-4,7,12] => 1 + 7 + 12 = 20

Note: if there is nothing to sum, the sum is default to 0.

A)

#include <stddef.h>

int positive_sum(const int *values, size_t count)
{
  int sum;
  
  sum = 0;
  while(count >= 1)
  {
    if (values[count - 1] > 0)
      sum += values[count - 1];
    count--;
  }
  return (sum);
}
profile
it's me!:)

0개의 댓글