
Patrick B. answered 11/12/20
Math and computer tutor/teacher
The definition of SET is that each element can only appear ONCE and only once...
So first, SORTS the elements of the domain in set X, which contains N elements
then use a for loop, to check for two elements next to each other that are equal, like this:
boolReturn=true
for iLoop=0 to N-2
if X(iLoop) equals X(iLoop+1) then
boolReturn = false
exit Loop
endif
end for loop
if boolReturn is true <-- does it again for the range Y
sorts the elements of the range in array set Y
for iLoop=0 to N-2
if Y(iLoop) equals Y(iLoop+1) then
boolReturn = false
exit Loop
endif
end for loop
end if
here is some code written in C++ that actually does this!!!!!!!
using namespace std;
#include <iostream>
#include <stdlib.h>
int compareDblFloatAmt( const void * val1, const void * val2)
{
double * dblPtr1 = (double*)val1;
double * dblPtr2 = (double*)val2;
double dblAmt1 = *dblPtr1;
double dblAmt2 = *dblPtr2;
int iReturn=0;
if (dblAmt1!=dblAmt2)
{
iReturn = (dblAmt1>dblAmt2) ? 1 : - 1;
}
return(iReturn);
}
bool isOneToOne( double * x, double * y, int n)
{
qsort(x,n,sizeof(double),compareDblFloatAmt);
bool boolReturn=true;
for (int iLoop=0; iLoop<n-1; iLoop++)
{
if (x[iLoop]==x[iLoop+1])
{
boolReturn=false;
break;
}
}
if (boolReturn)
{
qsort(y,n,sizeof(double),compareDblFloatAmt);
for (int iLoop=0; iLoop<n-1; iLoop++)
{
if (y[iLoop]==y[iLoop+1])
{
boolReturn=false;
break;
}
}
}
return(boolReturn);
}
int main()
{
double X[] = {5,7,9,11};
double Y[] = {10,14,18,22}; //1 -to- 1 function y = 2x
int N=4;
/**********************************
double X[] = {-3,-2,-1, 0, 1};
double Y[] = { 8, 3, 0,-1, 0}; //not 1 -to- 1 function y = x^2-1
int N=5;
*******************************************/
if (isOneToOne(X,Y,N))
{
cout << " one -to- one " << endl;
}
else
{
cout << " not one-to-one " << endl;
}
}