/* * To input an array ,and find its maximum using a recursive function * @author-Karan Narain(karan@iitk.ac.in) * */ #include void max(int A[],int low,int high) { int temp; //If low and (high-1) are equal,then the array has only one element,and that is the maximum element if(low==(high-1)) printf("\nThe maximum element of the array is %d\n",A[low]); else { if(A[low]>A[low+1]) { //swap A[low] and A[low+1] temp=A[low]; A[low]=A[low+1]; A[low+1]=temp; } //a recursive call passed with the value of low incremented by 1 max(A,low+1,high); } } int main() { int A[10],i=0,temp,len; //len holds the number of elements in array A.It would be equal to the last value of i before the while loop ends or breaks while(i<10) { printf("Enter an element(To stop entering,enter a non-positive number)\n"); scanf("%d",&temp); if(temp<=0) break; else { A[i]=temp; i++; } } len=i; //if no element was entered if(len==0) printf("The array has no elements\n"); else { printf("The array you entered is :\n"); for(i=0;i