반응형

문제

두 개의 배열을 비교하여 같으면 true를, 아니면 false를 리턴하는 제네릭 함수 equalArray()를 작성하라. 또한 main() 함수를 작성하여 equalArrays()를 호출하는 몇 가지 사례를 보여라. equalArrays()를 호출하는 코드 사례는 다음과 같다.

 

메인함수

int x[] = { 1,10,100,5,4 };
	int y[] = { 1,10,100,5,4 };
	if (equalArrays(x, y, 5))cout << "같다";
	else cout << "다르다";

 

소스코드

#include<iostream>
using namespace std;
template <class T>
T equalArrays(T x[], T y[], T n) {
	for (T i = 0; i < n; i++)
		if (x[i] != y[i])
			return false;
	return true;
}
int main() {
	int x[] = { 1,10,100,5,4 };
	int y[] = { 1,10,100,5,4 };
	if (equalArrays(x, y, 5))cout << "같다";
	else cout << "다르다";
}
반응형

+ Recent posts