2016-01-12 85 views
0

美好的一天!我在計算遞歸時遇到了一個問題,這段代碼已經在網絡中,但我做了一些額外的代碼,我的問題是如何計算程序的IF部分? ,這是代碼:c遞歸計數

#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; 
} 

這是輸出:

enter image description here

+0

」代碼已經在網絡中「。咦? – Drop

+0

「來計算程序的IF部分」你如何計算程序的一部分? – Drop

+0

@Drop我猜OP意味着他從互聯網上獲得了代碼。 – user3437460

回答

1

我想以計數的IF段,你應該添加計數變量作爲引用參數的combinationUtil函數,所以你可以從函數中獲得計數的結果。

#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; 
} 

編輯:代碼完成並只在C不是在C++。 「

+0

這就是說,如果你想獲得主要功能的計數,否則你應該更具體。 – Artwo

+0

謝謝artwo,我會嘗試這個代碼 – chanboi

+0

抱歉標記C++,我認爲c和C++有點相同,所以我也標記C++ – chanboi