2013-10-24 16 views
-1

經過多次嘗試並反覆思考之後,感覺就像一個沮喪的人。 我在這裏得到你們的一些建議..2D(每列最多)

其實我試圖找到每個行和列的最大值。 所以通過使用分治技術,我編寫了兩個單獨的函數,一個用於查找每行的最大值並將其存儲到我用作參數的行向量中。分別爲列。

void maxValuesR(int a[][cols], int rv[], int row) 
void maxValuesC(int a[][cols], int cv[], int row) 

的問題:代碼甚至無法編譯,我只是不understnd錯誤.. 請幫助..

我真的需要你的幫助這裏!

的代碼如下:

#include <iostream> 
using namespace std; 
const int rows = 2;  // Declared As Global Variable 
const int cols = 3; // Declared As Global Variable 

void getData(int arr[][cols], int rows) 
{ 
    for(int i = 0; i < rows; i++) 
    { 
     for(int j = 0; j < cols; j++) 
     { 
      cout << "Enter Element: "; 
      cin >> arr[i][j]; 
     } 
    } 

} 

void printData(int a[][cols], int rows) 
{ 
    for(int i = 0; i < rows; i++) 
    { 
     for(int j = 0; j < cols; j++) 
     { 
      cout << a[i][j] << "\t"; 
     } 
     cout << endl; 
    } 
} 

void print_single(int a[], int row) 
{ 
    for(int i = 0; i < row; i++) 
    { 
     cout << a[i] << "\t"; 
    } 
} 

void maxValuesR(int a[][cols], int rv[], int row) 
{ int foxi; 
    for(int i = 0; i < row; i++) 
    { 
     foxi = a[i][0]; 
     for(int j = 0; j < cols; j++) 
      { 

       if(foxi < a[i][j]) 
       { 
        foxi = a[i][j]; 
       } 
      } 
      rv[i] = foxi; 
    } 
} 

void maxValuesC(int a[][cols], int cv[], int row) 
{ 
    int maxi; 
    for(int i = 0; i < cols; i++) 
    { 
     maxi = a[0][i]; 
      for(int j = 0; j < row; j++) 
      { 

       if(maxi > a[j][i]) 
       { 
        maxi = a[j][[i];// show error here => expected a '{' introducing lambda body 
       } 
      } 
      cv[i] = maxi; 
    } 
} 

int main() 
{ 
    int rowVector[rows]; 
    int colVector[cols]; 
    int a[rows][cols]; 

    cout << "Fill Array_1. " << endl; 
    getData(a, rows); 

    cout << "Array_1." << "\n\n"; 
    printData(a, rows); cout << endl; 

    maxValuesR(a, rowVector, rows); 
    print_single(rowVector, rows); 

    cout << "\n\n"; 

    maxValuesC(a, colVector, rows); 
    print_single(colVector, rows); 

    return 0; 
} 
+0

那麼你會得到什麼樣的編譯錯誤? – codeling

+0

很難理解你看不到的錯誤。 –

+0

你的錯誤是什麼?爲什麼不使用'std :: array'或'std :: vector'而不是C類數組?爲什麼你使用相同的名稱常量和局部變量?這很難遵循。 –

回答

1

爲了澄清編譯錯誤:

  • 你忘了包括(或沒有在這裏不寫?)#include <iostream>
  • 你沒」 t指定cin,coutendl的命名空間(它們在std名稱空間中)
  • 你有一個多餘的「[」在聲明中a[j][[i],通過所有的可能性,你想寫a[j][i]

的編譯代碼如下所示:

#include <iostream> 

const int rows = 2;  // Declared As Global Variable 
const int cols = 3; // Declared As Global Variable 

void getData(int arr[][cols], int rows) 
{ 
    for(int i = 0; i < rows; i++) 
    { 
     for(int j = 0; j < cols; j++) 
     { 
      std::cout << "Enter Element: "; 
      std::cin >> arr[i][j]; 
     } 
    } 

} 

void printData(int a[][cols], int rows) 
{ 
    for(int i = 0; i < rows; i++) 
    { 
     for(int j = 0; j < cols; j++) 
     { 
      std::cout << a[i][j] << "\t"; 
     } 
     std::cout << std::endl; 
    } 
} 

void print_single(int a[], int row) 
{ 
    for(int i = 0; i < row; i++) 
    { 
     std::cout << a[i] << "\t"; 
    } 
} 

void maxValuesR(int a[][cols], int rv[], int row) 
{ int foxi; 
    for(int i = 0; i < row; i++) 
    { 
     foxi = a[i][0]; 
     for(int j = 0; j < cols; j++) 
      { 

       if(foxi < a[i][j]) 
       { 
        foxi = a[i][j]; 
       } 
      } 
      rv[i] = foxi; 
    } 
} 

void maxValuesC(int a[][cols], int cv[], int row) 
{ 
    int maxi; 
    for(int i = 0; i < cols; i++) 
    { 
     maxi = a[0][i]; 
      for(int j = 0; j < row; j++) 
      { 

       if(maxi > a[j][i]) 
       { 
        maxi = a[j][i]; 
       } 
      } 
      cv[i] = maxi; 
    } 
} 

int main() 
{ 
    int rowVector[rows]; 
    int colVector[cols]; 
    int a[rows][cols]; 

    std::cout << "Fill Array_1. " << std::endl; 
    getData(a, rows); 

    std::cout << "Array_1." << "\n\n"; 
    printData(a, rows); 
    std::cout << std::endl; 

    maxValuesR(a, rowVector, rows); 
    print_single(rowVector, rows); 

    std::cout << "\n\n"; 

    maxValuesC(a, colVector, rows); 
    print_single(colVector, rows); 

    return 0; 
} 

就無法知道它產生的輸出你想,但是,因爲你沒有指定任何示例輸入(更不用說相應的預期輸出)...

+0

先生我已經包括isostream我忘了在這裏添加.. – usman

+0

現在的問題是什麼?檢查我的第三點 - 看起來你沒有真正閱讀我的答案?另請參閱我發佈的代碼 - **我在上面的答案中給出的完整代碼編譯**!我建議嘗試一下嗎?然後如果它不能按預期工作,請創建一個新問題幷包括:**您輸入給程序的輸入,輸出結果以及輸出結果** – codeling

+0

ahh請接受我的道歉。 – usman