Recursive Function to Print an Array


//  main calls a recursive function to print an array

#include <iostream.h>

void recArrayPrint(int[],int,int);
int main()
{
	int values[10] = {1,2,3,4,5,6,7,8,9,10};

	recArrayPrint(values,10,0);
	return 0;
}

//  print values in array
//  preconds:	array is an array of ints
//		size >= 0 (number of elements in array)
//		0 <= position <= size
//  postconds:	parms are unchanged
//		prints array[position],...,array[size-1]

void recArrayPrint(int array[],int size,int position)
{
	if (position < size)
	{
		cout << array[position] << "   ";
		recArrayPrint(array,size,position+1);
	}

//  base case is empty array (size == position), so do 
//  nothing

}


Output


1   2   3   4   5   6   7   8   9   10


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page