//第一個
#include <iostream>
using namespace std;
double Average(double [], int);
double MaxElem(double [], int);
int main(){
const int Size = 5;
double P[Size] = {48.4, 39.8, 40.5, 42.6, 41.2};
P[0] = 3.2;
P[3] = P[0]*2.0;
cout << "陣列 P 的平均值是:"<< Average(P, Size) << endl;
cout << "陣列 P 的最大值是:"<< MaxElem(P, Size) << endl;
system("PAUSE");
}
double Average(double X[], int M){
double Sum = 0;
for (int i = 0; i < M; i++){
Sum += X;
}
return Sum/M;
}
double MaxElem(double Y[], int N){
double MaxE;
MaxE = Y[0];
for (int i = 1; i < N; i++ ){
if (MaxE < Y){
MaxE = Y;
}
}
return MaxE;
}
//第二題
#include <iomanip>
#include <iostream>
using namespace std;
const int Row = 2;
const int Col = 3;
void ShowMatrix(double B[][Col]);
double MatrixAvg(double [ ][Col]);
int main(){
double A[Row][Col]={ 1.8, 4.9, 6.8,
6.2, 2.1, 3.4};
cout << " 陣列 A 是: " << endl;
ShowMatrix(A);
cout << " 陣列 A 的平均值是: " << MatrixAvg(A) << endl;
cout << " 直接將 A 輸出的結果是:" << A << endl;
system("PAUSE");
}
void ShowMatrix(double B[][Col]){
for (int i=0; i< Row; i++)
{
for (int j=0; j< Col; j++){
cout << setw(5) << B[ i ] [ j ];
}
cout << endl;
}
cout << endl;
}
double MatrixAvg(double M[ ][Col]){
double Sum = 0;
for (int i=0; i< Row; i++){
for (int j=0; j< Col; j++){
Sum+= M[j];
}
}
return Sum / double(Row*Col);
}
歡迎光臨 鐵之狂傲 (https://www.gamez.com.tw/) |