Smallest element in each row of matrix C++

815 views Asked by At

This code can only return one smallest element in a matrix, but how if I want to return smallest element in each row? I need to use recursive function in C++. Thanks for your help

#include<iostream>
using namespace std;
int smallest(int** arr, int rows, int columns, int column_index = 0)
{
    if (rows <= 0 || column_index >= columns)
        return INT_MAX;


    if (rows == 1)
        return min(*(*arr + column_index),       
            smallest(arr, 1, columns - 1,
                column_index + 1)); 


    return min(smallest(arr, 1, columns), 
        smallest(arr + 1, rows - 1, columns));
}
int main()
{
    int row, col, index=0;
    cin >> row;
    cin >> col;
    int** arr;
    arr = new int* [row];
    for (int i = 0; i < row; i++) {
         arr[i] = new int[col];
         for (int j = 0; j < col; j++) {
             cin >> arr[i][j];
           }
        }
    cout<<smallest(arr, row, col, index);
    return 0;
}
2

There are 2 answers

0
brc-dd On BEST ANSWER

I think this much code will be sufficient if you use standard algorithm - std::min_element:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    int r, c;
    std::cin >> r >> c;
    std::vector<std::vector<int>> mat(r, std::vector<int>(c));
    for (auto &&row : mat)
        for (auto &&ele : row)
            std::cin >> ele;
    for (auto &&row : mat)
        std::cout << *std::min_element(row.begin(), row.end()) << std::endl;
}

If you want to do it your way (old school style, using recursion) then do something like this. You just need to fix the index of row while calling smallest. Below is some self-explanatory code :

#include <algorithm>
#include <iostream>

// here row_index represents the index of row and col represents the number of
// elements in that row which are not yet traversed (counted from beginning)
int smallest(int **arr, int row_index, int col) {

    // only first element is not traversed
    if (col == 1)
        return arr[row_index][0];

    // return minimum of last element and value returned by recursive call for
    // first col - 1 elements
    return std::min(arr[row_index][col - 1], smallest(arr, row_index, col - 1));
}

int main() {
    int row, col;
    std::cin >> row;
    std::cin >> col;
    int **arr = new int *[row];
    for (int i = 0; i < row; i++) {
        arr[i] = new int[col];
        for (int j = 0; j < col; j++)
            std::cin >> arr[i][j];
    }

    // call the function for each row
    for (int i = 0; i < row; i++)
        std::cout << "Smallest element in row " << i + 1 << " : "
                  << smallest(arr, i, col) << '\n';
}
1
Vlad from Moscow On

You can write a recursive function that finds the smallest element in a one-dimensional array that will be called for each "row" of an array of arrays or of a two-dimensional array.

Here is a demonstrative program.

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <ctime>

const int * smallest( const int *a, size_t n )
{
    return n < 2 ? a  
                 : std::min( a, smallest( a + 1, n - 1 ), 
                             []( const int *p1, const int *p2 )
                             {
                                return not ( *p2 < *p1 );
                             } );
}

int main() 
{
    std::srand( ( unsigned int )std::time( nullptr ) );
    size_t rows, cols;
    
    std::cin >> rows >> cols;
    
    int **a = new int *[rows];
    
    for ( size_t i = 0; i < rows; i++ )
    {
        a[i] = new int[cols];
        for ( size_t j = 0; j < cols; j++ )
        {
            a[i][j] = std::rand() % ( rows + cols );
        }
    }
    
    for ( size_t i = 0; i < rows; i++ )
    {
        for ( size_t j = 0; j < cols; j++ )
        {
            std::cout << std::setw( 2 ) << a[i][j] << ' ';
        }
        std::cout << '\n';
    }
    std::cout << '\n';
    
    for ( size_t i = 0; i < rows; i++ )
    {
        std::cout << std::setw( 2 ) << *smallest( a[i], cols ) << ' ';
    }
    std::cout << '\n';
    
    for ( size_t i = 0; i < rows; i++ )
    {
        delete [] a[i];
    }
    
    delete [] a;
}   

If to enter the numbers of rows and columns equal to 10 then the program output might look like

 1 16  7  6  2  7  1 14  3  8 
 0 14  9  0  6 18 18  7  7 19 
12 17  9 12 14 10  7  9 15  3 
14  8 19 13 14  1 12 15  7 15 
16  7  1 17 19  8 15 18  7 15 
 9 19 12 10  3 18  0 10  7  8 
 6 13 16 17  7  3 19 19 18  6 
 7 14  6  8  3 17  8 19  7 16 
 6 16  7 10 19 11  1 19 13  8 
19 19 14  8 17  1 11  8 12  1 

 1  0  3  1  1  0  3  3  1  1 

Pay attention to that the function should return a pointer to a smallest element because in general the function can be called with the second argument equal to 0. In this case returning any integer value does not make a sense because it can coincide with an actual value.