printing filtered array content – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #CPP#3095 siteicon   siteicon   siteicon  

printing filtered array content

Demonstrating how to print the filtered array content.

Learning Objectives

  • Getting specific data out from an array based on a condition called the filter condition.

Source Code

TC++ #3095

#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int ar[]={32,10,19,44,99};
for(int i=0;i<5;i++)
if(ar[i]>30) cout<<ar[i]<<" ";
getch();
return 0;
}

Source Code

#include <iostream>
using namespace std;
int main()
{
int ar[]={32,10,19,44,99};
for(int i=0;i<5;i++)
if(ar[i]>30) cout<<ar[i]<<" ";
return 0;
}

Test it !
Alternate (?) :

Run Output

32 44 99

Code Understanding

int ar[]={32,10,19,44,99}; //An array with initialised integer values/

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

if(ar[i]>30) cout<<ar[i]<<” “;
With in if is the filter condition. This can be simple condition as given here or it can be a complex condition returned from a function. Once the condition is met here we are printing the filtered data.

Notes

  • The filter condition can also be returned from a complex condition returned via a selection statement, a series of loops or a function return. Aim is to select specific members of the array which are meeting the criterion.
  • The filtered output may stored in a secondary array.


Suggested Filename(s): arr-filt.cpp



Share

sunmitra| Created: 27-Jan-2018 | Updated: 28-Jan-2018|






Back