print an array in cpp code example

Example 1: print array c++

void printArray(int a[],int n){
	for(int i=0;i<n;i++)
      cout<<a[i]<<" ";
  	cout<<endl;
}

Example 2: c++ print every element in array

#include<iostream>

int main()
{
 	int array[8] = {1,2,3,4,5,6,7,8};
  	int i = 0;
  
  	while (i <= sizeof(array)/sizeof(int))
    {
     	std::cout << array[i];
      	i++;
    }
}

Example 3: take input from user in array c++

// declare and initialize an array without defining size
int x[] = {19, 10, 8, 17, 9, 15};

Tags:

Cpp Example