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

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

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

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

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

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

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