chanboi - 1 year ago 73
C Question

# recursive counting on c

Good Day! I have a problem on counting in recursion, this code is already in the net, but i did some addition code on it, my problem is how to count the IF part of the program? , this is the code:

``````#include <stdio.h>
#include <math.h>

int combinationUtil(int arr[], int data[], int start, int end, int index, int r,float MEAN);

int printCombination(int arr[], int n, int r,float MEAN)
{

int data[r];

combinationUtil(arr, data, 0, n-1, 0, r,MEAN);
}

int combinationUtil(int arr[], int data[], int start, int end, int index, int r,float MEAN)
{
int j,i,a=1,b=0,f=0,ch,total=0;
float sum=0,mean,deviation=0,standard_deviation;

if (index == r){
int count=0;

for (j=0; j<r; j++)
{

sum=sum+data[j];
deviation=deviation+pow((MEAN-data[j]),2);
}

mean=sum/r;
standard_deviation=sqrt(deviation/(r-1));

if(MEAN<=(mean+(2.776*standard_deviation))&& MEAN>=(mean-(2.776*standard_deviation))){
printf("%d",a);
count++;}
else printf("%15d",b);
count=count;

return ;}

for (i=start; i<=end && end-i+1 >= r-index; i++)
{

data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r,MEAN);

}

}

int main()
{
int arr[] = {2,4,6,8,10,7,8};
int r = 3,i,count=1,total=0;
int n = sizeof(arr)/sizeof(arr[0]);
float sum=0,MEAN;
for(i=0; i<n; i++)
{
sum=sum+arr[i];
}
MEAN=sum/n;
printf("MEAN=%f\n\n\n",MEAN);

printCombination(arr,n,r,MEAN));

return 0;
}
``````

This is the output:

I think in order to count the IF segment, you should add the count variable as a reference parameter to the combinationUtil function, so you can get the result of the count out of the function.

``````#include <stdio.h>
#include <math.h>

int combinationUtil(int arr[], int data[], int start, int end, int index, int r, float MEAN, int *count);

int printCombination(int arr[], int n, int r,float MEAN, int *count)
{
int *data = malloc(sizeof(int) * r);

combinationUtil(arr, data, 0, n-1, 0, r,MEAN, count);

free(data);
return 0;
}

int combinationUtil(int arr[], int data[], int start, int end, int index, int r, float MEAN, int *count)
{
int j, i, a=1, b=0, f=0, ch, total=0;
float sum=0, mean, deviation=0, standard_deviation;

if (index == r) {

for (j=0; j<r; j++)
{
sum=sum+data[j];
deviation=deviation+pow((MEAN-data[j]),2);
}

mean=sum/r;
standard_deviation=sqrt(deviation/(r-1));

if(MEAN<=(mean+(2.776*standard_deviation))
&& MEAN>=(mean-(2.776*standard_deviation))){

printf("%d",a);
(*count)++;
}
else
printf("%15d",b);

return 1; // different return value
}

for (i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r,MEAN, count);
}
return 0;
}

int main()
{
int arr[] = {2,4,6,8,10,7,8};
int r = 3, i, count=0, total=0;
int n = sizeof(arr)/sizeof(arr[0]);
float sum = 0, MEAN;
for(i=0; i<n; i++)
{
sum = sum + arr[i];
}
MEAN = sum / n;
printf("MEAN=%f\n\n\n",MEAN);

printCombination(arr, n, r, MEAN, &count);

printf("\ncount = %d", count);

return 0;
}
``````

Edit: code is complete and only in C not in C++.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download