What's the error in line 42?

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
#include<iostream.h>
#include<conio.h>
void entermatrix(int [100][100], int, int);
void displaymatrix(int [100][100], int, int);
void addmatrix(int [100][100], int[100][100], int[100][100], int, int);
void subtractmatrix(int[100][100], int[100][100], int[100][100], int, int);
void main()
{
        clrscr(); 
	int A[100][100], B[100][100], C[100][100], m, n;
	entermatrix(A,m,n);
	displaymatrix(A,m,n);
	addmatrix(A,B,C,m,n);
	subtractmatrix(A,B,C,m,n);
	getch();
}
void entermatrix(int X[100][100], int y, int z)
{
	do
	{
	 cout<<"Enter no. of rows less than =100: ";
     cin>>y;
	}while(y>100);
    do
	{
	 cout<<"Enter no. of columns less than =100: ";
	 cin>>z;
	}while(z>100);
	cout<<"Enter the matrix elements: "<<endl;
	 for(int i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cin>>X[i][j];
	 }

}
void displaymatrix(int X[100][100], int y, int z)
{
	 cout<<"The matrix you entered is: "<<endl;
	 for(int i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
	      cout<<X[i][j];
	 cout<<endl;
	 }
}
void addmatrix(int X[100][100], int Y[100][100], int Z[100][100], int y, int z)
{
	cout<<"Enter the matrix you want to add to the first matrix you entered(obvoisly of the same order as the previous): "<<endl;
	 for(int i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cin>>Y[i][j];
	 }
	 cout<<"Matrix 1:"<<endl;
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cout<<X[i][j];
	 cout<<endl;
	 }
	 cout<<"Matrix 2:"<<endl;
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cout<<Y[i][j];
		 cout<<endl;
	 }
	 cout<<"The sum of the matrices you entered: "<<endl;
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
	    Z[i][j]=X[i][j]+Y[i][j];
	 }
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cout<<Z[i][j];
		 cout<<endl;
	 }
}
void subtractmatrix(int X[100][100], int Y[100][100], int Z[100][100], int y, int z)
{
 cout<<"Enter the matrix you want to subtract from the first matrix you entered(obvoisly of the same order as the previous): "<<endl;
	 for(int i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cin>>Y[i][j];
	 }
	 cout<<"Matrix 1:"<<endl;
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cout<<X[i][j];
	 cout<<endl;
	 }
	 cout<<"Matrix 2:"<<endl;
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cout<<Y[i][j];
		 cout<<endl;
	 }
	 cout<<"The difference of the matrices you entered: "<<endl;
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
	    Z[i][j]=X[i][j]-Y[i][j];
	 }
	 for(i=0; i<y; i++)
	 {
		 for(int j=0; j<z; j++)
			 cout<<Z[i][j];
		 cout<<endl;
	 }
}

When I run the program, the program executes an infinite loop and marks line 42 as that logical error(which causes the infinite loop). But my question is how is line 42 an infinite loop?
The program was supposed to make user enter a matrix, display it, then make the user enter another matrix then add and subtract it to/from the earlier one and display the sum/difference as a new matrix..Help pls..Urgent!
Last edited on
m and n has not been assigned any values when you pass them to the functions.
Actually the error is not in line 42.
You accepted the values of rows and columns but did not save them to int m and int n.
To make this right use 'reference' of m and n, i.e., the declaration and definition of function entermatrix must be respectively:-
void entermatrix( int&, int& );
void entermatrix( int &x, int &y)
{/*........Statements............*/}
Hope it helped.

P.S. I think you must also put spaces after each element while displaying the matrices
Topic archived. No new replies allowed.