-3

I'm writing a code for this problem https://codeforces.com/contest/118/problem/B And that's my solution https://codeforces.com/contest/118/submission/60674349 As you see I have a runtime error.

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
    int n, m;
    do
    {
        cin >> n;
    } while (n < 2 || n > 9);
    m = (n * 2) + 1;
    string shape[m];
    for (int i = 0, k, a; i < m; i++)
    {
        if (i == 0 || i == m - 1)
        {
            k = 1;
            a = m - 1;
        }
        else if (i <= n)
        {
            k = (2 * i) + 1;
            a -= 2;
        }
        else
        {
            k -= 2;
            a += 2;
        }

        for (int y = 0; y < a; y++)
        {
            cout << " ";
        }

        for (int j = 0; j < k; j++)
        {
            if (j == 0 || j == k - 1)
            {
                shape[i][j] = '0';
            }
            else if (j <= (k / 2))
            {
                shape[i][j] = shape[i][j - 1] + 1;
            }
            else
            {
                shape[i][j] = shape[i][j - 1] - 1;
            }
            cout << shape[i][j];
            if (j != (k - 1))
            {
                cout << " ";
            }
        }

        cout << endl;
    }
    return 0;
}

I expect it to give the output required and it does! but I still get the runtime error, and..I googled for the problem and didn't know under which topic I need to search.

berky ross
  • 13
  • 2
  • 3
    That's not really valid C++, as C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. – Some programmer dude Sep 17 '19 at 08:12
  • And no we don't "see" any runtime error. Please use a debugger to catch the crash when it happens, and see where in your code it happens, and examine the values of all involved variables. My bet is that you go out of bounds of your array (for example by using any index in an empty string). – Some programmer dude Sep 17 '19 at 08:13
  • Also, if `shape[i][j - 1]` is the character `'0'`, what would then `'0' - 1` do (as would happen with `shape[i][j - 1] - 1`)? – Some programmer dude Sep 17 '19 at 08:16
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Thomas Sablik Sep 17 '19 at 08:19
  • You don't need to build the result. It's easier to write a couple of loops that print. (Start with a program that prints a rhombus of asterisks or something, to get the shape right.) – molbdnilo Sep 17 '19 at 08:19
  • The 1 vla error, and more than a dozen compiler warnings ... consider enabling at least a few compiler options: -Werror=vla -Wconversion -Wsign-compare With practice, these are all easy to fix. – 2785528 Sep 17 '19 at 11:41
  • @2785528 I'm new to programming, so can you explain more about what you are saying? – berky ross Sep 24 '19 at 04:42
  • I use a Makefile that generates the following compile command. Search the following line for examples of "-W"'s which enable warnings. I suggest you try them all .... g++ -std=c++17 -m64 -ggdb -O3 -Wall -Wextra -Wshadow -pedantic -Werror=vla -Wcast-align -Wcast-qual -Wconversion -Wsign-conversion -Wsign-compare -Wsign-promo -Wpointer-arith -Wunused -Wold-style-cast -Woverloaded-virtual -Wsequence-point -Wdelete-incomplete -Wmaybe-uninitialized -Wmisleading-indentation -Wunreachable-code -Wnon-virtual-dtor -MMD -MP -O0 dumy826.cc -o dumy826 -L../../bag/src -lbag_i686 -lrt -pthread – 2785528 Sep 24 '19 at 21:50
  • Just so you know, -Wall is just another poorly named option (the last time I looked into it). I like all of these ... but your first try at these might discourage you. They are worth tolerating at first, so that you learn how to fix, and then learn how to avoid. I repeat, these fixes are EASY and worth while. If some warning seems impossible, relax, ignore the warning for a day or so, then try again. – 2785528 Sep 30 '19 at 22:31

2 Answers2

1

This here

string shape[m];

Makes an array of empty strings (if the compiler tolerates the VLA, anyway). Those strings aren't initialized with a size. Later, when you do this

shape[i][j] = '0';

You set a value at a position in the string that is out of bounds. Instead, you need a data structure that is initialized to hold your 2D data that you're writing to it. You could change the string shape[m]; line to this, for instance:

std::vector<std::vector<char>> shape(m,std::vector<char>(m));

If instead you wanted to keep it as an array of std::string, you could set the lengths of the strings like this:

for (string &s : shape)
    s.resize(m);

If you do it like this, I would recommend at least changing the definition to std::vector<std::string> shape(m); so it's not a VLA anymore and thus more portable.

Blaze
  • 16,736
  • 2
  • 25
  • 44
0

Also notice this line and that i∈Ν

k = (2 * i) + 1;

k is always an odd number. Then you got this line

 else if ( j <= (k / 2)) {

You are dividing an odd integer by two. Make sure that the floor rounding done by compiler is what you expect in such a case.