0% found this document useful (0 votes)
5 views1 page

array

The document contains a C++ program that performs matrix operations based on user input. Users can choose to either calculate the sum of elements in a single matrix or perform addition and multiplication of two square matrices. The program prompts for matrix dimensions and elements, then outputs the results accordingly.

Uploaded by

ermikasa2716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

array

The document contains a C++ program that performs matrix operations based on user input. Users can choose to either calculate the sum of elements in a single matrix or perform addition and multiplication of two square matrices. The program prompts for matrix dimensions and elements, then outputs the results accordingly.

Uploaded by

ermikasa2716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include using namespace std;

int main() { int choice; cout << "Enter 1 for single matrix operations or 2 for two matrix
operations: "; cin >> choice;

if (choice == 1) {
int r, c, mat[10][10], sum = 0;
cout << "Enter rows and columns: ";
cin >> r >> c;

cout << "Enter matrix: ";


for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) {
cin >> mat[i][j];
sum += mat[i][j];
}

cout << "Total Sum: " << sum << "\n";


}
else if (choice == 2) {
int n, A[10][10], B[10][10], sum[10][10], prod[10][10] = {};
cout << "Enter size of square matrix: ";
cin >> n;

cout << "Enter first matrix: ";


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> A[i][j];

cout << "Enter second matrix: ";


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> B[i][j];

cout << "Matrix Addition:\n";


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i][j] = A[i][j] + B[i][j];
cout << sum[i][j] << " ";
}
cout << "\n";
}

cout << "Matrix Multiplication:\n";


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++)
prod[i][j] += A[i][k] * B[k][j];
cout << prod[i][j] << " ";
}
cout << "\n";
}
}
return 0;

You might also like