7 Replies - 437 Views - Last Post: 20 September 2010 - 05:07 AM Rate Topic: -----

#1 tejasd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 14-April 09

Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 03:40 AM

Hi I was solving a problem : http://uva.onlinejud.../100/10038.html

I have written this code :
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int inp;
char list [3005];



int jumpers [3005];

string input;
int total_numbers;

void clear_first_number() //for use with sstrem
{
    int initial = 0;
    int final = 0;
    char buffer;
    for (int counter = 0; counter == counter ;++counter)
    {
        buffer = input[counter];
        if (buffer == ' ')
        {
            final = counter;
            ++ final;
            break;
        }
    }
    input.erase(0, final);
}

int subtract (int one, int two)
{
    int difference = one - two;
    if (difference < 0)
    {
        difference = -1*difference;
    }
    return difference;
}



int main()
{

    for (;;)/> // main loop till no input
    {
        /*stringstream(input) >> total_numbers;
        if (input.length() == 0)
        {
            return 0;
        }
        stringstream(input) >> total_numbers;*/

        int is_jolly = 1;
        input.clear();
        getline (cin, input);
        if (input.length() == 0)
        {
            break;
        }
        stringstream(input) >> total_numbers;
        clear_first_number();
        for (int i = 0 ; i < total_numbers; ++i)//process input string
        {
            stringstream(input) >> inp;
            jumpers[i] = inp;
            clear_first_number();

        }

        /*for (int i = 0; i < total_numbers; ++i)
        {
            cin >> jumpers[i];
        }*/
        //set bool array all to false
        for (int e = 0; e < total_numbers; ++e)
        {
            list [e] = '0';
        }
        //create subtractions
        for (int j = 0; j < total_numbers ; ++j)
        {
            int difference = subtract(jumpers[j], jumpers[j+1]);
            list[difference-1] = '1';
        }






        /*//make those indexes true which are present
        for (int t = 0; t < total_numbers ; ++t)
        {
            list[jumpers[t]-1] = true;
        }*/
        //check if jolly
        for (int d = 0; d < total_numbers-1; ++d)
        {
            if (list[d] == '0')
            {
                is_jolly = 0;
                break;
            }
        }
        //print statements
        if (is_jolly == 1)
        {
            cout << "Jolly\n";
        }
        else
        {
            cout << "Not jolly\n";
        }

    }
    return 0;
}



But the judge is not accepting the program and is saying runtime error.
Could you please help me out and tell me where I am going wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Online Judge gives runtime error. Can I know why?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 03:59 AM

What is your input?
Was This Post Helpful? 0
  • +
  • -

#3 tejasd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 14-April 09

Re: Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 04:02 AM

I only have the sample input given on the link. Other input is secret.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 04:21 AM

Your bounds testing in clear_input_number is faulty resulting in an infinite loop. Step through your code in a debugger.
Was This Post Helpful? 0
  • +
  • -

#5 tejasd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 14-April 09

Re: Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 04:28 AM

Ok Will check it :)
But for decent sized inputs running on windows I dont seem to find any infinite loops. I get correct answers. Will it result in infinite loop on large inputs?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 04:35 AM

It results in an infinite loop for me on the first sample input provided on that page. It's all in how the input is received.
Was This Post Helpful? 0
  • +
  • -

#7 tejasd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 14-April 09

Re: Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 04:48 AM

I'm sorry, but could you correct it and give it to me. I checked this code on two different computers (one running win 7 x64 and the other x86) both using code blocks, i was able to execute properly.

Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Online Judge gives runtime error. Can I know why?

Posted 20 September 2010 - 05:07 AM

If I give "4 1 4 2 3" as input, your loop is infinite. Easy as that, go debug it.

No, I will not fix your code and "give it to you". But I will close this topic just because you had the temerity to demand it of me/us.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1