Matrix addition 2D with pre-initialised values – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#1635 siteicon   siteicon   siteicon  

Matrix addition 2D with pre-initialised values

Adding two 2D integer matrix into a third matrix of same size with values pre-initialised at the beginning.

Learning Objectives

  • 2D Matrix initialisation
  • Matrix addition into a target matrix of same size.
  • Retrieving data from a 2D matrix and printing in a matrix fashion only.

Source Code

TC++ #1635

Source Code

Run Output

8       10      12
14 16 18

Code Understanding

int A[2][3]={{1,2,3},{4,5,6}};
int B[2][3]={{7,8,9},{10,11,12}};
int C[2][3];
A & B are 2 row three column integer matrix with values initialised as given. C is a matrix of same size as A and B but without any initial values. This will contain the sum of values of A and B at the corresponding postions.
for(int i=0 ; i<2 ; i++ ) //Row traversing loop
for(int j=0; j<3; j++ ) //column traversing loop
C[i][j]=A[i][j]+B[i][j]; //Adding A & B corresponding member in putting in C

This one is the loop for printing the final matrix.
for(int i=0 ; i<2 ; i++ )  {    for(int j=0; j<3; j++ ) {
      cout<<C[i][j]<<“t”;    //here individual items of each row are printed with tab character in between.
} cout<<endl;  // //here a line between two rows are printed.


A check for target matrix meeting the condition of addability of two matrix can also be done.

Suggested Filename(s): addmatrix.cpp, matrixadditioninit.cpp


sunmitra| Created: 5-Dec-2017 | Updated: 8-Dec-2017|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues