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

Using C/C++, define an integer array (size between 10 and 20) and a target numbe

ID: 3889382 • Letter: U

Question

Using C/C++, define an integer array (size between 10 and 20) and a target number (e.g. 50), assign the array with random numbers in between [0, 10]), then define a function to find all unique number pairs or tuples (more than 2 numbers) of the array such that the numbers of each pair/tuple add up to the target number. Note that besides printing the numbers of each pair/tuple, you are also required to print the original index information of the numbers. For example, given array A = {5, 3, 4, 2, 6, 7}, target = 9, the number pairs are (5, 4), (3, 6), (2, 7), and (3, 4, 2). Their indices are (0, 2), (1, 4), (3, 5), and (1, 2, 3), respectively.

Explanation / Answer

int countPairsWithDiffK(int arr[], int n, int k)

{

    int count = 0;

     

    // Pick all elements one by one

    for (int i = 0; i < n; i++)

    {      

        // See if there is a pair of this picked element

        for (int j = i+1; j < n; j++)

            if (arr[i] - arr[j] == k || arr[j] - arr[i] == k )

                  count++;

    }

    return count;

}

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