Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive function called inverse_pair with a single parameter L, which

ID: 3679481 • Letter: W

Question

Write a recursive function called inverse_pair with a single parameter L, which is a list of integers. The function returns True if L contains a pair of integers whose sum is zero and False otherwise. The base case occurs when the list has exactly two integers (since it doesn’t make sense to talk about a “pair” of integers for lists with fewer than two elements). For example, inverse pair should return False for the list [12, 8, 10, -5] and True for the list [12, 5, 10, -5, -9]. Your function should consist only of the base case and the recursive calls. As above, all usual restrictions apply for the recursive implementation.

Explanation / Answer

boolean inverse_pair(integerList L)

{

                int n,sum=0,x,flag=0;

                struct integerList *t;

                //base case list containing two integers

                if(L->next->next == null)

                {

//if sum of two integers is 0 return true else return false

                                sum = (L->data)+(L->next->data);

                                if(sum == 0)

                                                return true;

                                else

                                                return false;

                               

                }

//lists containing more than two integers

                else

                {

//get first element

                                n=L->data;

//put second element into a list node

                                t=L->next;

//find if first element gives sum 0 with any other element in the list

                                while(L)

                                {

                                                x=L->next->data;

                                                sum = n+x;

                                                if(sum == 0)

                                                                flag = 1;

                                                L=L->next;

t->next=L;

                                }

                                if(flag == 1)

                                                return true;

                                else

//start list from second place

                                return inverse_pair(t);

                }

                return false;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote