0

I am beginner in C++ and have been trying to get an output of all the sums from 4 different lists of numbers. I want to know all the possible sums using up to 1 from each list. Repeats can be omitted.

For example with an input of [1, 2], [1, 3], [2, 3], [2, -1] should output [-1, 0, 1, 2, 3, ... 10].

My lists are 4, 6, 6, and 9 digits long, should that make a difference?

I have tried

#include<bits/stdc++.h> 
using namespace std; 

void subsetSums(int arr[], int l, int r, 
                int sum=0) 
{ 
    // Print current subset 
    if (l > r) 
    { 
        cout << sum << " "; 
        return; 
    } 

    subsetSums(arr, l+1, r, sum+arr[l]); 

    subsetSums(arr, l+1, r, sum); 
} 

int main() 
{ 
    int arr[] = {7, 14, 21, 28}, {-10, -20, -30, -40, -50, -60}; 
    int n = sizeof(arr)/sizeof(arr[0]); 

    subsetSums(arr, 0, n-1); 
    return 0; 
} 

But it only produces an error:

expected unqualified-id before ‘{’ token
int arr[] = {5, 4, 3}, {4, -1, 5};
JaMiT
  • 9,693
  • 2
  • 12
  • 26
  • 3
    [Why should I not #include?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Rohan Bari Sep 20 '20 at 03:58
  • What's the intent with `int arr[] = { ... }, { ... }`? Are you aware of what the comma operator does? – tadman Sep 20 '20 at 04:00
  • 2
    Tip: In C++ use `std::vector` instead of naked C arrays. – tadman Sep 20 '20 at 04:00
  • If this is supposed to be an array of arrays then you need to use a different structure, like `std::vector>`. – tadman Sep 20 '20 at 04:02
  • @tadman the comma was to create a secondary array. No am not aware what it does i only started to mess with C++ a week ago. Also i do not understand what a naked array is. yes it is supposed to be an array of arrays. – Kyle Aikey Sep 20 '20 at 07:18
  • @JaMiT the error is error: expected unqualified-id before ‘{’ token int arr[] = {5, 4, 3}, {4, -1, 5}; – Kyle Aikey Sep 20 '20 at 07:20
  • 1
    "the comma was to create a secondary array". But the type is `int arr[] `. This is one single array of type int. That combined with you using raw arrays, `#include` and `using namespace std` makes me think whatever book you use is bad. Consider beginning fresh with a good tutorial. – Aziuth Sep 20 '20 at 08:06
  • @KyleAikey The error message should be in the question. – JaMiT Sep 20 '20 at 20:33

2 Answers2

1

The simplest mechanism would be to create an array of all possible sums, and then remove duplicates.

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::vector<std::vector<int>> arrs = {
    {7, 14, 21, 28},
    {-10, -20, -30, -40, -50, -60},
    {50, 90}
  };

  // Let's start with 0 in the results, as if we used no value from any of
  // the arrays
  std::vector<int> results = {0};

  // Append new sums to the results list
  for (const auto &arr : arrs) {
    const int length = results.size();
    for (int i = 0; i < length; i++)
      for (int j = 0; j < arr.size(); j++)
        results.push_back(results[i] + arr[j]);
  }

  // Remove duplicates
  std::sort(results.begin(), results.end());
  results.erase(
    std::unique(results.begin(), results.end()),
    results.end());

  // Print the results
  for (int value : results)
    std::cout << value << " ";
  std::cout << "\n";
}
Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
  • 1
    Why not use `std::set` for the results, not requiring any removal of duplicates? (As the guy who asked the question is new to C++ maybe both for the educational factor.) – Aziuth Sep 20 '20 at 08:08
  • I also thought about whether to bracket all the for loops in code one gives to a beginner. Not sure about that one. – Aziuth Sep 20 '20 at 08:11
  • @Aziuth: Those are all reasonable ideas. – Bill Lynch Sep 20 '20 at 09:13
  • That's a whole lotta code to do a whole lotta nothing. Meh. It's like brandishing the "C++" banner, running against the opponents, and stopping mid-way down the battlefield to go get an ice cream. You've let the C hordes win. Make it half the size or less. A few lines of actual "work" is all the OP is asking for. Don't disappoint them :) – Kuba hasn't forgotten Monica Sep 20 '20 at 20:45
  • @UnslanderMonica: I encourage you to post an answer to this question. – Bill Lynch Sep 21 '20 at 02:49
  • @BillLynch what you gave me did wonders for several of the arrays. however when i got to one with a decimal (4.2) it didnt work. – Kyle Aikey Sep 21 '20 at 06:05
  • @KyleAikey: You are unable to store a decimal value as an integer. Either use floating point numbers or use a decimal number class. – Bill Lynch Sep 21 '20 at 06:30
0

Let us parse the line

int arr[] = {7, 14, 21, 28}, {-10, -20, -30, -40, -50, -60};

This is a variable definition. The type is int. The first variable being defined is arr, which due to the [] is actually an array with the size to be determined by the initialization. The = starts the initialization. The {7, 14, 21, 28} gives the four values to be stored in the array and at the same time determines that the array has four elements. The , says "I'm done defining that variable and moving on to the next variable of the same basic type". There is no name for this next variable, hence the error. The {-10, -20, -30, -40, -50, -60} would define the initial values for another array, but since you did not name another variable, the compiler gets rather confused.

One way to define two arrays is like:

int arr[] = {7, 14, 21, 28}, other_array[] = {-10, -20, -30, -40, -50, -60};

but using two lines often works out better for the human reader (the below means the same thing as the above)

int arr[] = {7, 14, 21, 28};
int other_array[] = {-10, -20, -30, -40, -50, -60};

Since you need four lists, you probably will end up with something like:

int arr1[] = {7, 14, 21, 28};
int arr2[] = {-10, -20, -30, -40, -50, -60};
int arr3[] = {-15, -25, -35, -45, -55, -65};
int arr4[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

Note that you will have four lengths to keep track of. This is where std::array can become convenient.

JaMiT
  • 9,693
  • 2
  • 12
  • 26