• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Functions » Array as Argument in A Function in C++
Next →
← Prev

Array as Argument in A Function in C++

By Dinesh Thakur

Arguments in a function can also be of array type. When the arrays are passed by value in a function, it is written as shown in the following example:

float f1(int* c, int* d){}
void main() {
  int c[5];
  int d[4];
  f1(c, d);
}

 Example 

The program illustrates merging two arrays using Merge sort. Two or more sorted arrays are given as input for merge sort. The procedure is explained below when two sorted arrays X and Y are given as input.                             Sorted Array X and YThe array X has m elements and the array Y has n elements. Z is the newly created array. The algorithm is as follows:

if(x<sub>i</sub><y<sub>j</sub>) {
  Z<sub>k</sub>=X<sub>i</sub>
  increment i and k
}
else {
 if (Y<sub>j</sub><X<sub>i</sub>)
   Z<sub>k</sub> = Y<sub>j</sub>
   increment j and k
}
else if (X<sub>i</sub> = Y<sub>j</sub>) {
   Z<sub>k </sub>= X<sub>i</sub>
   Z<sub>k+1</sub>= Y<sub>j</sub>
increment i, j and k
}

The program uses call by value function.

#include<iostream.h>
const int size = 10;
void merge(int* a, int* b) {
  int i=0,j=0,k=0,I=0;
  int c[10];
  while(i<5 && j<5) {
    if(a[i] < b[j]) {
       c[k]=a[i] ;
       i++;
       k++;
     }
   else if(b[j]<a[i]) {
       c[k]=b[j];
       j++;
       k++;
   }
   else {
        c[k]=a[i] ;
        k++;
        c[k]=b[j];
        k++;
        i++;
        j++;
   }
}
for(I=i;I<5;I++) {
  c[k]=a[I];
  k++;
}
for(I=j ;i<5;I++) {
   c[k]=b[I];
   k++;
}
cout<<"\nSorted array: ";
  for(k=0;k<10;k++)
    cout<<c[k]<<" ";
}
void main() {
  int a[5];
  int b[5];
  int c[10];
  cout<<"Enter the sorted values for first array";
  for(int i=0;i<5;i++)
  cin>>a[i];
  cout<<"\nEnter the sorted values for second array";
  for(i=0;i<5;i++)
  cin>>b[i];
  merge( a,b);
}

When the arrays are passed by reference to a function, it is written as follows :

float f1(int* c, int* d) {
  void main() {
    int c[5];
    int d[4];
    fi(c, d);
}

 

In this case, the address of c and d are given as arguments in the function. 

Example  

Following is an example to illustrate two dimensional array passed as reference.

#include <iostream.h>
void Transpose(int** &);
const m=3, n=3;
void main() {
  int i, j;
  int **A;//Pointer to pointer to int
  A = new int* [m];
  for(i=0;i<m;i++ )
    A[i] = new int[n];//An mxn Matrix
    // A[i] is a pointer to row of int
    for(i=0;i<m;i++ )
    for(j=0;j<n;j++ ) {
     cout<<" A["<<i<<", "<<j<<"] = ";
     cin>>A[i][j];
    }
    for(i=0;i<m;i++ ) {
      for(j=0;j<n;j++ )
        cout<<" " <<A[i][j];
        cout<<endl;
}
Transpose(A);
cout<< endl<<"Transpose of the matrix is: "<<endl;
   for(i=0;i<m;i++ ) { 
     for(j=0;j<n;j++)
       cout<<A[i] [j]<<" ";
       cout<<endl;
   }
}
void Transpose(int** &a) { 
   int i,j,t;
   for (i=0;i<m;i++)
     for(j=0;j<i;j++ ) {
        t=a[i][j];
        a[i][j] = a[j][i];
        a[j] [i] = t;
     }
}

You’ll also like:

  1. When does a control’s Validate event occur? How is the Cancel argument used
  2. Write A C++ Program To Illustrate The Concept Of Passing Of One Dimensional Array To Function.
  3. C Program Pass Array to Function
  4. C Program How to Pass one Dimensional Array to Function in C
  5. C Program How to Pass Two Dimensional Array to a Function in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW