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

Lec-13 Passing Array To Functions

The document discusses passing arrays to functions in C++. Some key points: - Arrays can be passed to functions like regular variables by specifying the array name as an argument. - When an array is passed, only a reference to the array is passed, not a copy. - Multiple arrays can be passed to the same function. - The same array can be passed to multiple functions. - Functions that accept array arguments typically specify the array type and size.
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)
41 views

Lec-13 Passing Array To Functions

The document discusses passing arrays to functions in C++. Some key points: - Arrays can be passed to functions like regular variables by specifying the array name as an argument. - When an array is passed, only a reference to the array is passed, not a copy. - Multiple arrays can be passed to the same function. - The same array can be passed to multiple functions. - Functions that accept array arguments typically specify the array type and size.
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/ 22

Arrays

Lecture # 11
Passing Arrays to Functions
Passing Arrays to functions
 You can pass array as an argument to a
function just like you pass variables as
arguments.
 In order to pass array to the function you just
need to mention the array name during
function call like this:
 functionname(arrayname); //
passing array to function  
Passing Arrays to functions
void function_name(data_type array_name[])
{
….
}

int main()
{
int array_name[]={…}
function_name(array_name);
}
Passing Arrays to functions
#include <iostream>
sum(a,b) will invoke this
using namespace std; function definition

void sum(int arr1[], int arr2[])


{ int temp[5];
for(int i=0; i<5; i++)
int main()
{
{
temp[i] = arr1[i]+arr2[i]; int a[5] = {10, 20, 30, 40 ,50};
cout<<temp[i]<<endl; } int b[5] = {1, 2, 3, 4, 5};
} //Passing arrays to function
sum(a, b);
}
 When an array is passed as an argument to a
function, only the name of an array is used as
argument.
 display(marks);
 Also notice the difference while passing array
as an argument rather than a variable.
 void display(int m[5]);
Passing Arrays to Functions
#include <iostream>
using namespace std; Displaying marks:
void display(int marks[5]); Student 1: 88
int main() Student 2: 76
Student 3: 90
{ int marks[5] = {88, 76, 90, 61, 69}; Student 4: 61
display(marks); Student 5: 69
}
void display(int m[5])
{ cout << "Displaying marks: "<< endl;
for (int i = 0; i < 5; ++i)
{ cout << "Student "<< i + 1 <<": "<< m[i] << endl;
}
}
#include <iostream>
float average(float a[])
{ int i;
float avg, sum=0;
for(i=0;i<8;++i)
{ sum+= a[i]; }
avg = sum/8;
return avg;
}
int main(){
float b, n[ ] = { 20.6, 30.8, 5.1, 67.2, 23, 2.9, 4, 8 };
b = average(n);
cout << "Average of numbers = " << b << std::endl;
}
PASSING MULTIPLE ARRAYS TO
FUNCTIONS
PASSING MULTIPLE ARRAYS TO FUNCTIONS
Program to Print Maximum Number
void printMax(int arr[5]);
int main()
{ int arr1[5] = { 25, 10, 54, 15, 40 };
int arr2[5] = { 12, 23, 44, 67, 54 };
printMax(arr1); //Passing array to function
printMax(arr2);
}
void printMax(int arr[5])
{ int max = arr[0];
for (int i = 0; i < 5; i++)
{ if (max < arr[i])
{ max = arr[i]; }
}
cout<< "Maximum element is: "<< max <<"\n";
}
PASSING ARRAY TO MULTIPLE
FUNCTIONS
PASSING ARRAY TO MULTIPLE FUNCTIONS
#include <iostream>
using namespace std;
void display1(int marks[5]);
void display2(int marks[5]);
void display1(int m[5])
{ for (int i = 0; i < 5; ++i)
{ cout << "Student "<< i + 1 <<": "<< m[i] << endl; cout<<"\nFUNCTION 1 ";
}}
void display2(int n[5])
{for (int i = 0; i < 5; ++i)
{ cout << "Student "<< i + 1 <<": "<< n[i] << endl; cout<<"\nFUNCTION 2 ";
}}
int main()
{ int marks1[5]= {88,76,90,61,69};
display1(marks1);
display2(marks1);
}
Example
Passing Arrays to Functions
#include<iostream.h>
#include<conio.h>
const int LENGTH=7;
void show(int te[LENGTH]);
void main(void)
{ clrscr();
int t[LENGTH];
for(int a=0;a<LENGTH;a++)
{
cout<<"Enter Temperature for day” <<a+1<<" ";
cin>>t[a];
}
show(t);
getch();
}
Passing Arrays to Functions( cont…)
void show(int temperature[LENGTH])
{ int s=0;
for(int a=0;a<LENGTH;a++)
{
cout<<"\nTemperature for day "<<a+1<<" is ";
cout<<temperature[a];
s += temperature[a];
}
cout<<"\nSum of Temperatures is "<<s;
cout<<"\nAverage of Temperature is "<<s/LENGTH;
}
Lets do Some Practice
What is the output of the code segment
below?
int mys(int [ ],int);
int main()
{ int nums[9] = {13, 11, 15, 9, 7, 5, 8, 3, 1};
int n = mys(nums, 9);
cout << n << endl; // (*) }
int mys(int array[], int len)
{ int n = 1;
for (int i = 1; i < len; i++)
{ if (array[i] < array[i - 1])
{ n++;}
else
{ n = 1; }
cout<<n;}
return n;
}
Write the output of the following code.
#include<iostream>
using namespace std;
int main()
{ int arr[10], i, num, n, cnt=0, pos;
cout<<"\n Enter Array Size : ";
cin>>n;
cout<<"\n Enter Array Elements : \n";
for(i=0; i<n; i++)
{ cout<<" ";
cin>>arr[i];
}
cout<<"\n Enter Element to be Searched : ";
cin>>num;
for(i=0; i<n; i++)
{ if(arr[i]==num)
{ cnt=1;
pos=i+1;
break; } }
if(cnt==0)
{ cout<<"\n Element Not Found..!!"; }
else
{ cout<<"\n Element "<<num<<" Found At Position "<<pos; }}
Write the output of the following code.
#include<iostream.h>
using namespace std;
void most_occurred_number(int nums[], int size)
{ int max_count = 0;
cout << "\nMost occurred number: ";
for (int i=0; i<size; i++)
{ int count=1;
for (int j=i+1;j<size;j++)
if (nums[i]==nums[j])
count++;
if (count>max_count)
max_count = count;
}
for (int i=0;i<size;i++)
{ int count=1;
for (int j=i+1;j<size;j++)
if (nums[i]==nums[j])
count++;
if (count==max_count)
cout << nums[i] << endl;
}}
int main()
{ int nums[] = {4, 5, 9, 12, 9, 22, 45, 7};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
most_occurred_number(nums, n);
}

You might also like