Saddle point- Class - Garbage value

Ques. To find saddle point of the matrix using classes

Doubt. The function display giving garbage value of the contents of matrix. Why?

Code :

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
#include<iostream>
using namespace std;

class Saddle
{
private:
int matrix[3][3];
int rno;
int cno;	


public:
void display()
{
	int i,j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<matrix[i][j]<<" ";	
  }  
  }
Saddle(int n)
{
int i,j;	
int **matrix = new int*[3];
for(i = 0; i<3; i++)
matrix[i]	= new int[3];
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matrix[i][j];	
  }
  
//for(i=0; i<3; i++)
//{
//for(j=0; j<3; j++)
////cout<<matrix[i][j]<<" ";	
 // }  
// cout<<endl; 
//rno = -1;
//cno = -1;  
}


void findSaddlePoint(Saddle &s)	
{

int i, j, k, min_row, col_index;
cout<<s.matrix[1][1]	;
for(i=0; i<3; i++)
{
min_row = s.matrix[i][0];
col_index = 0;
for(j=0 ;j<3 ;j++)
{
if(min_row>s.matrix[i][j])	
{
min_row = s.matrix[i][j];
col_index = j;	
}
}
cout<<endl<<min_row<<" "<<col_index;
for(k=0; k<3; k++)
{
if(min_row<s.matrix[k][col_index])
break;
//cout<<"No saddle point found"<<endl;
//else
//cout<<"Saddle point = "<<min_row;	
if(k==3)
cout<<"saddle point "<<min_row<<endl;
}
//cout<<"No saddle point";
	}	
}
};

int main()
{
Saddle s(3);
s.display();
s.findSaddlePoint(s);
return 0;	
}
Last edited on
hello there, i think the problem is on line 25.
you're creating a new variable called "matrix" overshadowing your class variable "SaddleObject.matrix" try changing your constructor code to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Saddle(int n) //not sure what n means.
{
int i,j;	
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matrix[i][j];	
  }
  
//for(i=0; i<3; i++)
//{
//for(j=0; j<3; j++)
////cout<<matrix[i][j]<<" ";	
 // }  
// cout<<endl; 
//rno = -1;
//cno = -1;  
}
The question says that the 2d matrix is to be allocated space dynamically. The exact words are : A parameterised constructor which takes the size n (as parameter), allocates dynamic space for the array, and puts values in it from the keyboard. It also initializes the data members (b) and (c) above to a value -1, indicating that the saddle has not yet been determined.
n is size of square matrix. At the present I have fixed it to 3.

@Yolanda, you are right, the problem is of "matrix" overshadowing class variable. But I need to allocate memory dynamically for the array. What shall I do?
Last edited on
if that the case then all you need to do is not create a new variable

change line 7 to a pointer to pointer
 
int **matrix = nullptr;/// i init matrix to a null pointer 


change the constructor definition to.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Saddle(int n)
 :rno(-1),cno(-1) /// list initializers are prefered
{
  int i,j;
  matrix = new int*[n];

  for(i = 0; i<n; i++)
    matrix[i]	= new int[n];
  
  for(i=0; i<n; i++)
   {
       for(j=0; j<n; j++)
       cin>>matrix[i][j];
   }

}



advice - now that you have decided to use dynamic memory, you also
have to handle it's de-allocation to avoid memory leak, you should
have a deconstructor ~Saddle() defined
Last edited on
Topic archived. No new replies allowed.