0% found this document useful (0 votes)
37 views

Arrays 3 - Searching and Finding Frequency

Uploaded by

Syed Ali Ahsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Arrays 3 - Searching and Finding Frequency

Uploaded by

Syed Ali Ahsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Arrays

Lecture 3
Objectives
• Searching a number in Array
• Finding index of key element
• Finding frequency of key element  
• Find and replace (one and all)  
• Calculating sum and average 
Searching in Array
• The process of finding a particular element in an array is called
searching.

• In an unsorted array, the search operation can be performed by linear


traversal from the first element to the last element.

• The linear search compares each array element with the search key.
Code for searching a key element in array
#include <iostream>
using namespace std;
 
int main()
{
const int size = 5;
int arr[size] = {3,21,5,33,9};
int key = 0, flag = 0;
cout<<"Enter a number you want to search in array: ";
cin>>key;
for(int i=0; i<size; i++)
{
if(arr[i]==key)
{
flag = 1;
break;
}
}
if(flag == 1)
cout<<"number found";
else
cout<<"number not found";
  return 0;
}
• In this code we are using a loop to sequentially step through an array,
starting with the first element.

• It compares each element with the key being searched for and stops
when that key is found or the end of the array is reached.

• If we found the key element, we will set our flag and stops with the
help of break statement. It will save extra comparisons.
Finding Index of key element
• In searching algorithm we were only indicate the user about the
presence of key element.

• But now we have to indicate the user about the location of key element
if found.

• If the search key is a member of the array, typically the location of the
search key is reported to indicate the presence of the search key in the
array. Otherwise, a sentinel value is reported to indicate the absence of
the search key in the array
Code for Finding Index of key element
#include <iostream>
using namespace std;
 
int main()
{
const int size = 5;
int arr[size] = {3,21,5,33,9};
int key = 0, index = -1;
cout<<"Enter a number you want to search in array: ";
cin>>key;
for(int i=0; i<size; i++)
{
if(arr[i]==key)
{
index = i;
break;
}
}
if(index >= 0)
cout<<"number is at index: "<<index;
else
cout<<"number not found";
  return 0;
}
Finding frequency of key element
• In this Program we will count the occurrences of key element in an
array.
• The idea is simple, we initialize the variable count as 0 and traverse
array in linear fashion.
• For every element that matches with key element, we increment
count.
• After traversing the whole array we will print the count.
• Below is the implementation of the approach.
Code for Finding frequency of key element
#include <iostream>
using namespace std;
 
int main()
{
const int size = 5;
int arr[size] = {3,21,5,33,9};
int key = 0, count = 0;
cout<<"Enter a number you want to search in array: ";
cin>>key;
for(int i=0; i<size; i++)
{
if(arr[i]==key)
{
count++;
}
}
cout<<“count of your entered number is: "<<count;
  return 0;
}
Find and Replace

• Assigns new_value to all the elements that compare to key value.


Code for Finding frequency of key element
#include <iostream>
using namespace std;
 
int main()
{
const int size = 5;
int arr[size] = {3,21,5,33,9};
int key = 0, new_value = 0, flag = 0;
cout<<"Enter a number you want to replace in array: ";
cin>>key;
cout<<"Enter a new value you want to replace with: ";
cin>>new_value;
for(int i=0; i<size; i++)
{
if(arr[i]==key)
{
arr[i] = new_value;
flag = 1;
}
}
if(flag == 1)
{
cout<<"number found and replaced with new value now the elements of array are\n";
for(int i=0; i<size; i++)
cout<<arr[i]<<" ";
}
else
cout<<"number not found";
  return 0;
}
Code for Calculate Sum of elements in array
#include <iostream>
using namespace std;
 
int main()
{
const int size = 5;
int arr[size] = {3,21,5,33,9};
int sum = 0;
for(int i=0; i<size; i++)
{
sum = sum + arr[i];
}
cout<<“sum of Elements in Array is: “<<sum;
  return 0;
}
Code for Calculate Avg of elements in array
#include <iostream>
using namespace std;
 
int main()
{
const int size = 5;
int arr[size] = {3,21,5,33,9};
int sum = 0; float avg = 0;
for(int i=0; i<size; i++)
{
sum = sum + arr[i];
}
avg = (float)sum/size;
cout<<“Avg of Elements in Array is: “<<avg;
  return 0;
}s

You might also like