need a urgent help.

this code should disply the sum of N*N matrix.but i am having problm/

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
  int main ()
{
enterMatrices();
sumOfMatrices();
displaySumMatrix ();
return 0;
}
void enterMatrices()
{
int matrix1[3][3];
int matrix2[3][3];
int matrix1=0;
int matrix2=0;
int z;
cout << "Please enter the rows and columsnfor your first matrix.";
//for statement enter the value of rows inmatrix 1
for (int i = 0; i < 3; i++)
{
cout << "Please enterthe values of row " << i + 1 << endl;

//for statement to enter thevalue of columns in matrix 1
for (int j = 0; j <3; j++){
cout<<matrix1[i][j];
}}
cout << "Please enter the rows and columsnfor your first matrix.";
for (int i = 0; i < 4; i++)
{
cout << "Please enterthe values of row" << i + 1 << endl;
for (int j = 0; j <4; j++)
{
z=matrix2[i][j];
}}}
void sumOfMatrices()
{
int matrix1Rows, matrix2Columns, matrix1Columns,matrix2Rows, matrix1, matrix2;
int sum=0;
if (matrix1Rows == matrix2Columns &&matrix1Columns == matrix2Rows)
{
for (int i = 0; i <matrix1Rows; i++)
{
for (int j= 0; j < matrix1Columns; i++)
sum[i][j] = matrix1[i][j] +matrix2[i][j];
}
}

else
cout << "Incorrect matrix. Pleasetry again later!";
exit(0);

}
void displaySumMatrix ()
{
int matrix1, matrix2, sumMatrix;

for (int i = 0; i < 3; i++)
{
cout << "| ";

for (int j = 0; j<3; j++)
cout<< sumMatrix[i][j];
cout<< " | " << endl;
}
}
You need to find an indentation style you like and use it consistently. Your code with no formatting is very very difficult to read. https://en.wikipedia.org/wiki/Indent_style

Next you probably should read up on what happens when your code encounters the exit() function.
http://www.cplusplus.com/reference/cstdlib/exit/


@mehdinaqvi
this code might help you. i just made it for another guy and jlb is right your code is unreadable.

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
# include<iostream>
# include<conio.h>
using namespace std;
void main()
{
	int a[3][3];
	int b[3][3];
	int sum[3][3];
	cout<< "Matrix A "<<endl<<endl;
	for(int i=0;i<3;i++)
	{
		
		for(int j=0;j<3;j++)
		{
			cout<<"Enter Value for loaction : ("<< i << "," << j << ") " ; //you can use "i+1" and "j+1" instead of "i" and "j" to understand better for locations.
			cin>>a[i][j];
			cout<<endl; 
		}
	
	}  // i am copying the same code as above for getiing input in b matrix but if you are aware of functions you can write it in a function and use twice.
	cout<< "Matrix B "<<endl<<endl;
		for(int i=0;i<3;i++)
	{
		
		for(int j=0;j<3;j++)
		{
			cout<<"Enter Value for loaction : ("<< i << "," << j << ") " ; //you can use "i+1" and "j+1" instead of "i" and "j" to understand better for locations.
			cin>>b[i][j];
			cout<<endl; 
		}
	
	}

	for (int i=0;i<3;i++)           
	{
		for( int j=0;j<3;j++ )
		{
			sum[i][j]=a[i][j]+b[i][j];
		}

	}

	cout<<endl<<endl;

		for (int i=0;i<3;i++)
	{

		for( int j=0;j<3;j++ )
		{
			cout<<sum[i][j]<< " " ;
		}
		cout<<endl;
	}
	getch();
}
Topic archived. No new replies allowed.