Question

In: Computer Science

In this task you will implement a C++ function with arguments: a sorted integer array, size...

In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than one loop. Use pointer notation of arrays to do this question.

Solutions

Expert Solution

Function for your combinations is provided below. As you have asked it uses pointers for traversing array passed in function. The whole code is explained in the code comments. I have tested the function and it runs smoothly. For your better understanding i have a made main method also in the last part of answer, and tested this function. i have attached the screenshot of test in the last.

#################################################################################

FUNCTION DEFINITION:--

//function taking 3 parameters
void combinationFinder(int a[],int length,int target)
{
    int combination[length]; //array of combination
    int c=0; //iterator for array

    for(int i=0;i<length;i++) //for each element
    {
        for(int j=i+1;j<length;j++) //check with all the rest elements
        {
            if((*(a+i)+*(a+j))==target) //if sum is equal to target
            {
                if(c==0) //if array is empty
                {
                    combination[c++]=*(a+i); //add first no
                    combination[c++]=*(a+j); //add second no
                }
                else //if array is not empty
                {
                    combination[c++]=200; //add 200 as separator
                    combination[c++]=*(a+i); //add first no
                    combination[c++]=*(a+j); //add second no
                } 
            }
        }
    }

    if(c>0) //if there are elemnts in array
    {
        for(int i=0;i<c;i++)
            cout<<" "<<combination[i]; //print each elemnt
    }
}

##############################################################################

WHOLE CODE WITH TESTING AND OUTPUT

#include <iostream>

using namespace std;
//function taking 3 parameters
void combinationFinder(int a[],int length,int target)
{
    int combination[length]; //array of combination
    int c=0; //iterator for array

    for(int i=0;i<length;i++) //for each element
    {
        for(int j=i+1;j<length;j++) //check with all the rest elements
        {
            if((*(a+i)+*(a+j))==target) //if sum is equal to target
            {
                if(c==0) //if array is empty
                {
                    combination[c++]=*(a+i); //add first no
                    combination[c++]=*(a+j); //add second no
                }
                else //if array is not empty
                {
                    combination[c++]=200; //add 200 as separator
                    combination[c++]=*(a+i); //add first no
                    combination[c++]=*(a+j); //add second no
                } 
            }
        }
    }

    if(c>0) //if there are elemnts in array
    {
        for(int i=0;i<c;i++)
            cout<<" "<<combination[i]; //print each elemnt
    }
}
int main()
{

   int a[]={-1,1,2,3,4,5,6,7,8,9,10}; //array declaration
   combinationFinder(a,11,8); //calling function for target 8
    return 0;
}


Related Solutions

C++ Write a function that accepts an array of doubles and the array's size as arguments....
C++ Write a function that accepts an array of doubles and the array's size as arguments. The function should display the contents of the array to the screen.
you are asked to implement a C++ class to model a sorted array of unsigned integers....
you are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Class will provide (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments....
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments. Your function will then return the n largest values from that array as an array containing n elements. It is safe to assume that arr will have at least n elements. The resulting array should have the largest number on the end and the smallest number at the beginning. For Example: max_n(np.array([1,2,3,4,5]), 3) returns np.array([3,4,5]) max_n(np.array([10,9,8,7,6,5]), 4) returns np.array([7,8,9,10]) max_n(np.array([1,1,1]), 2) returns np.array([1,1])
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT