Question

I need a program in c++ the same as below code but I want it to...

I need a program in c++ the same as below code but

I want it to prompt the user to enter the number of elements

after that I want it to ask the user to enter the array elements


#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[50]={2,5,4,3};
bool x[100];
int N=4;//number of elements

  
int k=10;//target sum
int sum;//current target sum

int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
void backtrace(int n)
{
if(sum>k)
return ;
if(sum==k)
{
for(int j=0;j<n;++j)
{
if(x[j])
cout<<a[j]<<" ";
}
cout<<endl;
return;
}
if(n==N)
return ;
for(int i=n;i<N;++i)
{
if(x[i]==false)
{
x[i]=true;
sum+=a[i];
backtrace(i+1);
x[i]=false;
sum-=a[i];
while(i<N-1 && a[i]==a[i+1])
i++;
}
}
}
int main()
{
sum=0;

memset(x,0,sizeof(x));
qsort(a,N,sizeof(a[0]),cmp);
backtrace(0);
return 0;
}


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

#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[50];
bool x[100];
int N;//number of elements

  
int k=10;//target sum
int sum;//current target sum

int cmp(const void *a,const void *b)
{
    return *(int *)b-*(int *)a;
}
void backtrace(int n)
{
   if(sum>k)
   return ;
   if(sum==k)
   {
       for(int j=0;j<n;++j)
       {
           if(x[j])
               cout<<a[j]<<" ";
       }
       cout<<endl;
       return;
   }
   if(n==N)
       return ;
   for(int i=n;i<N;++i)
   {
       if(x[i]==false)
       {
           x[i]=true;
           sum+=a[i];
           backtrace(i+1);
           x[i]=false;
           sum-=a[i];
           while(i<N-1 && a[i]==a[i+1])
           i++;
       }
   }
}
int main()
{
    sum=0;
    cout<<"Enter no of elements: ";
    cin>>::N;//::N consider variable N as the global variable
    cout<<"Enter Array Elements: ";
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    memset(x,0,sizeof(x));
    qsort(a,N,sizeof(a[0]),cmp);
    backtrace(0);
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
I need a program in c++ the same as below code but I want it to...
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 fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • C++ Programming I have finished the code but there's one error which shows that "strcpy" might...

    C++ Programming I have finished the code but there's one error which shows that "strcpy" might be unsafe. Consider using strcpy_s instead. I've tried that but it's not working probably because I didn't implement it correctly. I am posting my code below. It'd be really helpful if you could fix all the strcpy errors and post it below. Thank you. Text.cpp: #include <iostream> #include <iomanip> #include <cassert> #include <cstring> #include "Text.h" Text::Text ( const char *charSeq ) {    bufferSize...

  • Need help in c++ programming to output the lines in my code: if word if found:...

    Need help in c++ programming to output the lines in my code: if word if found:            cout << "'" << word << "' was found in the grid" << endl;            cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • This is a C++ code, ıt works successfully, I want to convert to C language It...

    This is a C++ code, ıt works successfully, I want to convert to C language It is very emergency!! Please help #include<iostream> using namespace std; void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) { int rem_bt[n]; for (int i = 0 ; i < n ; i++) rem_bt[i] = bt[i];    int t = 0; while (1) { bool done = true; for (int i = 0 ; i < n; i++) { if (rem_bt[i] > 0)...

  • I need assistance with the C++ code below to remove the "this" pointers while maintaining the...

    I need assistance with the C++ code below to remove the "this" pointers while maintaining the code's current output and functionality. Also, there should be a private class in the header file, but I'm not sure what I should include there. Any help would be greatly appreciated! ***main.cpp driver file*** #include <iostream> #include "vector.h" using namespace std; int main() {   vectorInfo v1(7, 6);   vectorInfo v2(5, 4);       v1.set(3, 2);   cout << "v1 : ";   v1.print();       cout << "v2 :...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using...

    Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using namespace std; int ksmall(int*, int, int , int); void swap(int*, int*); int main() {     int SIZE = 10;     int target;     int begining=0;     int ending=SIZE-1;     int *array1= new int[SIZE];     cout << "Enter 10 integers: " << endl;     for (int i=0; i<SIZE; i++)     {        cin>>array1[i];     }     cout << " What is the Kth smallest number...

  • 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...

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