Sum of primary diagonal members of a square matrix array – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#7519    siteicon   siteicon  

Sum of primary diagonal members of a square matrix array

Finding sum of primary diagonal (from left top to right bottom) members of a 2D of a square matrix array

Learning Objectives

  • Understanding summing of elements in a primary diagonal location of a square 2D matrix form array using the traditional nested style for loops.
  • Understanding calculation of matrix size of one side dimension in terms of numbers of rows or number of columns in a square matrix.

Source Code

TC++ #7519

 

Source Code

Run Output

Sum of primary diagonal members = 15

Code Understanding

int arr[3][3]={{1,2,3},{3,4,5},{6,7,8}};
Here is an initialised 3 row and 3 column two dimensional array.

int dim=sqrt(sizeof(arr)/sizeof(arr[0][0]));
being a square matrix type array we can find the array side or dimension by square rooting the whole size divided by size of first member

int sum=0; //Here we initialise variable sum as 0 so that further member value can be added to it

for(int i = 0; i < dim; i++ )
This for loop handles the row index.

for(int j = 0; j < dim; j++ )
This loop handles the column in each row. both the loop travel for same size given by dim.

if(i==j) sum+=arr[i][j];
for each primary diagonal members row count and column count is same. row column count in given example can be seen below
0,0  0,1  0,2
1,0  1,1  1,2
2,0  2,1  2,2

we can observe above that row and column count is same of primary diagonal which are given by 0,0/1,1/2,2, thus the logic i==j will work
for each element of primary diagonal.

cout<<“Sum of primary diagonal members = “<<sum<<endl;
Here we are simply printing the sum after the looping is over.


Suggested Filename(s): sum-primary-diag.cpp, pdiagsum.cpp



Share

CSKC| Created: 11-Oct-2019 | Updated: 11-Oct-2019|






×
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 2D Array Memory Addressing 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 Multi-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


Back