Got runtime error (SIGABRT) in a SPOJ classical problem

Ques link: To and Fro

My code works for the given test cases and on my own test cases,but shows runtime error on submission.Please help.

My code:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
int n;
cin >> n;
while (n != 0)
{
    string s;
    cin >> s;
    char a[10][20];
    int cols = n, rows = s.length() / n, k = -1;
    for (int i = 0; i < rows; i++)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < cols; j++)
            {
                k++;
                a[i][j] = s[k];
            }
        }
        else
        {
            for (int j = cols - 1; j >= 0; j--)
            {
                k++;
                a[i][j] = s[k];
            }
        }
    }
    for (int j = 0; j < cols; j++)
    {
        for (int i = 0; i < rows; i++)
        {
            cout << a[i][j];
        }
    }
    cout << endl;
    cin >> n;
}
return 0;
}

why fix size of 10 X 20 ?

1 Like

Because it was given that max length of i/p is 200 and max no of columns is 20.hence max no of rows will be 200/20=10.

Please correct me if I am wrong.

constant/variable is max when variable is min

when no of col is 1, what is max possible no of row

1 Like

Well…Thanks!!..I just changed the no of max rows as 200 and it got accepted!
And all the while I thought that my logic was wrong somewhere!..Thanks! :grin: :+1:t2: