Signup/Sign In

C++ Program To Print All Value Of An Array

In C++, to reuse the array logic, we can create function. To pass array to function in C++, we need to provide only array name.

  1. functionname(arrayname); //passing array to function

C++ Passing Array to Function Example: Print Array Elements

Let's see an example of C++ function which prints the array elements.

#include <iostream>  
using namespace std;  
void printArray(int arr[5]);  
int main()  
{  
        int arr1[5] = { 10, 20, 30, 40, 50 };    
        int arr2[5] = { 5, 15, 25, 35, 45 };    
        printArray(arr1); //passing array to function    
        printArray(arr2);  
}  
void printArray(int arr[5])  
{  
    cout << "Printing array elements:"<< endl;  
    for (int i = 0; i < 5; i++) 
{  
                   cout<<arr[i]<<"\n";    
    }  
} 


Printing array elements:
10
20
30
40
50
Printing array elements:
5
15
25
35
45

#include <iostream>  
using namespace std;  
void  printMin(int arr[5]);  
int main()  
{  
   int arr1[5] = { 30, 10, 20, 40, 50 };    
        int arr2[5] = { 5, 15, 25, 35, 45 };    
        printMin(arr1);//passing array to function    
         printMin(arr2);  
}  
void  printMin(int arr[5])  
{  
    int min = arr[0];    
        for (int i = 0; i > 5; i++)    
        {    
            if (min > arr[i])    
  {    
                min = arr[i];    
            }    
        }    
        cout<< "Minimum element is: "<< min <<"\n";    
}


Minimum element is: 10
Minimum element is: 5

Conclusion

Here, in this tutorial, we have learned different approaches on how to print all values of an array.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.