Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรม C++ หาค่าสูงสุดของแต่ละแถวในเมทริกซ์

พิจารณาว่าเรามีเมทริกซ์ งานของเราคือหาสมาชิกสูงสุดของแต่ละแถวของเมทริกซ์นั้นและพิมพ์ออกมา งานนี้ง่าย สำหรับแต่ละแถว รีเซ็ตค่าสูงสุด และหาสมาชิกสูงสุด แล้วพิมพ์ มาดูโค้ดเพื่อความเข้าใจที่ดีกว่า

ตัวอย่าง

#include <iostream> 
#define MAX 10 
using namespace std; 

void largestInEachRow(int mat[][MAX], int rows, int cols) { 
    for (int i = 0; i < rows; i++) { 
        int max_row_element = mat[i][0]; 
        for (int j = 1; j < cols; j++) { 
            if (mat[i][j] > max_row_element) 
                max_row_element = mat[i][j]; 
        } 
        cout << max_row_element << endl; 
    } 
} 

int main() { 
    int row = 4, col = 4; 
    int mat[][MAX] = { { 3, 4, 1, 81 }, 
        { 1, 84, 9, 11 }, 
        { 23, 7, 21, 1 }, 
        { 2, 1, 44, 5 } 
    }; 
    largestInEachRow(mat, row, col); 
}

ผลลัพธ์

81 84 23 44