//Date: 2003.01.27
//Author: AOU
//File: areDistinct1.cpp
#include <iostream.h>
/* A function to check if all the values are unique/distinct
*/
/* Algorithm
if n <= 1 then
return true
else
do the following for i=1 to n-1
do the following for j=0 to i-1
if a[i] = a[j] then
return false
next j
next i
return true
*/
bool areDistinct(int a[], int n)
{
if (n <= 1)
return true;
else
{
int i, j;
for (i=1; i<=n-1; i++)
for (j=0; j<=i-1; j++)
if (a[i] == a[j])
return false;
return true;
}
}
void main(void)
{
//int a[] = {1};
//int a[] = {1,1};
//int a[] = {1, 2, 3};
//int a[] = {1, 2, 3, 1};
int a[] = {1, 2, 3, 4, 3};
int n = sizeof(a)/sizeof(a[0]);
if (areDistinct(a, n))
cout << "Array has distinct values\n";
else
cout << "Array does not have distinct values\n";
}
|