2015-04-03 42 views
1

我試圖讓程序找到數組第二列中的最大值並打印此值(下面的工作程序),但然後在同一行中打印另一個對應的值。查找陣列中的列中的最大值並在同一行中打印其他對應的值

#include<iostream>; 

using namespace std; 

int main() 
{ 
    float array[3][2] = { { 16, 22 }, { 258, 1 }, { 42, 54 } }; 
    int i; 
    float max = 0; 

    for (i = 0; i<3; i++) 
    { 
     if(array[i][1] > max) 
     { 
     max = array[i][1]; 
     } 
    } 

    cout << "The maximum value in the array is " << max << endl; 
    return 0; 
} 
+5

除了保存最大值,還保存*索引*。 – 2015-04-03 17:01:58

+0

下次在發佈之前請正確格式化您的代碼,謝謝 – 2015-04-03 17:10:03

回答

1

將評論由@JoachimPileborg轉換爲答案。

  1. 除了保存最大值以外,還保存找到最大值的索引。

  2. 從索引打印行的值。


int main() 
{ 
    float array[3][2] = { { 16, 22 }, { 258, 1 }, { 42, 54 } }; 
    int i; 
    float max = 0; 
    int maxValueIndex = 0; 

    for (i = 0; i<3; i++) 
    { 
     if(array[i][1] > max) 
     { 
     max = array[i][1]; 
     maxValueIndex = i; 
     } 
    } 

    cout << "The maximum value in the array is " << max << endl; 
    cout << "The other value in the array is " << array[maxValueIndex][0] << endl; 
    return 0; 
} 
0

您還需要找到索引i,它對應於最大元素。之後,您可以在for循環中使用cout

#include<iostream>; 


using namespace std; 


int main() 

{ 

float array[3][2] = { { 16, 22 }, { 258, 1 }, { 42, 54 } }; 
int i; 
int indmax; 
float max = 0; 

for (i = 0; i<3; i++) 
{ 

    if(array[i][1] > max) 
    { 
     max = array[i][1]; 
     indmax=i; 
    } 

} 
cout << "The maximum value in the array is " << max << endl; 
cout << "The row containing max values: "; 
for(int j=0;j<2;j++) 
    cout << array[indmax][j] << " "; 
cout << endl; 
return 0; 
} 
1

您可以簡單地存儲行索引:

#include <iostream>; 
using namespace std; 

int main() 
{ 
    float array[3][2] = { { 16, 22 }, { 258, 1 }, { 42, 54 } }; 
    int i; 
    float max = 0; 
    int row = 0; 

    for (i = 0; i < 3; i++) 
    { 
     if (array[i][1] > max) 
     { 
      max = array[i][1]; 
      row = i; 
     } 
    } 

    cout << "The maximum value in the array is " << max << endl; 
    cout << "The other corresponding value in the same row is " << array[row][0] << endl; 

    return 0; 
} 

請注意,您的代碼發現最大假定所有正值。如果不是的話,你應該使用此代碼來代替:

#include <iostream>; 
using namespace std; 

int main() 
{ 
    float array[3][2] = { { 16, 22 }, { 258, 1 }, { 42, 54 } }; 
    int i; 
    float max = array[0][1]; 
    int row = 0; 

    for (i = 1; i < 3; i++) 
    { 
     if (array[i][1] > max) 
     { 
      max = array[i][1]; 
      row = i; 
     } 
    } 

    cout << "The maximum value in the array is " << max << endl; 
    cout << "The other corresponding value in the same row is " << array[row][0] << endl; 

    return 0; 
} 
0

隨着標準算法std::max_element

#include <algorithm> 
#include <iostream> 

int main() 
{ 
    float array[3][2] = { { 16, 22 }, { 258, 1 }, { 42, 54 } }; 

    auto it = std::max_element(std::begin(array), std::end(array), 
    [](const auto& lhs, const auto& rhs) { return lhs[1] < rhs[1]; }); 

    std::cout << "The maximum value in the array is " << (*it)[1] << std::endl; 
    std::cout << "other element in the array is " << (*it)[0] << std::endl; 
} 

Live demo

注意:代碼是C++ 14,但也可以在之前的標準中重寫。

相關問題