Answer to Question #277779 in C++ for Zain Abbas

Question #277779

You are required to write menu driven C++ program for matrix operations. You will input matrices from


the user depending on the operation. Your program should support following operations.


1. Addition of matrices


2. Multiplication of matrices


3. Norm of matrix (square root of sum of squares of elements in a matrix)


4. Inverse of the matrix

1
Expert's answer
2021-12-09T17:37:19-0500
#include<iostream>
#include<iomanip>
using namespace std;

void Menu()
{
	cout << "\nPlease, select some of the following:\n"
		<< "A - Addition of matricies\n"
		<< "M - Multiplication of matricies\n"
		<< "N - Norm of matrix\n"
		<< "I - Inverse of the matrix\n"
		<< "E - Exit program\n";
	cout << "Your choice: ";
}

void AddMatr()
{
	unsigned int N;
	cout << "Please, enter an order of matrices: ";
	cin >> N;
	int **m1 = new int*[N];
	for (int i = 0; i<N; i++)
		m1[i] = new int[N];
	cout << "Please, enter values for the first matrix.\n";
	for (int i = 0; i<N; i++)
		for (int j = 0; j<N; j++)
		{
			cout << "Enter a value for [" << i << "][" << j << "] element: ";
			cin >> m1[i][j];
		}
	int **m2 = new int*[N];
	for (int i = 0; i<N; i++)
		m2[i] = new int[N];
	cout << "Please, enter values for the second matrix.\n";
	for (int i = 0; i<N; i++)
		for (int j = 0; j<N; j++)
		{
			cout << "Enter a value for [" << i << "][" << j << "] element: ";
			cin >> m2[i][j];
		}
	cout << "Resulting matrix is:\n";
	for (int i = 0; i<N; i++)
	{
		for (int j = 0; j<N; j++)
		{
			cout << m1[i][j] + m2[i][j] << "\t";
		}
		cout << endl;
	}
	delete[] m1;
	delete[] m2;
}


void MultMatr()
{
	unsigned int N;
	cout << "Please, enter an order of matrices: ";
	cin >> N;
	int **m1 = new int*[N];
	for (int i = 0; i<N; i++)
		m1[i] = new int[N];
	cout << "Please, enter values for the first matrix.\n";
	for (int i = 0; i<N; i++)
		for (int j = 0; j<N; j++)
		{
			cout << "Enter a value for [" << i << "][" << j << "] element: ";
			cin >> m1[i][j];
		}
	int **m2 = new int*[N];
	for (int i = 0; i<N; i++)
		m2[i] = new int[N];
	cout << "Please, enter values for the second matrix.\n";
	for (int i = 0; i<N; i++)
		for (int j = 0; j<N; j++)
		{
			cout << "Enter a value for [" << i << "][" << j << "] element: ";
			cin >> m2[i][j];
		}
	cout << "Resulting matrix is:\n";
	for (int i = 0; i<N; i++)
	{
		for (int j = 0; j<N; j++)
		{
			cout << m1[i][j] * m2[i][j] << "\t";
		}
		cout << endl;
	}
	delete[] m1;
	delete[] m2;
}


void NormMatr()
{
	unsigned int N;
	double result = 0;
	cout << "Please, enter an order of matrices: ";
	cin >> N;
	int **m1 = new int*[N];
	for (int i = 0; i<N; i++)
		m1[i] = new int[N];
	cout << "Please, enter values for the first matrix.\n";
	for (int i = 0; i<N; i++)
		for (int j = 0; j<N; j++)
		{
			cout << "Enter a value for [" << i << "][" << j << "] element: ";
			cin >> m1[i][j];
		}
	cout << "Norm matrix is: ";
	for (int i = 0; i<N; i++)
	{
		for (int j = 0; j<N; j++)
		{
			result = result + m1[i][j] * m1[i][j];
		}
	}
	cout << sqrt(result);
	delete[] m1;
}


void getCofactor(int **arr, int** temp, int p, int q, int n)
{
	int i = 0, j = 0;
	for (int row = 0; row<n; row++)
	{
		for (int col = 0; col<n; col++)
		{
			if (row != p&&col != q)
			{
				temp[i][j++] = arr[row][col];
				if (j == n - 1)
				{
					j = 0;
					i++;
				}
			}
		}
	}
}


int determinant(int** arr, int n)
{
	int N = n + 1;
	int D = 0;
	if (n == 1)return arr[0][0];
	int **temp = new int*[N];
	for (int i = 0; i<N; i++)
		temp[i] = new int[N];
	int sign = 1;
	for (int i = 0; i<n; i++)
	{
		getCofactor(arr, temp, 0, i, n);
		D += sign*arr[0][i] * determinant(temp, n - 1);
		sign = -sign;
	}
	delete[] temp;
	return D;
}


void adjoint(int* arr[], int** adj,int N)
{

	if (N == 1)
	{
		adj[0][0] = 1;
		return;
	}


	int sign = 1;
	int **temp = new int*[N];
	for (int i = 0; i<N; i++)
		temp[i] = new int[N];


	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			getCofactor(arr, temp, i, j, N);
			sign = ((i + j) % 2 == 0) ? 1 : -1;
			adj[j][i] = (sign)*(determinant(temp, N - 1));
		}
	}
}


bool Inverse(int** arr, float** inverse, int N)
{
	int det = determinant(arr, N);
	if (det == 0)
	{
		cout << "Singular matrix,can`t find its inverse";
		return false;
	}
	int **adj = new int*[N];
	for (int i = 0; i<N; i++)
		adj[i] = new int[N];
	adjoint(arr, adj,N);
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			inverse[i][j] = adj[i][j] / float(det);
	return true;
}
void InversMatr()
{
	unsigned int N;
	double result = 0;
	cout << "Please, enter an order of matrices: ";
	cin >> N;
	int **m1 = new int*[N];
	for (int i = 0; i<N; i++)
		m1[i] = new int[N];
	cout << "Please, enter values for the first matrix.\n";
	for (int i = 0; i<N; i++)
		for (int j = 0; j<N; j++)
		{
			cout << "Enter a value for [" << i << "][" << j << "] element: ";
			cin >> m1[i][j];
		}
	float **inv = new float*[N];
	for (int i = 0; i<N; i++)
		inv[i] = new float[N];
	Inverse(m1, inv,N);
	cout << "Resulting matrix is:\n";
	for (int i = 0; i<N; i++)
	{
		for (int j = 0; j<N; j++)
		{
			cout <<setprecision(10)<< inv[i][j] << "\t";
		}
		cout << endl;
	}
}

int main()
{
	char choice;
	do
	{
		Menu();
		cin >> choice;
		switch (choice)
		{
			case 'A':case 'a':
			{
				AddMatr();
				break;
			}
			case 'M':case 'm':
			{
				MultMatr();
				break;
			}
			case 'N':case 'n':
			{
				NormMatr();
				break;
			}
			case 'I':case 'i':
			{
				InversMatr();
				break;
			}
		}
	} while (choice != 'E'&&choice != 'e');
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog