0

I don't understand why im getting sigabrt error in this code for spoj question https://pl.spoj.com/problems/OSTSIL/ please show me what im doing wrong.

i cant use any external libraries because it's spoj problem I have very basic understanding what heap and stack is (i'm in second grade of high school so please keep it simple)

#include<iostream>
using namespace std;


int main()
{
    short tests;
    cin >> tests;
    if (tests > 1000)
        return 0;
    int *outputs = new int[tests];
    short buffer = tests;
    for (tests; tests > 0; tests--)
    {
        short j = 0, output = 1;
        short i = 1;
        short n;
        cin >> n;
        for (i; i <= n; i++)
        {
            output *= i;
            while (output % 10 == 0)
            {
                output /= 10;
            }
            do
            {
                output--;
                j++;
            } while (output % 10 != 0);
            output = j; j = 0;
        }
        outputs[tests] = output;
    }
    for (buffer; buffer > 0; buffer--)
        cout << outputs[buffer] << endl;
    delete[] outputs;
    return 0;
}
  • 4
    Hint: Array indexing start at 0, not 1. – 1201ProgramAlarm Sep 23 '19 at 18:43
  • btw unrelated to the error, your loops look a little odd. `for (tests; tests > 0; tests--)` is the same as `for (;test > 0; test--)`, but why not use a loop counter in the scope of the loop? Anyhow you need the value of `tests` again – 463035818_is_not_a_number Sep 23 '19 at 18:46
  • *i cant use any external libraries because it's spoj problem* -- You are allowed to use whatever C++ you like with the SPOJ problems. Second, your problem *could have* been solved by yourself if you *did* use `std::vector` and used `at()`. Example: `std::vector outputs(tests);` and then `outputs.at(tests) = output;` -- You would have been greeted with a `std::out_of_range` exception instead of a "sibabort". – PaulMcKenzie Sep 23 '19 at 18:57
  • Interesting thing about C++ is you don't need to know about heap and stack. The language has abstracted heap and stack away and preplaced them with the notions of Automatic allocation(the system takes care of it) and Dynamic allocation (the programmer takes care of it). You generally want to use Automatic allocation because it is typically faster and the system is less likely to make a mistake managing the allocation than a human is. This usually comes down to [don't use `new` if you don't have to.](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Sep 23 '19 at 19:02
  • thank you all for help especially thanks for the "Hint" – Wojciech Michalak Sep 25 '19 at 19:01

0 Answers0