#What causes RTE ?

1 messages · Page 1 of 1 (latest)

spice ivyBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

summer rapids
oak birch
#

Wow, recursive function to just do next tests

#

Have you heard of a loop

#

Your function is needlessly recursive, which might blow up the stack (and likely does)

#

You use VLAs which are a bad idea overall

#

And your function can be removed almost entirely by compiler, since your path doesn't have a return

#
  • your function really doesn't return any meaningful value, so why is it declared to return an int?
summer rapids
#

@oak birch "rte"s aren't caused by such things as far as i know

#

and this is for competitive programming so the idea that it is unsafe or bad implemented doesn't really matter only the result does

#

can you still help me @oak birch ?

oak birch
#

Yes. And yes, it matters. Very likely you can blow up the stack, and you have UB in your program that literally allows the compiler to remove your entire if branch

#

A cool trick for reading multiple lines that have the same format is:

while (cin >> t >> x >> y >> z)
{
    if (t>0) ...
#

This way you get rid of useless and problematic recursion

oak birch
#

Change the function to return void

#

If that's not that, you have logic error and access an array of of bounds

summer rapids
#

thank you bro

#

I tried to do that for another problem but I get WA

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

int main() {
    int n, x, y, z;
    while (cin >> n >> x >> y >> z) {
        int ans = x + 1;
        for (int i = 0; i < y; i++) {
            int k; cin >> k;
            bool good = true;
            for (int j = 0; j < z; j++) {
                int f; cin >> f;
                if (f < n) {
                    good = false;
                }
            }
            int sum = n * k;
            if (sum > x)
                good = false;
            if (good)
                ans = min(ans, sum);
        }
        if (ans != x + 1) {
            cout << ans << endl;
            return 0;
        }
        cout << "stay home" << endl;
    }
    return 0;
}```
#

however this dude's code works perfectly...

#
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{  
    int N, B, H, W;
    while (cin >> N >> B >> H >> W)
    {
        int minCost = B + 1;
        for (int h = 0; h < H; ++h)
        {
            int p;
            cin >> p;
            for (int w = 0; w < W; ++w)
            {
                int a;
                cin >> a;
                if (a >= N && p * N <= minCost)
                    minCost = p * N;
            }
        }
        if (minCost <= B)
            cout << minCost << endl;
        else
            cout << "stay home" << endl;
    }
    return 0;
}```
spice ivyBOT
#

@summer rapids Has your question been resolved? If so, run !solved :)

spice ivyBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.