copying filtered array content to another array by maintaining count – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#3106 siteicon   siteicon   siteicon  

copying filtered array content to another array by maintaining count

Demonstrating how to filter the array content and then copy it to another array by maintaining the filter size count.

Learning Objectives

  • Getting specific data out from an array based on a condition called the filter condition and then filling this selected data into an destination array. In this case we maintain the destination array content count which can be used while taking out the filtered members.

Source Code

TC++ #3106


Source Code

Alternate (?) :

Run Output

32 44 99

Code Understanding

int ar[]={32,10,19,44,99}; //An array with initialised integer values as the source array
int arf[5]; //This is the destination filtered array. It has to be the same size as it is possible that all members of the array are meeting the filter criterion

for(int i=0;i<5;i++)
loop to traverse the array terms one by one from index 0

if(ar[i]>30) {  arf[j]=ar[i]; j++; cnt++; }
With in if is the filter condition.  Once the condition is met here we are copying the data from original array to the destination array but with a separate index in the destination array. The destination array index moves only when the condition is met. If the condition is met we also increase a separate variable count so that keep a track over how much size filtered data has produced.

for(int i=0;i<cnt;i++) if(arf[i]>=0) cout<<arf[i]<<” “;
Here we print the data from the filtered array upto the count cnt maintained while filling. Beyond this count the destination array may contain any junk values.


  • This program approach is useful to handle any data which can have any range of values.
  • In modern programming approach we use pointer method or vector method to create dynamic arrays.

Suggested Filename(s): arr-filt-copy.cpp,arrfltcp.cpp


sunmitra| Created: 28-Jan-2018 | Updated: 28-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