Skip to content

Instantly share code, notes, and snippets.

Created March 27, 2019 14:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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]);//輸入雙精浮點數,取得雙精浮點數的記憶體位址
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變數 和文字
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