2017-10-08 15 views
2

剛剛學習C,我試圖瞭解如何使用2個函數得到數字的總和,但結果不正確。使用2個函數的數字總和

我想問用戶的10個數字存儲在函數主數組中。然後總和計算在一個單獨的函數中,然後顯示在main中。

這裏是我的原代碼,而多種功能的作品:

int main() 
{ 
    int n[10]; 
    int index; 
    int sum_n = 0; 
    int largest_n; 
    int smallest_n; 
    int *p; 
    p = &n[10]; 
    int a; 

    printf("Enter 10 Integers\n"); 

    for (index = 0; index < 10; index ++){ 
     scanf("%d", &n[index]); 

     sum_n += n[index]; 

    } 

    printf("The Sum of numbers is %d\n", sum_n); 

} 

下面是我試圖將其轉換爲功能,但是總和不工作:

int calculations (int); 

int main() 
{ 
    int n[10]; 
    int index; 
    int largest_n; 
    int smallest_n; 
    int *p; 
    p = &n[10]; 
    int a; 

    printf("Enter 10 Integers\n"); 

    for (index = 0; index < 10; index ++){ 
     scanf("%d", &n[index]); 
    } 
    if (n[index] = 10){ 
     //sum_n += n[index]; 
    printf("The Sum of numbers is %d\n",calculations(n[index])); 
    } 

&

int calculations (int num){ 

     int sum_n = 0; 

     sum_n += num; 

     return sum_n; 
} 

當我使用數字函數運行第二個程序1到10我越來越:

cmd image

我要麼做一些公然錯誤的或者不理解我在做什麼的。

回答

3
每次調用一個函數的函數內聲明的變量時

復位。 如果你想要一個變量,每次調用函數時都不會被重置,你可以簡單地將它變爲靜態的。

此外,你傳遞和參數n [10],但你的數組存儲數字從n [0]到n [9]。如果你想要所有十個數字的總和,那麼你必須爲每個數字調用計算函數,或者你可以傳遞整個數組。這裏是修改後的代碼。

#include<stdio.h> 
int calculations (int); 

int main() 
{ 
    int n[10]; 
    int index; 
    int largest_n; 
    int smallest_n; 
    int *p; 
    p = &n[10]; 
    int a; 
    int ans=0; 
    printf("Enter 10 Integers\n"); 

    for (index = 0; index < 10; index ++){ 
     scanf("%d",&n[index]); 
     ans = calculations(n[index]); 
     } 

    printf("The Sum of numbers is %d\n",ans); 
} 



int calculations (int num){ 

    static int sum_n; 

    sum_n += num; 

    return sum_n; 
} 
+0

謝謝 - 這工作,可以理解。 – Toblerone

1

您的功能calculations()只是返回其參數(0 + num只是num)。

聲明

int sum_n = 0; 

sum_n0調用它的每一次復位。

將此聲明從其中移出 - 直接撥入main()函數(並在致電calculations()之前)。

1

更正在下面的評論中提到。

int calculations (int *num){ //Should be a pointer or array eg. int num[] as you want to pass an array to this function 

     int sum_n = 0; 
     int i; 

     //Create loop here to iterate over array and sum elements 
     for(i=0; i<sizeof(num)/sizeof(int); i++) 
     sum_n+=num[i]; 

     return sum_n; 
} 

而且

if (n[index] = 10){ //This is logically incorrect. This should be if(index==10). 
        // n[index]=10 will assign 10 to a[10] and if will still pass as if(10) is true but make a note of it. Don't use assignment operator inside if, you need comparison operator `==`   

    printf("The Sum of numbers is %d\n",calculations(n[index])); //You should call calculations like this -> calculations(n). You should pass whole array not just an element. 
    } 
+0

感謝您的幫助,第一部分循環初始聲明只允許在C99或C11模式下使用| – Toblerone

+0

好的,爲了解決這個問題,在循環外聲明'i'。 'int i;'上面的函數。這是我的錯誤。 C++允許initalization在循環內,但'C'不能。讓我更新答案。 – batMan

2

首先,你不需要爲arraysum這段代碼,第二永遠記住檢查什麼返回由scanf .The代碼非常簡單。「對於」:意見是可以理解的,但我試圖用此方法,並不斷獲得「」 -

main

int main() 
    { 
    int n; 
    int sum=0; 

    printf("Enter 10 Integers\n"); 

    for (int index = 0; index < 10; index ++){ 
     if(scanf("%d", &n)) 
      sum+=n; 
    } 
    printf("The Sum of numbers is %d\n",sum);//calculations(n)); 

} 

其次使用功能calculation

int sum=0; 

    void calculation(int num){ 
     sum+= num; 
    } 

int main() 
{ 
    int n; 
    printf("Enter 10 Integers\n"); 

    for (int index = 0; index < 10; index ++){ 
     if(scanf("%d", &n)) 
      calculation(n); 
    } 
    printf("The Sum of numbers is %d\n",sum);//calculations(n)); 

    }