Yes, the function exists..
This is the code:
Code:
#include <iostream>
using namespace std;
template <class type> type mcm(type*,type);
template <class type> type MinInArr(type*,type);
// template <class type> type MaxInArr(type*,type);
template <class type> type* AddizionaFrazioni(type**,type);
int main()
{
cout << "Start.." << endl;
int frazioni[2][2] = { {1,4}, {2,4} };
int *risultato;
risultato = AddizionaFrazioni(&frazioni,2);
cout << risultato[0] << "%" << risultato[1] << endl;
return 0;
}
template <class type> type* AddizionaFrazioni(type** frazioni, type nfrazioni)
{
type denominatore = 0;
type *denominatori = new type[nfrazioni];
type numeratore = 0;
for(type i = 0; i < nfrazioni; i++)
{
denominatori[i] = frazioni[i][0];
}
denominatore = mcm(denominatori,nfrazioni);
delete [] denominatori;
for(int i = 0; i < nfrazioni; i++)
{
numeratore += denominatore/frazioni[i][1]*frazioni[i][0];
}
type risultato = new type[2];
risultato[0] = numeratore;
risultato[1] = denominatore;
return risultato;
}
template <class type> type mcm(type *numeri, type len)
{
type mcm = MinInArr(numeri,len);
type i = 0, err = 0;
while(1)
{
err = 0;
for(i = 0; i < len; ++i)
{
if( (mcm % numeri[i]) != 0 )
{
++err;
break;
}
}
if (err == 0)
{
return mcm;
}
++mcm;
}
}
template <class type> type MinInArr(type *numeri, type len)
{
type min = NULL;
for(type i = 0; i < len; ++i)
{
if( (min == NULL) || (min > numeri[i]) )
{
min = numeri[i];
}
}
return min;
}
/*
template <class type> type MaxInArr(type *numeri, type len)
{
type max = NULL;
for(type i = 0; i < len; ++i)
{
if( (max == NULL) || (max < numeri[i]) )
{
max = numeri[i];
}
}
return max;
}
*/