Sum of primary diagonal members of a square matrix using 1 loop only – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#7521    siteicon   siteicon  

Sum of primary diagonal members of a square matrix using 1 loop only

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

Learning Objectives

  • Understanding summing of elements in a primary diagonal location of a square 2D matrix form array using just one iteration loop.

Source Code

TC++ #7521

 

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 sum=0; //Here we initialise variable sum as 0 so that further member value can be added to it

for(int i = 0; i < 3; i++ )
This for loop handles the row as well as column index.

sum+=arr[i][i];
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-1loop.cpp, pdiags1.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