0% found this document useful (0 votes)
3 views13 pages

L10_PDS

The document discusses various methods to pass arrays to functions in C++, including passing as sized arrays, unsized arrays, pointers, and references. It provides examples of each method, demonstrating how to manipulate array elements within functions. Additionally, it includes examples of linear and binary search algorithms implemented in C++.

Uploaded by

neevmodi217
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)
3 views13 pages

L10_PDS

The document discusses various methods to pass arrays to functions in C++, including passing as sized arrays, unsized arrays, pointers, and references. It provides examples of each method, demonstrating how to manipulate array elements within functions. Additionally, it includes examples of linear and binary search algorithms implemented in C++.

Uploaded by

neevmodi217
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/ 13

Array in Function

Example - 1
#include <iostream>
using namespace std;

void print(int a)
{
a = a + 5;
}
int main()
{
int a = 5;
print(a);
cout << a<< " ";
return 0;
}
Output: 5
Methods to Pass Array to a
Function in C++
In C++, we have the following ways to pass an array as a parameter to
the function:
• As a sized array
• As an unsized array
• As a pointer (pass by pointer)
• As a reference (pass by reference)
Passing as a Sized Array

• In this method, we pass the array in the same way we declare it with
the array type, name, and size.

Syntax:

return_type function_name (datatype array_name [size]);


Example-2 int main()
{
// array declaration
// C++ program to demonstrate the int a[5] = { 1, 2, 3, 4, 5 };
passing of array as sized
printarray(a); // Passing array to function
// array.
// printing array elements
#include <iostream> for (int i = 0; i < 5; i++)
cout << a[i] << " ";
using namespace std;
return 0;
}
// function to update array elements
void printarray (int a[10]) Output:
6 7 8 9 10
{
for (int i = 0; i < 5; i++)
a[i] = a[i] + 5;
}
Passing as an Unsized Array

Syntax

return_type function_name (datatype array_name []);


Example-3
// C++ program to demonstrate the int main()
passing of array as {
// unsized array. // array creation
int a[5] = { 1, 2, 3, 4, 5 };
#include <iostream> int n=5;
printarray(a,n); // Passing array to function
using namespace std;
// printing array elements
// function to update array elements for (int i = 0; i < n; i++)
void printarray(int a[ ],int size) cout << a[i] << " ";
{ return 0;
for (int i = 0; i < size; i++) }
a[i] = a[i] + 5; Output:
} 6 7 8 9 10
Passing Array as a Reference

• Syntax:

return_type function_name (datatype (&array_name) []);


Example-4
int main()
// C++ to demonstrate array passing {
by pass by reference int arr[] = { 1, 2, 3, 4, 5 };
#include <iostream> int n = 5;
using namespace std; modifyArray(arr);
for (int i = 0; i < n; i++)
void modifyArray(int (&arr)[5])
{
{
cout << arr[i] << " ";
// deducing size
int size = sizeof(arr) / sizeof(int); }
for (int i = 0; i < size; ++i) { return 0;
arr[i] *= 2; }
} Output:
2 4 6 8 10
Example-5 (Linear Search)
// C++ code to linearly search
int main(void)
// x in arr[]. If x is present {
// then return its location,
// otherwise return -1 int arr[] = {2, 3, 4, 10, 40};
#include <iostream> int x = 10;
using namespace std;
int n = sizeof(arr) /sizeof(arr[0]);
int search(int arr[], int n, int x) // Function call
{
int i; int result = search(arr, n, x);
for (i = 0; i < n; i++) (result == -1) ? cout << "Element is not present in ar
{
if (arr[i] == x) cout << "Element is present at index " << result;
return i; return 0;
}
return -1; }
} Output:
Element is present at index 3
Example-6 (Binary Search)
// C++ program to implement recursive Binary Search

#include <bits/stdc++.h>
using namespace std;

// Recursive Binary Search function to find the index of an


// element 'x' in a sorted array 'arr' if elements is
// present, otherwise it return -1

// low: The index of the first element in the current sub-array


//high: The index of the last element in the current sub-array
int binarySearch(int arr[], int low, int high, int x)
{
if (high >= low)
{
int mid = (low + high / 2;

if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
if (arr[mid] < x)

return binarySearch(arr, mid + 1, high, x);


}

// If the element is not present in the array, return -1


return -1;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };

// Element to be searched
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array“ : cout << "Element is present at index "
<< result;

return 0;
}

Output:
Element is present at index 3

You might also like