鐵之狂傲

 取回密碼
 註冊
搜尋

切換到指定樓層
1#
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 編輯 ]

 

私は1人の平凡な人で、どうぞご指導下さい。



平凡な世界もとても楽しくなることができます。

轉播0 分享0 收藏0

回覆 使用道具 檢舉

全世界最先進的跳動筆

程式碼好像真的蠻多問題的  =    =a

可否說一下書名  還蠻好奇的   Orz

程式碼稍微改寫了一下   

但因為沒有輸  不知道答案是甚麼....所以執行結果.....(汗

第一題應該是這樣沒錯   但是第二題好像是顯示記憶體位子


//第一個

#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);
}


有問題再問吧  @@

剛有個地方打錯  Orz|||

[ 本文章最後由 風痕戀 於 08-12-21 11:48 編輯 ]
 

回覆 使用道具 檢舉

//第一個


  1. #include <iostream>
  2. using namespace std;
  3. double Average(double [], int);
  4. double MaxElem(double [], int);

  5. int main(){
  6.   const int Size =  5;
  7.   double P[Size]  = {48.4, 39.8, 40.5, 42.6, 41.2};
  8.     P[0] = 3.2;
  9.     P[3] = P[0]*2.0;
  10.    
  11.     cout  << "陣列 P 的平均值是:"<< Average(P, Size) << endl;
  12.     cout  << "陣列 P 的最大值是:"<< MaxElem(P, Size) << endl;
  13.    
  14.     system("PAUSE");
  15. }

  16. double Average(double X[], int M){
  17.     double Sum = 0;
  18.     for (int i = 0; i < M; i++)
  19.     {
  20.         Sum+=X[i];
  21.     }
  22.     return Sum/M;     
  23. }

  24. double MaxElem(double Y[], int N){
  25.    double MaxE;
  26.    MaxE = Y[0];
  27.   
  28.    for (int i = 1; i < N; i++ ){
  29.      if (MaxE < Y[i]){
  30.         MaxE = Y[i];}
  31.    }
  32.   return MaxE;  
  33. }
複製代碼


//第二個

  1. #include <iomanip>
  2. #include <iostream>
  3. using namespace std;
  4. const int Row = 2;
  5. const int Col = 3;
  6. void  ShowMatrix(double B[][Col]);
  7. double MatrixAvg(double [ ][Col]);

  8. int main(){
  9.   double A[Row][Col]={ 1.8, 4.9, 6.8,
  10.                  6.2, 2.1, 3.4};
  11.     cout  << " 陣列 A 是: "  << endl;
  12.     ShowMatrix(A);
  13.     cout  << " 陣列 A 的平均值是:"  << MatrixAvg(A) << endl;
  14.     cout  <<  "直接將 A 輸出的結果是: "<< A << endl;
  15.     system("PAUSE");
  16. }

  17. void ShowMatrix(double B[][Col]){
  18.   for (int i=0; i< Row; i++)
  19.     {
  20.       for (int j=0; j< Col; j++){
  21.          cout << setw(5) << B[ i ] [ j ];
  22.       }
  23.       cout << endl;
  24.     }
  25.   cout << endl;
  26. }

  27. double MatrixAvg(double M[ ][Col]){
  28.   double Sum = 0;
  29.   for (int i=0; i< Row; i++){
  30.     for (int j=0; j< Col; j++){
  31.         Sum+= M[i][j];
  32.     }
  33.   }
  34.   return  Sum / double(Row*Col);
  35. }
複製代碼


大大你省略很多地方喔!(導致錯誤)

 

回覆 使用道具 檢舉

原文由 滅‧愛 於 08-12-23 12:26 發表
恕略


程式碼直接貼上鐵傲時....沒有注意到很多字都被削掉了 Orz

大概是因為網頁語法的關係吧   (汗
 

回覆 使用道具 檢舉

你需要登入後才可以回覆 登入 | 註冊

存檔|手機版|聯絡我們|新聞提供|鐵之狂傲

GMT+8, 24-5-3 06:02 , Processed in 0.034256 second(s), 14 queries , Gzip On.

回頂部