2010-07-19 63 views
0

你好,我最近問了一個有關如何執行以下問題:取所有的矩陣數字修訂

寫,總結所有的整數使用以下標題整數矩陣的功能:

const int SIZE = 4; 
double sumMatrix (const double m [] [SIZE] , int rowSize, int columnSize) ; 

寫一個測試程序,讀取一個4×4矩陣並顯示其所有元素的總和。繼承人樣品運行:

1 2 3 4 [Enter] 
5 6 7 8 [Enter] 
9 10 11 12 [Enter] 
13 14 15 16 [Enter] 
Sum of the matrix is 136 

我試圖用我所能的所有建議,但問題也許,我只是需要回到基本和學習一些:

按行輸入4by4矩陣行基本的東西,我跳過了,但繼承人到目前爲止。更正和解決方案,以及任何形式的幫助將受到高度讚賞。

#include <iostream> 
using namespace std; 

const int COLUMN_SIZE = 4; 

int sum(const int a[] [COLUMN_SIZE], int rowSize) 
{ 
    int total = 0; 
    for (int row = 0; row < rowSize; row++) 
    { 
     for (int column = 0; column < COLUMN_SIZE; column++) 
     { 
      total += a[row][column]; 
     } 
    } 

    return total; 
} 
int main() 
{ 
    int m[4][4]= 
    { 
     {1,2,3,4}, 
     {5,6,7,8}, 
     {9,10,11,12}, 
     {13,14,15,16} 
    }; 
    cout<< "Sum of the matrix"<< sum(m,4) << endl; 
    return 0; 
} 
+4

而你的問題是....? – 2010-07-19 21:56:53

+0

看起來不錯,但有點不清楚你要求什麼。你的程序是否產生正確的輸出? – Daniel 2010-07-19 21:57:30

+0

閱讀部分目前爲止我錯過了...... – schnaader 2010-07-19 21:59:19

回答

1

,你需要看起來幾乎一樣的打印功能讀取功能:

void read(const int a[] [COLUMN_SIZE], int rowSize) 
{ 
    for (int row = 0; row < rowSize; row++) 
    { 
     for (int column = 0; column < COLUMN_SIZE; column++) 
     { 
      cout << "Enter number for [" << row << "][" << column << "]: "; 
      cin >> a[row][column]; 
     } 
    } 
} 

(作爲一個註腳:如果你能概括的循環,並具有與您傳遞一個功能「讀'或'總和'你很好,你的方式變得很棒)

編輯:猜測我沒有閱讀'逐行'位的問題。聳肩。

0

三年後,哈哈,但你可以輸入cols元素,用空格分隔它們,並使用enter鍵來獲取其他行的元素。

改善您的代碼。只是看到和理解。說教程序比千言萬語更好:對

#include <iostream> 
using namespace std; 

const int COLUMN_SIZE = 4; 

int sum(const int a[] [COLUMN_SIZE], int rowSize) 
{ 
    int total = 0; 
    for (int row = 0; row < rowSize; row++) 
    { 
     for (int column = 0; column < COLUMN_SIZE; column++) 
     { 
      total += a[row][column]; 
     } 
    } 

    return total; 
} 

void showmatrix(const int a[] [COLUMN_SIZE], int rowSize, const char *name) 
{ 
    int row_index, col_index; 

    cout<< name << " [" << rowSize << "][" << COLUMN_SIZE << "] =" << endl; 

    for(row_index = 0; row_index < rowSize; row_index++) 
    { 
     for(col_index = 0; col_index < COLUMN_SIZE; col_index++) 
     { 
      if(col_index == 0) 
      { 
       if(row_index ==0) 
       { 
        cout<< "\t /"; 
       } 
       else if(row_index == rowSize - 1) 
       { 
        cout<< "\t \\"; 
       } 
       else 
       { 
        cout<< "\t|"; 
       } 
      } 

      cout<< "\t" << a[row_index][col_index]; 

      if(col_index == 3) 
      { 
       if(row_index ==0) 
       { 
        cout<< "\t\\"; 
       } 
       else if(row_index == rowSize - 1) 
       { 
        cout<< "\t/"; 
       } 
       else 
       { 
        cout<< "\t |"; 
       } 
      } 
     } 
     cout<< endl; 
    } 

    cout<< endl; 
} 

int main() 
{ 
    int m[4][4]= 
    { 
     {1,2,3,4}, 
     {5,6,7,8}, 
     {9,10,11,12}, 
     {13,14,15,16} 
    }; 

    int row_index, col_index; 

    cout<< "There is a stored matrix on that program. It is shown below: " << endl << endl; 

    showmatrix(m, 4, "Stored_Matrix"); 

    cout<< "The sum of elements of that stored matrix is: "<< sum(m,4) << endl << endl; 

    cout<< "Now, let's enter another 4 x 4 matrix." << endl << endl; 
    cout<< "(Note: You can use <space> character to separate column elements or <enter>" << endl; 
    cout<< "key to enter a new row.)" << endl; 
    cout<< "(Note 2: Really, you can use both to separate each one of the 16 matrix" << endl; 
    cout<< "elements, but do not enter more than that or the program will do some error)." << endl << endl; 

    for(row_index = 0; row_index < 4; row_index++) 
    { 
     for(col_index = 0; col_index < 4; col_index++) 
     { 
      cin>> m[row_index][col_index]; 
     } 
    } 

    cout<< endl; 

    cout<< "The entered matrix is below:" << endl << endl; 

    showmatrix(m, 4, "Entered_Matrix"); 

    cout<< "The sum of elements of that entered matrix is: "<< sum(m,4) << endl << endl; 

    //only to do not close console window on Windows 
    cout<< "Program ended." << endl << endl << "Press something to exit."; 
    cin.get(); 
    cin.get(); 

    return 0; 
} 

我希望你可以用它在你的重生或開發一個時間機器使用你的家庭作業2010這個想法之後。

當您爲該問題使用C++ Builder標記時,可以創建一個新的控制檯應用程序並複製粘貼上面的代碼覆蓋生成的代碼。使用F9編譯並運行。