Carrying out a program that allows the sum of matrices in a recurrent exercise programming courses, and can structure the program easier if you have knowledge of linear algebra .
then the code for adding 2 arrays must have equal number of both rows and columns.
# include \u0026lt;iostream>
using namespace std;
int row, fil, i, j;
int main ()
{court \u0026lt;\u0026lt;" Enter the number of rows "\u0026lt;\u0026lt;endl;
cin>> fil;
court \u0026lt;\u0026lt;"Enter the number of columns" \u0026lt;\u0026lt;endl;
cin>> row;
int array1 [fil] [row];
for (i = 0; i \u0026lt;= fil-1, i + +)
{
for (j = 0, j \u0026lt;= row-1, j + +)
{
court \u0026lt;\u0026lt;"Enter the position" \u0026lt; \u0026lt;"("\u0026lt;\u0026lt; i j \u0026lt;\u0026lt;")"\u0026lt;\u0026lt;" \u0026lt;\u0026lt;")"\u0026lt;\u0026lt;"("\u0026lt;\u0026lt; Matrix 1 \u0026lt;\u0026lt;endl;
cin>> array1 [i] [j];}}
system ("pause");
int array2 [fil] [row];
is (i = 0; i \u0026lt;= fil-a i + +)
{
is (j = 0, j \u0026lt;= row-1, j + +)
{
court \u0026lt;\u0026lt;"enter the position \u0026lt;\u0026lt;"("\u0026lt;\u0026lt; and \u0026lt;\u0026lt;")"\u0026lt;\u0026lt;"("\u0026lt;\u0026lt; j \u0026lt;\u0026lt;")"\u0026lt;\u0026lt;" matrix 2"<<endl;
cin >>matriz2[i][j];
}
}
system("pause");
cout << "A continuacion se sumaran las matrices ingresadas "<<endl;
system("pause");
int matriz3[fil][row];
for (i=0;i<=fil-1;i++)
{
for (j=0;j<=row-1;j++)
{
matriz3[i][j]= matriz1[i][j]+matriz2[i][j];
cout << "Valor posicion "\u0026lt;\u0026lt;"("\u0026lt;\u0026lt; I j \u0026lt;\u0026lt;")"\u0026lt;\u0026lt;"("\u0026lt;\u0026lt; \u0026lt;\u0026lt;"): "\u0026lt;\u0026lt;arr3 [i] [j] \u0026lt; , \u0026lt;endl;}
} court \u0026lt;\u0026lt;"Thank you for using this program" \u0026lt;\u0026lt;endl;
system ("pause");
return 0;}
If you compile and run the program in Dev-C will be the summation results are printed in plain text, but restructuring the cycle for the print, it is possible to achieve an attractive graphical in printing the result matrix.
0 comments:
Post a Comment