PF Lecture 5 Function+Arrays
PF Lecture 5 Function+Arrays
FUNCTIONS + ARRAYS
Prepared By:
Shahid Mehmood
WHAT IS FUNCTION?
A function is set of instructions that are designed to
perform a specific task.
1. Function declaration
2. Function definition
3. Function calling
FUNCTION DECLARATION
Example
Void my_function (void); void means nothing
int sum (int, int); // function declaration
Function
Function Function
return type
Name Arguments
EXAMPLES
Float temp(void);
Void print (int, float, char)
Function definition
The actual code of the function is called function
definition.
It is the set of instructions that are written to perform a
specific task
The function definition is always written outside the
main function. It can be written before and after the main
function
PARTS OF FUNCTION DEFINITION
Example:
}
PROGRAM PRACTICE
#include <iostream>
using namespace std;
void addnum(int,int);
void addnum(int,int,int);
int main( )
{
addnum (5,5);
addnum (5,2,8);
return 0;
}
void addnum (int x, int y)
{ cout<<"Integer number: "<<x+y<<endl;
}
EXAMPLE: PROGRAM TO CALCULATE SQUARE OF A NUMBER USING FUNCTIONS
#include<iostream>
using namespace std;
int main()
{
int number;
cout << "\nThe Square of the Given " << number << " = " << square;
return 0;
}
EXAMPLE: FUNCTION CALL BY VALUE/REFERENCE
#include<iostream>
using namespace std;
int swap(int a, int b) //user define fuction for swap values
{
cout<<"Before swap value of a="<<a<<endl<<" "<<&a<<endl;
cout<<endl<<"Before swap value of b="<<b<<" "<<&b<<endl;
int temp=0; //temp=0
temp=a; //temp=10
a=b; // a=19
b=temp; //b=10
cout<<"After swap the value of a="<<a<<" "<<&a<<endl;
cout<<"After swap the value of b="<<b<<" "<<&b<<endl;
}
int main ()
{
int x, y,c;
x=10;
y=19;
#include<iostream>
using namespace std;
main () Program Understanding
{
Enter any no.
void tab (int); 4
int n; tab(4); function calling
cout<<“Enter any no.” <<endl; 4
cin>>n; Void tab (int tt)
{
tab(n);
1 x 4= 4
} 2 x 4= 8
void tab ( int tt) …..
10 x 4 = 40
{ }
for (int c=1; c<=10;c++)
cout<<c<<“X”<<tt<<“=“<<c*tt<<endl;
}
BENEFITS OF ARRAY
So far we have used only single variable name for storing one
data item. If we need to store multiple copies of the same data
then it is very difficult for the user. To overcome the difficulty
a new data structure is used called array
An array is a linear and homogeneous data structure
#include<iostream>
using namespace std;
int main ()
{
{
int abc[5],i;
for(i=0;i<=4;i++)
{
cout<<“Enter the value in element”<<i<<endl;
cin>>abc[i];
}
cout<<Output in reverse order”<<endl;
for(i=4;i>=0;i--)
{
cout<<“Value in a[“<<i<<“]”<<abc[i]<<endl;
}
for(i=0;i<=4;i++) Program 4.2
{ (Understanding
cout<<“Enter the value in element”<<i<<endl; )
cin>>abc[i];
}
Memory sketch
Output Window
abc[0] 1 Enter the value in element 0
1
abc[1] 2
Enter the value in element 1
2
abc[2] 3 Enter the value in element 2
3
abc[3] 4 Enter the value in element 3
4
Enter the value in element 4
abc[4] 5 5
cout<<Output in reverse order”<<endl;
Program 4.2
for(i=4;i>=0;i--)
(Understanding
{
)
cout<<“Value in a[“<<i<<“]”<<abc[i]<<endl;
}
Memory Sketch
abc[0] 1 Output:
Value in a[0] 1
abc[1] 2 Value in a[1] 2
Value in a[2] 3
abc[2] 3 Value in a[3] 4
Value in a[4] 5
abc[3] 4
abc[4] 5
Program 4.3
Write a program in C++ to input data in to an array of 5
elements. Calculate the sum and average of the elements
and then print the sum and average on the screen
#include<iostream>
using namespace std;
int main ()
{
float abc[5],sum, avg;
int i;
for(i=0;i<=4;i++)
{
cout<<“Enter value in element”<<i<<endl;
cin>>abc[i];
}
sum=avg=0.0;
for(i=4;i>=0;i--)
sum=sum+abc[i];
avg=sum/5.0;
Cout<<“Sum of array values=“<<sum<<endl;
Cout<<“Average of array values=“<<avg;
}
Program 4.3
(Understanding
for(i=0;i<=4;i++) )
{
cout<<“Enter value in element”<<i<<endl;
cin>>abc[i];
}
Memory Sketch
abc[0] 1
abc[1] 2
abc[2] 3
abc[3] 4
abc[4] 5
Program 4.3
(Understanding
sum=avg=0.0; )
i=4
for(i=4;i>=0;i--) Sum=0
sum=sum+abc[i]; Sum=sum + abc[4];
Sum =0+5=5
i=0;
Sum=sum+abc[0];
Sum=14+1=15
PROGRAM 4.4
Write a program in C++ to find out and print the
maximum value in the array
#include<iostream>
using namespace std;
main ()
{
float abc[5],max;
int i;
abc[0] 6
float abc [ 5],max;
for(i=0;i<=4;i++) abc[1] 4
{
cout<<“Enter the value in element”<<i<<“=“; abc[2] 8
cin>>abc[i];
abc[3] 9
}
max=abc[0];
abc[4] 1
for(i=0;i<=4;i++)
{
if(max<abc[i])
max=abc[0]- max=6
max=abc[i];
}
cout<<“Maximum value is =“<<max; Program 4.4
(Understanding
} )
Program 4.4
(Understanding max=abc[0]-
) max=689
for(i=0;i<=4;i++)
{
if(max<abc[i]) abc[0] 6
max=abc[i];
abc[1] 4
}
abc[2] 8
abc[3] 9
abc[4] 1
PROGRAM 4.5
Write a program in C to input data in to two different arrays and then
add two arrays and store the result in the 3rd array
#include<iostream>
using namespace std;
main ( )
{
float a[5],b[5], s[5];
int i;
cout<<“enter the value in the first array”<<endl;
for (i=0;i<=4;i++)
{
cout<<“enter value in element”<<i<<“=“;
cin>>>a[i];
}
cout<<“enter the value in the 2nd Array”<<endl;
for (i=0;i<=4;i++)
{
cout<<“enter value in element”<<i<<“=“;
cin>>>b[i];
}
cout<<“first array + second Array=sum”<<endl;
for (i=0;i<=4;i++)
{
s[i]=a[i]+b[i];
cout<<a[i]<<“+”<<b[i]<<“=“<<s[i]<<endl;
}
}
1st Array 2nd Array 3rd Array
a[0] 3 b[0] 1 s[0] 4
a[1] 6 b[1] 2 s[1] 8
int abc[5],n,i,p;
abc[3] 9
for(i=0;i<=4;i++)
{ abc[4] 1
#include <iostream>
Enter size of the array:
4
#define MAX_SIZE 100
Enter elements in array:
0<4; True
int main() cin>>arr[0]; 3
{ a[0] 3
i=1;
int arr[MAX_SIZE]; 1<4; True
int i, size, pos; a[1] 2
cin>>arr[1]; 2
i=2;
/* Input size and element in array */ 2<4; True a[2] 12
cout<<"Enter size of the array : "; cin>>arr[2];12
cin>>size; i=3; a[3] 10
cout<<"Enter elements in array : "; 3<4; True
for(i=0; i<size; i++) cin>>arr[3]; 10
{
cin>>arr[i];
}
/* Input element position to delete */
cout<<"Enter the element position to delete : “<<endl; pos=3
cin>>pos;
If(pos<0 || pos<size)
/* Invalid delete position */ 3<0 || 3>4
if(pos < 0 || pos > size) False;
{
cout<<"Invalid position! Please enter position between 1 to “<<size<<endl;
}
else
{
/* Copy next element value to current element */ i=3-1; i<3; i++
for(i=pos-1; i<size-1; i++) arr[2]=arr[3];
{ =10
arr[i] = arr[i + 1]; i=2+1=3
} 3<3; False