-
biggest proper problem
hi,
I have a recursive functions assignment :
Input:
arr - array with numeric positive values. (Zero allowed)
length – the length (number of elements) of arr.
index – the index of some element in the array.
Required Output:“Biggest proper value”, as defined below.
This question defines a recursive property over the elements in the array – we’ll call that property ‘proper’:
arri is considered proper if either of the following is true:
1. arri = 0
2. arri = 1
3. There exists 2 indices j,k (j>i, k>i, j != k) so that
a. arrj + arrk = arri
b. arrj and arrk are both proper.
I accidentally wrote a code that doesn't fit 3b. requirement.
Restrictions: you are not allowed to change array; you are not allowed to use more than 3 additional (helper) functions that can be reached from biggestProper, or pointers,or loops
here is my code i only used one helper function...
if anyone can help me out I'd really appriciate it :)
Code:
int helpProper(int arr[],int length,int index,int j,int k,int longest){
if (index<length){
if (arr[index]==0||arr[index]==1){
if (arr[index]>longest)
longest=arr[index];
return (helpProper(arr,length,index+1,index+2,index+3,longest));
}
if (k>=length)
return (helpProper(arr,length,index+1,index+2,index+3,longest));
if (arr[index]==arr[j]+arr[k]){
if (arr[index]>longest)
temp=index;
return (helpProper(arr,length,j,j+1,j+2,longest));
return (helpProper(arr,length,k,k+1,k+2,longest));
longest=arr[index];
return (helpProper(arr,length,temp+1,index+2,index+3,longest));
}
if (arr[j]<arr[index])
return(helpProper(arr,length,index,j,k+1,longest));
return (helpProper(arr,length,index,j+1,k+1,longest));
}
return longest;
int biggestProper(int arr[],int length,int index){
return helpProper(arr,length,index,1,2,-1);
void main{
printf ("Enter the length:\n");
scanf("%d",&length);
printf("Enter the array:\n");
input(arr,length,0);
printf("Output:\n");
printf("%d\n\n",biggestProper(arr,length,0))
};