Finding count of members in a linear array – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#2502 siteicon   siteicon   siteicon  

Finding count of members in a linear array

Finding the number of members in an array using the sizeof operator

Learning Objectives

  • A method to find the number of members present in an array.

Source Code

TC++ #2502

Source Code

Run Output

Count of members in given array = 5

Code Understanding

int a[]={1,2,3,4,5};
Here an integer array with 5 fixed members have been initialised

int cnt=sizeof(a)/sizeof(a[0]);
we first calculate the sizeof whole array which would be size of integer (4 or 2) multiplied by number of members. We then divide it by size of first member which is a[0]. As per rule an array has to be all same type of members so size of all members will be same. Thus this computation will give us the number of members in the array

cout<<“Count of members in given array = “<<cnt<<endl
Here we simple print the given count of members.

Notes

  • This method of finding size of array can be used easily for any data type char, long, float, double etc. It can even be used of multi dimensional arrays.


Suggested Filename(s): arr-len.cpp, arr-memb-count.cpp



Share

sunmitra| Created: 11-Jan-2018 | Updated: 11-Jan-2018|






×
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