Algorithm 10 - Find the smallest integer in the array

Beast from the east·2021년 10월 6일
0

Algorithm

목록 보기
10/27

Q.

Description:
Given an array of integers your solution should find the smallest integer.

For example:

Given [34, 15, 88, 2] your solution will return 2
Given [34, -345, -1, 100] your solution will return -345
You can assume, for the purpose of this kata, that the supplied array will not be empty.

A)

#include <stddef.h>

int find_smallest_int(int *vec, size_t len)
{
  size_t min_idx = 0;
  size_t i = 1;
  while (i < len)
  {
    if (vec[min_idx] > vec[i])
        min_idx = i;
    i++;
  }
  return vec[min_idx];
}

another solution
사망연산자 쓰기
profile
Hello, Rabbit

0개의 댓글