Question

I need help implementing a few functions in c++. Any help would be appreciated 1. a...

I need help implementing a few functions in c++. Any help would be appreciated

1. a function void PairsAddUpToK(int a[], int a_len, int k) that finds all pairs of numbers in an array that add up to a given number k

2. A function void TriplesAddUpToK(int a[], int a_len, int k) that finds all sets of three numbers in an array that ad up to a given number k

3. A function void SubsetsAddUpToK((int a[], int a_len, int k) that finds all possible subsets of numbers that add up to k

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

OUTPUT:

Raw_Code:

#include<iostream>
using namespace std;
void PairsAddUpToK(int a[],int a_len,int k){
   int i,j;           //Function to find pairs that Add up to k
   for(i=0;i<a_len;i++){
       for(j=i+1;j<a_len;j++){
           if((a[i]+a[j])==k){
               cout<<"("<<a[i]<<","<<a[j]<<") ";
           }
       }
   }
}
void TriplesAddUpToK(int a[],int a_len,int k){
   int i,j,p;               //Function to find Triples that Add up to k
   for(i=0;i<a_len;i++){
       for(j=i+1;j<a_len;j++){
           for(p=j+1;p<a_len;p++){
               if((a[i]+a[j]+a[p])==k){
                   cout<<"("<<a[i]<<","<<a[j]<<","<<a[p]<<") ";
               }
           }
       }
   }
}
void SubsetsAddUpToK(int a[],int a_len,int k){
   int i,j,p,sum;               //Function to find all the Subsets that Add up to k
   for(i=0;i<a_len;i++){
       if(a[i]==k){               //Loop to find sub sets with length 1
           cout<<"("<<a[i]<<") ";
       }
   }
   PairsAddUpToK(a,a_len,k);       //Calling Pairs and triples functions as these are also subsets
   TriplesAddUpToK(a,a_len,k);
   for(i=0;i<a_len;i++){
       for(j=i+3;j<a_len;j++){
           sum=0;
           for(p=i;p<=j;p++){       //Loops to Calculate Subsets with length more than 3

               sum=sum+a[p];
           }
           if(sum==k){
               cout<<"(";
               for(p=i;p<=j;p++){
                   cout<<a[p]<<",";
               }
               cout<<") ";
           }
       }
   }
}
int main(){
   int a[100],n,k,i;
   cout<<"Enter Number of Value:";   //Taking length of array as input
   cin>>n;
   cout<<"Enter The elements:";
   for(i=0;i<n;i++){           //Taking Array elements as input
       cin>>a[i];
   }
   cout<<"Enter The Value of K:";
   cin>>k;                   //Taking k value as input
   cout<<"\nThe PairsAddUpToK are:";
   PairsAddUpToK(a,n,k);
   cout<<"\nThe TriplesAddUpToK are:";           //Calling Required Functions
   TriplesAddUpToK(a,n,k);
   cout<<"\nThe SubsetsAddUpToK are:";
   SubsetsAddUpToK(a,n,k);
   return 0;
}

***Comment me in the Comment box for any Queries ***

Add a comment
Know the answer?
Add Answer to:
I need help implementing a few functions in c++. Any help would be appreciated 1. a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help implementing these functions in c++. One finds the intersection of two SortedType lists,...

    I need help implementing these functions in c++. One finds the intersection of two SortedType lists, and the other finds the union. SortedType Intersection(SortedType & list1, SortedType & list2); SortedType Union(SortedType & list1, SortedType & list2); Any help is appreciated

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a...

    Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a variable passed by reference- the GCD of all integer numbers in an array void GCD(int A[], int size, int &g) and another function that allows you to divide all numbers of an array by a given integer void Div(int A[], int size, int gcd) (assume the functions are in a library, therefore you don’t need to implement them, but just use them). 1. Write...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • I need help writing these functions in C programming. Write a C function that checks if...

    I need help writing these functions in C programming. Write a C function that checks if an array is sorted or not using an iterative approach. Write a C function that checks if an array is sorted or not using a recursive approach Write a C function to reverse the elements of an array. Note you are not allowed to use an 1. additional array to do that Write a C function to find the sum of the elements of...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I need help with this problem. Any help would be appreciated thank you REAL NUMBERS AND...

    I need help with this problem. Any help would be appreciated thank you REAL NUMBERS AND LINEAR EQUATIONS Solving equations with zero, one, or infinitely many solutions For each equation, choose the statement that describes its solution. If applicable, give the solution. -8(u + 1) = 2(1 - 4u) - 9 30 몸 No solution v=0 All real numbers are solutions -6(x + 1) + 8x = 2(x - 3) No solution x= 0 All real numbers are solutions Explanation...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT