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

PF Lecture 5 Function+Arrays

The document discusses programming fundamentals related to functions and arrays. It defines what a function is and the different parts of a function, including the function declaration, definition, and calling. It also discusses built-in and user-defined functions. The document then covers array terminology, such as size, type, base, index, and range. It provides examples of one-dimensional array initialization and types of arrays. Programming examples are included to demonstrate function and array concepts.

Uploaded by

Fahad Qureshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

PF Lecture 5 Function+Arrays

The document discusses programming fundamentals related to functions and arrays. It defines what a function is and the different parts of a function, including the function declaration, definition, and calling. It also discusses built-in and user-defined functions. The document then covers array terminology, such as size, type, base, index, and range. It provides examples of one-dimensional array initialization and types of arrays. Programming examples are included to demonstrate function and array concepts.

Uploaded by

Fahad Qureshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Programming Fundamental

FUNCTIONS + ARRAYS

Prepared By:
Shahid Mehmood
WHAT IS FUNCTION?
 A function is set of instructions that are designed to
perform a specific task.

 A function is a complete and independent program. It is


executed by the main function or any other function of the
program to perform its task

 The functions are written to write the code of large


program by dividing it in to smaller units.
 The function are also written to avoid replication of code in
the program
TYPES OF FUNCTIONS
 Built in function :

The function that have already been defined as a part of the


language and can be used in any program are called built in
function

 User defined function:


The functions created by user are called user defined
functions. These functions are written to perform a
specific task
PARTS OF USER DEFINED
FUNCTION

There are three parts of user defined function

1. Function declaration
2. Function definition
3. Function calling
FUNCTION DECLARATION

The function declaration is also called prototype. Prototype


means sample or model. The function declaration only
provides the model of the function

Function declaration included:

 Name of the function


 The type of data returned by the function

 The number of arguments or parameters used in the


function
SYNTAX OF FUNCTION DECLARATION

return_type function Name (Arguments);

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

It consists of two parts


1. Declarator:
It is the heading line of the function definition. The heading
line of the function definition is the same as the function
declaration but it is not terminated by the semicolon;

2. Body of the function

The set of arguments enclosed in braces after the declarator are


called body of the function
SYNTAX OF FUNCTION DEFINITION
Function Return type FunctionName (list of parameters)
{
set of statements;
}

Example:

void display (void)


{
cout<<“this is my first program”<<endl;
}
EXAMPLE PRACTICE
#include <iostream> Function Definition

using namespace std;


void function1( )
{
cout <<"Hello World";
}
int main( )
Function Declaration
{
void function1(void);
Function calling
function1( );
return 0;
Output:
}
Hello World
PRACTICE EXAMPLE
Program Understanding
#include <iostream>
using namespace std;
10 19
void sum(int x, int y) void function1 (int x, int y)
{ {
int c;
int c; c=x+y;
c = x+y; 10+19=19;
Sum is: 19
cout<<"Sum is: "<<c;
}
int main( )
{
sum(10,19); Output
return 0; Sum is: 9

}
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 calculateSquare(int number)


{
return number * number;
}

int main()
{
int number;

cout << "\nPlease Enter Number to find Square of it = ";


cin >> number;

int square = calculateSquare(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

 An array permits homogeneous data. It means that similar


types of elements are stored contiguously in the memory under
one variable name.
 An array can be declared of any standard or custom data type
 They can be used to store collection of primitive data
types such as int, float, double, char, etc of any
particular type. 
ARRAY TERMINOLOGIES
Size: Number of elements or capacity to store elements in an
array. It is always mentioned in square brackets [ ].

Type: Refers to data type. It decides which type of element is


stored in the array. It is also instructing the compiler to reserve
memory according to the data type.

Base: The address of the first element is a base address. The


array name itself stores address of the first element.

Index: The array name is used to refer to the array element.


For example num[x], num is array and x is index. The value of
x begins from 0.The index value is always an integer value.

Range: Value of index of an array varies from lower bound to


upper bound. For example in num[100] the range of index is 0
SYNTAX OF ARRAY REPRESENTATION

Data type array_name [array size]


ARRAYS INITISLIZATION EXAMPLES
Example 1 (Arrays initialization)
char name[20]=“kashif”;

Example 2(Arrays initialization)


name[0]=‘k’;
name[1]=‘a’;
name[2]=‘s’;
name [3]=‘h’;
name [4]= ‘I’;
name [5]= ‘f’;

Example 3 (Arrays initialization)

int abc [5]; // array name abc of size 5


abc[0]=2; abc[1]=3; abc[2]=5;abc[3]=7; abc[4]=10;
float b[3];
TYPES OF ARRAY
There are two types of Array
 One dimensional array

 Multi dimensional array


ARRAY SINGLE DIMENSIONAL
INITIALIZATION
ONE DIMENSIONAL ARRAY EXAMPLE
 One dimensional array is also known as a list of linear array. It
consists of only one column or one row.

Array Name Temp


Array size= 5
Memory Array Array Name
location Temp[5] Number of elements
Sketch
array =5
Temp [0] 22
Temp[1] 24 Tamp[0]= location 0
of array Temp
Temp[2] 18
Temp[3] 6 Temp[0]=22;
Element on the array
Temp[4] 32 location 0 is 22
Programming Examples
PROGRAM 4.01
#include<iostream>
using namespace std;
main ( ) Memory Sketch
{
Memory value
float a[5]; address
int i; a[0] 9.9
a[0]=9.9; // cout<<a[0]; a[1] 12.9
a[1]=12.9; // cout<<a[1]; a[2] 13.1
a[2]=13.1; a[3] 8.9
a[3]=8.9; a[4] 10.6
a[4]=10.6;
for(i=0;i<=4;i++)
cout<<“value in a[“<<i <<“]=“<<a[i]<<endl;// value in a[0]=9.9;
}
PROGRAM 4.1 (OUTPUT SCREEN)
Value in a[0]=9.9
Value in a[1]=12.9
Value in a[2]=13.1
Value in a[3]=8.9
Value in a[4]=10.6
PROGRAM 4.2
Write a program to enter any five values from
user in an array and print the reverse values
on the output screen

#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

Memory Sketch i=3;


abc[0] 1 Sum=sum+abc[3];
Sum=5+4=9
abc[1] 2
i=2;
abc[2] 3 sum-=sum+abc[2];
Sum=9+3=12
abc[3] 4
i=1;
abc[4] 5 sum-=sum+abc[1];
Sum=12+2=14

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=689
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

a[2] 9 b[2] 4 s[2] 13

a[3] 2 b[3] 6 s[3] 8

a[4] 4 b[4] 3 s[4] 7


PROGRAM 4.06
Write a program in C++, to input data in to the
array. Enter a value from the keyboard and find
out the location of the entered value in the array.
If the entered number is not found in the array,
display the message “ Number not found”
#include<iostream> abc[0] 6

using namespace std;


abc[1] 4
main ( )
{ abc[2] 8

int abc[5],n,i,p;
abc[3] 9
for(i=0;i<=4;i++)
{ abc[4] 1

cout<<“enter the value in element”<<i<<“=“;


cin>>abc[i];
}
p=0;
cout<<“Enter any integer value?”;
cin>>n;
for(i=0;i<=4;i+) 9==6; False abc[0] 6
{
if(n==abc[i]) 9==4; False abc[1] 4
{
p=i+1; 9==8; False abc[2] 8
break;
} p=i+1; 9==9; True abc[3] 9
} =3+1=4
if (p==0) abc[4] 1
cout<<“Number not found”;
else Output
cout<<“Number found at position =“<<p; Number Found at position 4
}
LEARN HOW ELEMENT DELETE INSIDE
ARRAY
PROGRAM
Write a C Program for deletion of an element from the specified location from an Array

#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

/* Decrement array size by 1 */


size--;
size=3
/* Print array after deletion */
cout<<“Elements of array after delete are : “<<endl;
for(i=0; i<size; i++)
{
cout<< arr[i]<<endl;
} Elements of array after deletion are:
} i=0 ; i<3; i++
cout<<arr[0]; 3
return 0; i=0+1=1;
} 1<3; True
cout<<arr[1]; 2
i=1+1=2;
2<3; True
cout<<arr[2];10
i=2+1=3;
3<3; False
OUTPUT
Enter the size of array : 4
Enter the elements in the array: 3, 2, 12, 10
Enter the element position to delete: 3

Elements of array after delete are : 3, 2, 10

You might also like