I have a problem with a matrix program

Hi, i have a problem to solve this :
Write two subprograms, one that determines the minimum element of a linear integer array and another that determines the maximum element of the array.Use this subprograms to find the element that is simultaneous smallest on its line and the biggest on its column in a matrix.

For example in :
1 2 3
4 5 6
7 8 9
The right element is 7.

Here is the program attempt :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cmath>
#include <string>
#include <climits>

using namespace std;

void citire_vector(int a[], int x)
{
   for(int i = 0; i < x; ++i)
        {
          cout << "a[" << i << "] : "; cin >> a[i];
        }
}
//-----------------
void afisare_vector(int a[], int x)
{
      for(int i = 0; i < x; ++i)
        {
          cout <<a[i]<<endl;
        }
}
//-----------------
int minn(int a[], int x)
{
       int temp = a[0];
      for(int i = 0; i < x; ++i)
        {
          if(temp>a[i]) {
         temp=a[i];
         }
        }
     return temp;
}
//-----------------
int maxx(int a[], int x)
{
     int temp = a[0];
      for(int i = 0; i < x; ++i)
        {
          if(temp<a[i]) {
         temp=a[i];
         }
        }
       return temp;
}
//-----------------
void matrice_read(int m[][100], unsigned r, unsigned c)
{
      for(int i = 0; i < r; ++i)
        for(int j = 0; j < c; ++j) {
            cout << "a[" << i << "][" << j << "] = "; cin >>m[i][j];
        }
}
//-----------------
void matrice_display(int m[][100], unsigned r, unsigned c)
{
      for(int i = 0; i < r; ++i) {
        for(int j = 0; j < c; ++j) cout << m[i][j] << " ";
        cout << "\n";
    }

}
//-----------------
void principal(int m[][100], unsigned r, unsigned c)
{
       int miny;
       int maxy;
      for(int i = 0; i < r; ++i)
       {
           miny = INT_MAX;
           maxy = INT_MIN;
        for(int j = 0; j < c; ++j) 
        {
           if(m[i][j]<miny){
               miny = m[i][j];
           }
           if(m[j][i]>maxy)
           {
               maxy = m[j][i];
           }
        }
        cout<<miny<<" "<<maxy<<" ";
         if(miny == maxy)
           {
               cout<< "The right element is : "<<miny<<endl;
           }
       }  
}
int main()
{
    int a[100];
    int m[100][100];
    unsigned n;
    unsigned r,c;
    cout<< "Rows: "; cin>>r;
    cout<< "Columns : "; cin>>c;
    matrice_read(m,r,c);
    matrice_display(m,r,c);
    principal(m,r,c);
}


i want to make it using the 2 subprograms(minn,maxx) but i cant figure it out how
Last edited on
blaster123 wrote:
find the element that is simultaneous smallest on its line and the biggest on its column in a matrix.


So what would you answer for this matrix?
1 2 3
4 5 6
9 8 1
So what would you answer for this matrix?

0 elements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

const int SIZE = 10;    // or whatever

//-----------------

int rowMin( int a[SIZE][SIZE], int rows, int cols, int r )
{
   int result = a[r][0];
   for ( int j = 0; j < cols; j++ ) if ( a[r][j] < result ) result = a[r][j];
   return result;
}

//-----------------

int colMax( int a[SIZE][SIZE], int rows, int cols, int c )
{
   int result = a[0][c];
   for ( int i = 0; i < rows; i++ ) if ( a[i][c] > result ) result = a[i][c];
   return result;
}

//-----------------

int main()
{
   int m[SIZE][SIZE] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// int m[SIZE][SIZE] = { { 1, 2, 3 }, { 4, 5, 6 }, { 9, 8, 1 } };
// int m[SIZE][SIZE] = { { 2, 2, 2 }, { 2, 2, 2 }, { 2, 2, 2 } };
   int rows = 3, cols = 3;
   cout << "Elements satisfying condition: ";
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ )
      {
         if ( m[i][j] == rowMin( m, rows, cols, i ) && m[i][j] == colMax( m, rows, cols, j ) ) cout << m[i][j] << ' ';
      }
   }
}


Elements satisfying condition: 7 
it would be slightly less work to do your row min and feed the result (the column where min is) of that to colmax. Then its just for each row, the inner for each col is redundant. I think?
Last edited on
Topic archived. No new replies allowed.