0

I have tried to solve the following problem: "n numbers are given. For every one of them, calculate the sum 1+(1+2)+(1+2+3)+...+(1+2+3+...+x) and output the results in a separate vector". I have gotten a formula for the sum and have implemented it into c++. The code seems to be working perfectly on vs code, but when I upload it to the website where I got the problem from I get 0 points, with the explanation: Caught fatal signal 11. I have read some articles but none of them have helped me crack the problem. Here is the code:

#include <iostream>
#define N 1000000

using namespace std;

int v[N], s[N];

int main()
{
    int n, i;
    cin >> n; // user inputs number of elements
    for(i = 0; i < n; i++)
    {
        cin >> v[i]; // user inputs the elements
        s[i] = v[i]*(v[i] + 1)*(v[i] + 2) / 6; // another vector is calculated using the formula
        cout << s[i] << " ";
    }
    return 0;
}
Vatine
  • 20,782
  • 4
  • 54
  • 70
Cyrex231
  • 1
  • 1
  • 1
    What happens when the user inputs a really large number (larger than `N`)? Or a negative one? (Rhetorical question.) – DevSolar Aug 31 '21 at 12:37
  • 2
    You have huge static allocation for arrays that in the best case is very wasteful, and in the worst case isn't even large enough. Use an actual `std::vector` – AndyG Aug 31 '21 at 12:38
  • 2
    Why do you use the arrays at all? You don't use previous values, so why store it at all? `int num; cin>>num; cout<<(num*(num+1)*(num+2)/6)<<" ";` should have the exact same behaviour. – mch Aug 31 '21 at 12:43
  • 1
    Related interesting reading: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Stef Aug 31 '21 at 12:48
  • 1
    The solution requires no arrays - just read each value `x` and output the desired result. – Alnitak Aug 31 '21 at 12:53
  • @Alnitak: These online judge problems usually require the output to happen in a collection for testing (they don't usually intercept stdout and check that instead). – AndyG Aug 31 '21 at 12:54
  • @Alnitak The problem they are trying to solve requires them to output the results into a separate vector. – fortytoo Aug 31 '21 at 12:54
  • @spitconsumer That may be what the problem requires, but that's not what their current code is doing. – Stef Aug 31 '21 at 12:57
  • @Stef: While the code does indeed output the value, it's also at least attempting to store the results in a separate array: `s[i]` – AndyG Aug 31 '21 at 12:58
  • "output the results in a separate vector". This phrase is meaningless on its own. Are you sure you are copying the problem statement verbatim? – n. m. could be an AI Aug 31 '21 at 13:25

1 Answers1

0

Your problem is two fold. One, you're not using vectors, you're using C-style arrays. And two, you're running out of memory due to the size of your C-style arrays.

C-style arrays are allocated on the stack, and there is only limited memory on the stack. So if you allocate, say, two arrays of size, I don't know, 1000000, you might run into some memory issues.

On the other hand, C++ vectors are more modern, allow for dynamic resizing (useful for when you don't know how many elements you're going to have, I doubt you'll have 1000000), and it is also memory stored on the heap, giving you a lot more breathing room when it comes to memory.

Vectors work like this:

#include <vector>

std::vector<int> vec; // creates the vector

vec.push_back(5); // adds an element equal to 5 to the vector, vec size is now 1
vec.push_back(6); // adds an element equal to 6 to the vector, vec size is now 2

std::cout << vec[0]; // elements are accessed just like a C-style array
fortytoo
  • 452
  • 2
  • 11
  • 2
    Probably also worth throwing in there that you can reserve memory for a vector if you know how many elements you expect. – AndyG Aug 31 '21 at 12:55
  • @AndyG I felt like omitting that fact, considering the whole `#define N 1000000` thing. – fortytoo Aug 31 '21 at 12:57
  • 2
    In the OP's example, the `v` and `s` C style arrays are stored in the BSS of the static storage area. Not on the stack. – Eljay Aug 31 '21 at 14:16
  • Are global variables/arrays/etc. stored there? Huh. Didn't know that. Thanks. – fortytoo Aug 31 '21 at 14:25