Skip to content

Instantly share code, notes, and snippets.

@ruyut
Created March 27, 2019 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruyut/36844cad0e794c0b3b7eb31a59357612 to your computer and use it in GitHub Desktop.
Save ruyut/36844cad0e794c0b3b7eb31a59357612 to your computer and use it in GitHub Desktop.
//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