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

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.
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...
In this third part, write a function that accepts an array of integers and its size as arguments.
in C++In this third part, write a function that accepts an array of integers and its size as arguments. The function should create a new array that is of half size the argument array (round up the fraction if the size of the argument array is odd). The value of the first element (Element 0) of the new array should be the sum of the first two elements of the argument array (Element 0 and 1). Element 1 of the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1 small, 2 medium, 3 large), one for the number of meat toppings, one for the number of other toppings and another for the quantity of pizzas ordered. It should calculate and return the total due for that pizza order based on the following information: Small pizza base price: $6.50 Medium pizza base price: $9.50 Large pizza base price: $11.50 The base pizza price includes...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT