11

Click here to load reader

C++ ARRAY WITH EXAMPLES

Embed Size (px)

DESCRIPTION

HOW ARRAY WORKS?

Citation preview

Page 1: C++ ARRAY WITH EXAMPLES

C++ : ARRAY WITH EXAMPLES

SUMMATION OF TWO MATRIXES

#include <iostream>

using namespace std;

int main()

{

int m,n,c,d, first[10][10], second[10][10], sum[10][10];

cout << "Enter the number of rows and columns of matrix" << endl;

cin >> m>> n;

cout << "Enter the elements of first matrix \n";

for( c=0; c<m ; c++)

for(d=0; d<n ; d++)

cin >> first [c][d];

cout << "Enter the elements of second matrix \n";

for(c=0; c<m; c++)

for(d=0; d<n; d++)

cin >> second[c][d];

for (c=0;c<m;c++)

for(d=0;d<n;d++)

sum[c][d]= first[c][d] + second[c][d];

Page 2: C++ ARRAY WITH EXAMPLES

cout << "The summation of entered matrixes :- \n";

for(c=0; c<m; c++)

{

for(d=0; d<n; d++)

cout << sum[c][d] << "\t";

cout << endl;

}

return 0;

}

//Output :

Enter the number of rows and columns of matrix

3 3

Enter the elements of first matrix

1 2 3

4 5 6

7 8 9

Enter the elements of second matrix

1 2 3

4 5 6

7 8 9

The summation of entered matrixes :-

2 4 6

8 10 12

14 16 18

Page 3: C++ ARRAY WITH EXAMPLES

DIFFERENCE OF TWO MATRIXES

#include <iostream>

using namespace std;

int main()

{

int m,n,c,d, first[10][10], second[10][10], sum[10][10];

cout << "Enter the number of rows and columns of matrix" << endl;

cin >> m>> n;

cout << "Enter the elements of first matrix \n";

for( c=0; c<m ; c++)

for(d=0; d<n ; d++)

cin >> first [c][d];

cout << "Enter the elements of second matrix \n";

for(c=0; c<m; c++)

for(d=0; d<n; d++)

cin >> second[c][d];

for (c=0;c<m;c++)

for(d=0;d<n;d++)

sum[c][d]= first[c][d] - second[c][d];

cout << "The summation of entered matrixes :- \n";

for(c=0; c<m; c++)

{

Page 4: C++ ARRAY WITH EXAMPLES

for(d=0; d<n; d++)

cout << sum[c][d] << "\t";

cout << endl;

}

return 0;

}

//Output:

Enter the number of rows and columns of matrix

3 3

Enter the elements of first matrix

7 8 9

4 5 6

1 2 3

Enter the elements of second matrix

1 2 3

4 5 6

7 8 9

The summation of entered matrixes :-

6 6 6

0 0 0

-6 -6 -6

Page 5: C++ ARRAY WITH EXAMPLES

TRANSPOSE OF A MATRIX

#include <iostream>

using namespace std;

int main()

{

int c,d, matrix[3][3],transpose_matrix[3][3];

cout <<"Enter the elements of 3x3 matrix \n";

for( c=0; c<3 ; c++)

for(d=0; d<3 ; d++)

cin >> matrix [c][d];

for (c=0;c<3;c++)

for(d=0;d<3;d++)

transpose_matrix[c][d]= matrix[d][c];

cout << "The transpose of the given 3x3 matrix :- \n";

for(c=0; c<3; c++)

{

for(d=0; d<3; d++)

cout << transpose_matrix[c][d] << "\t";

cout << endl;

}

return 0;

}

Page 6: C++ ARRAY WITH EXAMPLES

//Output:

Enter the elements of 3x3 matrix

1 2 3

4 5 6

7 8 9

The transpose of the given 3x3 matrix :-

1 4 7

2 5 8

3 6 9

// Alternative way to find transpose of a matrix:

#include <iostream>

using namespace std;

int main()

{

int m,n,c,d, matrix[10][10],transpose_matrix[10][10];

cout << "Enter the number of rows and columns of matrix" << endl;

cin >> m>> n;

cout << "Enter the elements of matrix \n";

for( c=0; c<m ; c++)

for(d=0; d<n ; d++)

cin >> matrix [c][d];

for (c=0;c<m;c++)

for(d=0;d<n;d++)

Page 7: C++ ARRAY WITH EXAMPLES

transpose_matrix[c][d]= matrix[d][c];

cout << "The transpose of the entered matrixes :- \n";

for(c=0; c<m; c++)

{

for(d=0; d<n; d++)

cout << transpose_matrix[c][d] << "\t";

cout << endl;

}

return 0;

}

// Output:

Enter the number of rows and columns of matrix

5 5

Enter the elements of matrix

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

The transpose of the entered matrixes :-

1 6 11 16 21

2 7 12 17 22

3 8 13 18 23

4 9 14 19 24

5 10 15 20 25

Page 8: C++ ARRAY WITH EXAMPLES

MULTIPLICATION OF TWO MATRIXES (2X2 DIMENSION ONLY)

#include <iostream>

using namespace std;

int main()

{

int m,n,c,d, first[2][2], second[2][2], mult[2][2];

cout << "Enter the number of rows and columns of matrix" << endl;

cin >> m>> n;

cout << "Enter the elements of first matrix \n";

for( c=0; c<m ; c++)

for(d=0; d<n ; d++)

cin >> first [c][d];

cout << "Enter the elements of second matrix \n";

for(c=0; c<m; c++)

for(d=0; d<n; d++)

cin >> second[c][d];

for (c=0;c<m;c++)

for(d=0;d<n;d++)

mult[c][d]= first[c][0] * second[0][d] + first[c][1] * second[1][d];

cout << "Multiplication of entered matrixes :- \n";

Page 9: C++ ARRAY WITH EXAMPLES

for(c=0; c<m; c++)

{

for(d=0; d<n; d++)

cout << mult[c][d] << "\t";

cout << endl;

}

return 0;

}

//Output :

Enter the number of rows and columns of matrix

2 2

Enter the elements of first matrix

1 2

3 4

Enter the elements of second matrix

1 2

3 4

Multiplication of entered matrixes :-

7 10

15 22

Page 10: C++ ARRAY WITH EXAMPLES

INVERSE OF A MATRIX (2x2 DIMENSION ONLY)

#include <iostream>

using namespace std;

int main()

{

int c,d, determinant, matrix[2][2], inv[2][2];

cout << "Enter the elements of 2x2 matrix \n";

for( c=0; c<2 ; c++)

for(d=0; d<2 ; d++)

cin >> matrix [c][d];

determinant= matrix[0][0] * matrix[1][1] - matrix[0][1]*matrix[1][0];

for (c=0; c<2; c++)

for(d=0; d<2; d++)

{ if ( c==0 && d==0)

inv[c][d]= matrix[1][1];

else if (c==1 && d==1)

inv[c][d]= matrix[0][0];

else

inv[c][d]= - matrix[c][d];

}

Page 11: C++ ARRAY WITH EXAMPLES

cout << "Inverse of entered matrixes :- \n";

for(c=0; c<2; c++)

{

for(d=0; d<2; d++)

cout << (float) inv[c][d]/determinant << "\t";

cout << endl;

}

return 0;

}

//Output:

Enter the elements of 2x2 matrix

1 2

3 4

Inverse of entered matrixes :-

-2 1

1.5 -0.5