Optimizing iteration using recursion

Hello forum Users,
I have three bodies of the same void.
I ask you about idea for recursion or faster iteration.

void Zliczanie(double B1[], double B2[], int B3[])
/*{
q=0;
for(int S=0;S<=19;S++)
{
for(int s=q; s<=18423;s++)

{ double a=(B1[s]-B2[S])/B2[S];
if(a<0){a*=-1;};
if(a<0.000000001){B3[s]+=1;q=s;break;};}
}
}*/
{
q=0;
int s=-1;
double a;
for(int S=0;S<=19;S++)
{
do
{
++s;
a=(B1[s]-B2[S])/B2[S];
if(a<0){a*=-1;};

}while(a>0.000000001);

++B3[s];
}
q=s;
}

/*{
q=0;
int s=0;
int S = 0;
while(true)
{
double a=(B1[s]-B2[S])/B2[S];
if(a<0){a*=-1;};
if(a>0.000000001)
{
++s;
}
else
{
++S;
++B3[s];
}

if(S==20)
break;
}
q=s;
}*/

I put a sample code to call the function void. The data in Table Z [] are always growing and unique, without repetition.
I have written sample code to the random generator, so that the numbers in the table have been growing, for example the number is duplicated,
what you will see after compiling the code given below.
The first and third body of void works on the same principle counts the number of unique repetitive as well. The body of void can do it because
in the actual code I wrote the values are unique and growing.
Moreover, the second body of Void counts unique values in the table, which also is great because they are the unique values in the table Z [].
In turn, the variable 'q' after each iteration function counts the zero in the table Z [], also in this example, the variable q is the start value of 19 or 20 and decreases with the number of iterations function.
The variable q is the need to further optimize the code for the other iterations.

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <fstream>
#include <cmath>
#include <random>
#include <chrono>
#include <iomanip>

using namespace std;
double liczby[]={1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0,20.0};
double Z[19];
int tabl[19];
void Zliczanie(double B1[], double B2[], int B3[])
{
int q=0;
for(int S=0;S<=19;S++)
{
for(int s=q; s<=19;s++)

{ double a=(B1[s]-B2[S])/B2[S];
if(a<0){a*=-1;};
if(a<0.000000001){B3[s]+=1;q=s;break;};}
}
}

/*{
//q=0;
int s=-1;
double a;
for(int S=0;S<=19;S++)
{
do
{
++s;
a=(B1[s]-B2[S])/B2[S];
if(a<0){a*=-1;};

}while(a>0.000000001);

++B3[s];
}
//q=s;
}*/


int main()
{
unsigned seed = static_cast<int> (chrono::system_clock::now().time_since_epoch().count());
mt19937 generator(seed);
uniform_int_distribution<int> distribution(1, 20);

for(int k=0;k<=0;k++) // nie dzialajaca pêtla, by zilustrowac jak jest iterowane wywolanie voida
{ // i zmieniajace siê wartoœci w tablicy Z[] przed kazda iteracji voida

for(int S=0;S<=19;S++)
{
n:
Z[S]=distribution(generator);
if(Z[S]<Z[S-1]){goto n;};
}


Zliczanie(liczby,Z,tabl);


for(int w=0;w<=19;w++)
{
cout<<liczby[w]<<" "<<tabl[w]<<" "<<Z[w]<<endl;
}
}

return 0;
}
Last edited on
What function are you trying to optimize?
Topic archived. No new replies allowed.