This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//804 | |
#include<stdlib.h> | |
#include<stdio.h> | |
double average(double*, int);//宣告副程式average是一個雙精浮點數 | |
//使用時要回傳 一個雙精浮點數的記憶體位址和一個整數 | |
int main() { | |
int i;//宣告整數 | |
double data[6];//宣告雙精浮點數陣列 | |
for (i = 0; i<6; i++) {//迴圈0~5 共會執行6次 | |
printf("請輸入第%d個浮點數:", i + 1);//顯示文字和整數i | |
scanf("%lf", &data[i]);//輸入雙精浮點數,取得雙精浮點數的記憶體位址 | |
} | |
printf("\n您輸入的陣列值如下\n");//顯示文字 | |
for (i = 0; i<6; i++)//迴圈0~5 共會執行6次 | |
printf("\ndata[%d]:%.2f", i, data[i]);//列印 換行符號+data陣列的第i個 | |
printf("\n平均:%.2f", average(data, 6));//呼叫並列印average變數 和文字 | |
system("PAUSE");//停住等待使用者按下任意一個按鍵 | |
return 0;//跳出 | |
} | |
double average(double* arr2, int n) {//回傳記憶體位址,回傳 輸入的雙精浮點數個數 | |
double tot = 0;//宣告雙精浮點數 | |
int i;//宣告整數 | |
for (i = 0; i<n; i++) | |
tot += *(arr2 + i);//tot =tot + 取出 (arr2所代表的記憶體位址+1)的內容 | |
return tot / n;//tot除以n (計算平均值) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment