answersLogoWhite

0

#include<iostream>

#include<iomanip>

#include<vector>

#include<string>

#include<sstream>

using namespace std;

const unsigned width = 4;

const unsigned height = 3;

class matrix

{

private:

vector< vector<unsigned> > m_data;

string m_title;

public:

matrix(string title=""): m_data(height, vector<unsigned>(width)), m_title(title) {}

matrix(const matrix& copy): m_data(copy.m_data), m_title(copy.m_title) {}

matrix& operator=(matrix rhs)

{

// Note: assignment does not overwrite the matrix title.

for(unsigned row=0; row<height; ++row)

for(unsigned col=0; col<width; ++col)

operator[](row)[col]=rhs[row][col];

return(*this);

}

vector<unsigned>& operator[](const unsigned index){return(m_data[index]);}

void set_title(const string title){ m_title = title; }

string& get_title(){return(m_title);}

void show()

{

cout<<m_title<<'\n'<<endl;

for(unsigned row=0; row<height; ++row)

{

for(unsigned col=0; col<width; ++col)

cout<<setw(7)<<(*this)[row][col];

cout<<endl;

}

cout<<endl;

}

matrix& operator+=(matrix rhs)

{

for(unsigned row=0; row<height; ++row)

for(unsigned col=0; col<width; ++col)

(*this)[row][col]+=rhs[row][col];

return(*this);

}

matrix operator+(matrix rhs)

{

matrix result(m_title+" + "+rhs.m_title);

for(unsigned row=0; row<height; ++row)

for(unsigned col=0; col<width; ++col)

result[row][col]=(*this)[row][col]+rhs[row][col];

return(result);

}

matrix& operator-=(matrix rhs)

{

for(unsigned row=0; row<height; ++row)

for(unsigned col=0; col<width; ++col)

(*this)[row][col]-=rhs[row][col];

return(*this);

}

matrix operator-(matrix rhs)

{

matrix result(m_title+" - "+rhs.m_title);

for(unsigned row=0; row<height; ++row)

for(unsigned col=0; col<width; ++col)

result[row][col]=operator[](row)[col]-rhs[row][col];

return(result);

}

};

unsigned input_num (std::string prompt)

{

unsigned id = 0;

while (1)

{

cout<<prompt<<": ";

string input="";

getline (cin, input);

stringstream ss (input);

if (ss>>id)

break;

cout<<"Invalid input.\n";

}

return (id);

}

void initialise(matrix& m)

{

for(unsigned row=0; row<height; ++row)

{

for(unsigned col=0; col<width; ++col)

{

stringstream ss;

ss<<"Enter a value for "<<m.get_title()<<'['<<row<<"]["<<col<<']';

m[row][col]=input_num(ss.str());

}

}

cout<<endl;

}

int main()

{

matrix matrix_1("matrix_1");

initialise(matrix_1);

matrix_1.show();

matrix matrix_2("matrix_2");

initialise(matrix_2);

matrix_2.show();

matrix matrix_3 = matrix_1 + matrix_2;

matrix_3.show();

matrix matrix_4 = matrix_3 - matrix_2;

matrix_4.show();

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

What is matrix programming in C programming?

C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion


Why you divide a program into functions?

In order to make a large program more manageable, it is convenient to identify and isolate specific tasks that a program performs and to separate out those tasks into functions. These functions are used/invoked as needed by the main part of the program. They can also be invoked by other functions. Often a program will perform the same task in different parts of the program. Using a function to perform the task and invoking the function from the different parts of the program means that only one copy of the code is needed. This helps reduce the size of the program.


What are the functions of a utility program?

functions of utility program is to perform specific tasks related to the management of computer functions,resources or files as password protection,memory management,virus protection and file compressionThis utility reads all V3 MMS output files, print out the header, partial sub-header and a value from all fields in the dataset.


The max number of functions you have used in a c program?

There is no limit to the number of functions you can have in a program. The only practical limit is dependant upon the amount of memory you have available in order to load the compiled program, whether it has 4 functions or 4 trillion functions. If the program makes use of dynamic libraries, then the amount of available memory reduces accordingly.


What is the purpose of the command prompt?

the command prompt is a program where you can command the computer to perform functions such as shutdown, restart, open a certain file, start a program, etc.

Related Questions

What is matrix programming in C programming?

C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion


How do you develop a JAVA program that computes matrices?

Matrices can't be "computed" as such; only operations like multiplication, transpose, addition, subtraction, etc., can be done. What can be computed are determinants. If you want to write a program that does operations such as these on matrices, I suggest using a two-dimensional array to store the values in the matrices, and use for-loops to iterate through the values.


Why you divide a program into functions?

In order to make a large program more manageable, it is convenient to identify and isolate specific tasks that a program performs and to separate out those tasks into functions. These functions are used/invoked as needed by the main part of the program. They can also be invoked by other functions. Often a program will perform the same task in different parts of the program. Using a function to perform the task and invoking the function from the different parts of the program means that only one copy of the code is needed. This helps reduce the size of the program.


Program to display multiplication of two matrix?

The matrix multiplication in c language : c program is used to multiply matrices with two dimensional array. This program multiplies two matrices which will be entered by the user.


How do you write c program to perform sum of elements of matrix using pointers with functions?

i cant write


Write a C program to find sum of 3 matrices?

matrix


Is there any benefit in a C plus plus program to add two matrices?

No.


What is the definition of computer program?

A combination of computer instructions and data definitions that enable computer hardware to perform computational or control functions.


What does the word computer application mean?

It is a software or program designed to allow a user to perform a veriety of coordinated user defined functions


How data processing cycle work?

The instructions of a program generally direct the computer to perform three basic functions over and over again. These functions are input, processing and output. Collectively, these functions operate the data processing cycle.


What are the primary arithmetic operations a computer program can perform?

The four primary arithmetic operations a computer program can perform are addition, subtraction, multiplication and division.2 + 3 = 5 is an example of addition9 - 7 = 7 is an example of subtraction2 x 3 = 6 is an example of multiplication10 / 2 = 5 is an example of division


Write a program for addition in c plus plus?

#include#includemain(){clrscr();coutb;cout