| 
鐵幣1857 元文章583 篇聲望1868 枚上次登入18-9-22精華0註冊時間07-10-1UID452552
 
 | 
        
        
10鐵幣 
| 請大大幫一下忙,我遇到了瓶頸。 
 題目一:
 
 改寫7.4節程式Array2Fnc.cpp,以求取程式中所定義的矩陣A[2][3]的最大元素值。
 
 
 題目二:
 
 提示:可以參照7.2節ArrayFnc.cpp中函數MaxElem()的演算法,並將其改寫成一個可供主程式呼叫的函數。
 
 將問題7中的程式進一步改寫,使其同時能夠輸出最大元數的兩個下標值。
 
 
 -------------------------------------------------------------------------------------------------------------------
 由於參考程式根本看不懂,以及參考的程式好像怪怪的,導致我根本不知如何下手。
 請大大幫幫忙,教導一下。
 
 
 參考7.2節程式如下↓
 
 #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;
 }
 // -----------------------------------
 double Average(double X[], int M)
 {
 double Sum = 0;
 for (int i = 0; i < M; i++)
 Sum += X;
 return Sum/double(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;
 }
 
 參考7.4節程式如下↓
 
 #include <iomanip>
 #include <iostream>
 using namespace std;
 const int Row = 2;
 const int Col = 3;
 void  ShowMatrix(float B[][Col]);
 float MatrixAvg(float [ ][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;
 }
 // -----------------------------------
 void ShowMatrix(double B[][Col])
 {
 for (int i=0; i< Row; i++)
 {
 for (int j=0; j< Col; j++)
 cout << setw(5) << B[j];
 cout << endl;
 }
 cout << endl;
 return;
 }
 // -----------------------------------
 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);
 }
 
 [ 本文章最後由 滅‧愛 於 08-12-23 19:57 編輯 ]
 | 
 
 |