5.18.2017

C++ Linear Search

실습 과제 - Linear Search
 bool linearSearch(int key, int data[], int size)

  •  크기가 size인 배열 data를 0번 방부터 마지막 방까지 차례대로 방문하면서 주어진 key와 같은 값이 있는지 확인한다. 
  • key와 같은 값이 발견되면 즉시 true를 반환한다. 
  • 마지막 방까지 다 방문을 했는데도 key와 같은 값이 발견되지 않으면 false를 반환한다.







0번 방부터 마지막 방까지 차례대로 검사

========================================================

#include <iostream>
using namespace std;
const int SIZE = 10;
int data[SIZE];
bool linearSearch(int key, int data[], int size)
{
      for (int i = 0; i < size; i++){
          if (key == data[i])
          {
                return true;
          }
      }
      return false;
}
int main()
{
      int x[] = {25, 36, 17, 12, 45, 78, 62, 31, 18, 63};
      int number;
      while (true) {
          cout << "수험번호를입력하세요: ";
          cin >> number;
          if (linearSearch(number, x, sizeof(x)/sizeof(int)))
                cout << "합격입니다" << endl;
          else cout << "불합격입니다" << endl;
      }
      system("pause");
      return 0;
}

댓글 없음:

댓글 쓰기