infinite loop

Write your question here.
on executing the program it goes to infinite loop,where should i make correction with respect to my program?
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  #include<iostream.h>
class matrix
{
   int m[10][10];
   int r;
   int c;
   public:
   void display();
   void input();
   int getr()
{
  return(r);
}
int getc()
{
 return(c);
}

   matrix add(matrix m2);
   matrix sub(matrix m2);
   matrix mul(matrix m1,matrix m2);
   matrix tps();
};
void matrix::input()
{
  cout<<"\n enter dimensions";
  cin>>r>>c;
  for(int i=0;i<r;i++)
  for(int j=0;j<c;j++)
  cin>>m[i][j];

}
void matrix::display()
{
   for(int i=0;i<r;i++)
   {
     cout<<"\n";
     for(int j=0;j<c;j++)
     cout<<m[i][j]<<"\t";
   }
}
matrix matrix::add(matrix m2)
{
  matrix m3;

 for(int i=0;i<m2.getr();i++)

 for(int j=0;j<m2.getc();j++)
    {
       m3.m[i][j]=m[i][j]+m2.m[i][j];
    }

     return(m3);
}
matrix matrix::sub(matrix m2)
{
  matrix m3;

     for(int i=0;i<m2.getr();i++)
     for(int j=0;j<m2.getc();j++)
      {
	m3.m[i][j]=m[i][j]-m2.m[i][j];
      }
	return (m3);
}
matrix matrix::mul(matrix m1,matrix m2)
{
  matrix m3;
  m3.r=m1.r;
  m3.c=m2.c;
   {

     for(int i=0;i<m3.getr();i++)
     for(int j=0;j<m3.getc();j++)
      {
	 m3.m[i][j]=0;
	 for(int k=0;k<=m3.getc();k++)
	 m3.m[i][j]=m3.m[i][j]+m[i][k]*m2.m[k][j];
      }

    }

    return(m3);
}
matrix matrix::tps()
{
  matrix m3;
  for(int i=0;i<c;i++)
  for(int j=0;j<r;j++)
     m3.m[i][j]=m[j][i];

   return m3;
}

void main()
{
  matrix m1,m2,m3,m4,m5,m6,m7,m8;
  int ch;
  cout<<"\n enter information for first matrix";
  m1.input();
  cout<<"\n first matrix is";
  m1.display();
  cout<<"\n enter information for second matrix";
  m2.input();
  cout<<"\n seond matrix is";
  m2.display();
  if( m1.getr()==m2.getr() && m1.getc()==m2.getc())
  cout<<"\n menu";
  cout<<" \n press 1 for addition";
  cout<<"\n press 2 for multiplication";
  cout<<"\n press 3 for subtraction";
  cout<<"\n press 4 for transpose";
  cout<<"enter your choice";
  cin>>ch;
  switch(ch)

{ case 1:
  m3=m1.add(m2);
  cout<<"\n sum of two matrices is";
  m3.display();
  break;
  case 2:
  m4=m1.sub(m2);
  cout<<"\n difference of two matrices is";
  m4.display();
  break;
  case 3:
  m5=m8.mul(m1,m2);
  cout<<"\n product of two matrices is";
  m5.display();
  break;
  case 4:
  m6=m1.tps();
  cout<<"\n transpose of first matrix is";
  m6.display();
  m7=m2.tps();
  cout<<"\n transpose of second matrix is";
  m7.display();
  break;
  deafault:cout<<"invalid choice";
  break;
}
}


How do you know you're getting an infinite loop? When is the application stopping?
Run it...application not stopping
Could you enlighten us with some more information? Where exactly does the program not stop?

Also, <iostream.h> is depreciated; use <iostream> instead. void main() is not even legal C++. It must always be int main(). I strongly suggest you update your compiler if it's letting this compile, as it means your compiler is not even compliant with standards set over a decade ago.

Member variables 'r' and 'c' are not initialized for matrix "m3" in "add", "sub"
and "tps" funtions.

Corrected funtions

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
matrix matrix::add(matrix m2)
{
 matrix m3;
 m3.r=r;
 m3.c=c;

 for(int i=0;i<m2.getr();i++)

 for(int j=0;j<m2.getc();j++)
    {
       m3.m[i][j]=m[i][j]+m2.m[i][j];
    }

     return(m3);
}

matrix matrix::sub(matrix m2)
{
  matrix m3;
  m3.r=r;
  m3.c=c;

     for(int i=0;i<m2.getr();i++)
     for(int j=0;j<m2.getc();j++)
      {
	m3.m[i][j]=m[i][j]-m2.m[i][j];
      }
	return (m3);
}

matrix matrix::tps()
{
  matrix m3;
  m3.r=c;
  m3.c=r;

  for(int i=0;i<c;i++)
  for(int j=0;j<r;j++)
     m3.m[i][j]=m[j][i];

   return m3;
}


To avoid this type of problems, always initialize the member variables in the constructor to suitable values.
Last edited on
Topic archived. No new replies allowed.