#include <iostream>
using namespace std;
void fun(int arr,vector<int> sum,int i,int len){
if(i==len){
cout<<"0 ";
sort(sum.begin(), sum.end());
for (auto a = sum.begin(); a != sum.end(); a++)
cout << *i << " ";
return;
}
else{
int s=0;
for(int j=i;j,len;j++)
{
s+=arr[i];
sum.push_back(s)
}
fun(arr,sum,i+1,len)
}
}
int main() {
//code
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
vector<int> sum;
int len=arr.size();
fun(arr,sum,0,len);
}
return 0;
}
My code isn’t compiling. please help me find the error.
Your code several syntax and logic flaws. Here are some I noticed:
- Your include statement is empty. You need to include
iostream
, vector
, and bits/stdc++.h
.
- You have declared
arr
as an int
but are using it as an array.
- You probably mean to print
"* " << i
instead of *i
.
- You have
y,len
when you probably want j < len
.
- You have missing semicolons.
-
You probably mean while (t--)
instead of while(t-)
. False error from not using ```
-
arr.size()
isn’t a valid method. Why not use n
?
Also, a few notes on style:
- Your function name hurts readability. I don’t know what this function is for.
- Comments would also help debugging and understanding.
- I would use more whitespace to help separate function arguments, loop head elements, etc.
Even when I fix the errors I listed above, there are compilation issues from the misuse of vector
. Edit: bug from formatting.
What are you trying to get this code to do? It’s not clear to me.
Protip: To post code on the forum, you want to put ```
these symbols on a blank line before and after your code.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Note: Backticks are not single quotes.
Given an array of integers, print sums of all subsets in it. Output should be printed in increasing order of sums.
Input : arr = {2, 3}
Output: 0 2 3 5
Input : arr = {2, 4, 5}
Output : 0 2 4 5 6 7 9 11
I am trying to solve this question.
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
void fun(int arr[],int i,int len,vector<int>s){
if(i==len)
{
sort(s.begin(), s.end());
for (auto a = s.begin(); a != s.end(); a++)
cout << *a << " ";
return;
}
else
{
int sum=0;
for(int j=i;i<len;j++)
{
sum+=arr[j];
s.push_back(sum);
}
}
}
int main() {
//code
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
vector<int>s;
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"0";
fun(arr,0,n,s);
}
return 0;
}
I am getting Segmentation Fault now.
When in doubt, add cout
. Does this help you see why you are segfaulting?
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
void fun(int arr[], int i, int len, vector<int> s){
cout << " ---- Inside fun: i = " << i << endl;
if (i == len) {
cout << " ---- Case 0: i == len" << endl;
sort(s.begin(), s.end());
for (auto a = s.begin(); a != s.end(); a++)
cout << *a << " ";
return;
} else {
cout << " ---- Case 1: i < len" << endl;
int sum = 0;
for (int j = i; i < len; j++) {
cout << " ----- Inside loop: j = " << j << endl;
sum += arr[j];
s.push_back(sum);
cout << " ----- Inside loop: sum = " << sum << endl;
}
}
}
int main() {
//code
cout << "Reading Number of Test Cases" << endl;
int t;
cin >> t;
while (t--) {
cout << " -- Test Case " << t << endl;
cout << " --- Reading Number of Values" << endl;
int n;
cin >> n;
cout << " --- Reading Values" << endl;
int arr[n];
vector<int> s;
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << " --- Calling Fun" << endl;
fun(arr, 0, n, s);
}
return 0;
}
Also, if you haven’t tried it, I recommend repl.it for this sort of quick debugging of small C++ codes.