Lecture 2
Function andArray
Faculty of Computer Science
Jamilurahman Faizi
Structure
programming
3.
FUNCTION AND ARRAY
Generallywe pass values and variable while calling a function, likewise
we can also pass arrays to a function as argument you can pass arrays
element as well as whole array by just (specifying the array name)
which works as a pointer to a function.
We can pass array element
• Each element one by one
• Entire array
09/06/2025
NU-CSF
3
EXAMPLE
• Write aprogram that input five integers in array and passes the array
to a function and then display its value?
09/06/2025
NU-CSF
5
6.
#include<iostream>
using namespace std;
voidfshow(int arr[]);
int main()
{
int num[5],a;
cout<<"enter 5 int values";
for( a=0;a<5;a++)
cin>>num[a];
fshow(num);
return 0;
}
void fshow(int arr[])
{
int d;
cout<<"the values in array are n";
for(d=0;d<5;d++)
cout<<" n"<<arr[d];
}
09/06/2025
NU-CSF
6
7.
EXAMPLE
• Write aprogram that inputs five integers in array and passes array to a
function the function counts even numbers in array and returns result
to calling function.
09/06/2025
NU-CSF
7
8.
#include<iostream>
using namespace std;
inteven(int arr[]);
int main()
{
int num[5],i,n,a;
cout<<"enter 5 int values";
for( a=0;a<5;a++)
cin>>num[a];
n=even(num);
cout<<"the array contains "<<n<<" even numbers";
}
int even(int arr[])
{
int e=0,i;
for( i=0;i<5;i++)
if(arr[i]%2==0)
e++;
return e;
}
09/06/2025
NU-CSF
8
9.
PASSING 2DIMENSIONAL ARRAYTO FUNCTION
Write a program that inputs value in a 2-d array with 3 rows and 2
columns the program passes the array to function and the function
return the maximum value?
09/06/2025
NU-CSF
9
10.
#include<iostream>
using namespace std;
intmax(int arr[3][2]);
main()
{
int num[3][2],mx,a,b;
cout<<"enter 6 integer values";
for( a=0;a<3;a++)
for( b=0;b<2;b++)
cin>>num[a][b];
mx=max(num);
cout<<"the max value ="<<mx;
}
int max(int arr[3][2])
{
int m=0,s,d;
for( s=0;s<3;s++)
for( d=0;d<2;d++)
if(arr[s][d]>m)
m=arr[s][d];
return m;
}
09/06/2025
NU-CSF
10
11.
SEARCHING IN ARRAY
•Searching is the process of finding the required data in the array . searching becomes more
important when the length of an array is very large.
There are two types of search
• Sequential search
• Binary search
12.
SEQUENTIAL SEARCHING
• Sequentialsearch is also called linear search or serial search which is a
simple way to search an array for the desired value. Loops are frequently
used to visit elements of array for searching a value.
• As the name implies it searches the array starting with the first array
component it compares the search item with the elements in the array
starting at position zero and continues the search until it finds the item or
until no more data is left in the list to be compared with the searched items.
09/06/2025
NU-CSF
12
13.
ADVANTAGE
o Simplicity.
o Itis easy to understand
o Easy to implement
o Does not require the array to be in order
o The disadvantage is its inefficiency
o If there are 20,000 items in the array and what you are looking for is in
the 19,999th element, you need to search through the entire list.
09/06/2025
NU-CSF
13
14.
EXAMPLE
• Write downa program that initializes an array that input a value from
the user and search a number in array.
09/06/2025
NU-CSF
14
15.
#include<iostream>
using namespace std;
main()
{
intarr[]={1,2,3,4,5,6,7,8,9};
int i,n,loc=-1;
cout<<"enter value to find";
cin>>n;
for(i=0;i<=8;i++)
if(arr[i]==n)
loc=i;
if(loc!=-1)
cout<<"value found at positon="<<loc;
else
cout<<"value does not exist";
}
09/06/2025
NU-CSF
15
16.
EXAMPLE
#include<iostream>
using namespace std;
intarrsize=5;
int seqsearch(int list[], int listlength, int searchitem);
main()
{
int intlist[arrsize];
int number, index;
cout<<"enter "<<arrsize<<" integers value";
for(index=0;index<arrsize;index++)
cin>>intlist[index];
cout<<"enter no to find";
cin>>number;
index=seqsearch(intlist,arrsize,number);
if(index!=-1)
cout<<"no found at position "<<index;
else
cout<<"no not found";
return 0;
}
09/06/2025
NU-CSF
16
BINARY SEARCH
• Binarysearch is much more efficient than linear search.
• It requires the list to be in order.
• The algorithm starts searching with the middle element.
• If the item is less than the middle element, it starts over searching the first
half of the list.
• If the item is greater than the middle element, the search starts over starting
with the middle element in the second half of the list.
• It then continues halving the list until the item is found.
19.
EXAMPLE
Write a programthat initializes an array of size 12, it inputs an integer from the users and searches
the value in the array using binary search.
20.
BINARY SEARCH
#include<iostream>
using namespacestd;
int main()
{
int arr[12]={4,20,30,40,50,60,70,80,90,99,100,110};
int n,i,mid,start,end,loc;
loc=-1;
start=0;
end=11;
cout<<"enter any number to find";
cin>>n;
while(start<=end)
{
mid=(start+end)/2;
if(arr[mid]==n)
{
loc=mid;
break;
}
else if(n<arr[mid])
end=mid-1;
else
start=mid+1;
}
if(loc==-1)
cout<<"the value not found";
else
cout<<n<<" vlaue found at index="<<loc;
return 0;
}
21.
REFERENCE BOOKS
• Bjarnestroustrup. (2017). Programming principles and practice using c++. 6th edition.
• Paul deitel and harvey deitel "c++ how to program"
• Malik, d. S. (2014). C++ programming: from problem analysis to program design. Cengage
learning.