ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

ошибка: Runtime error (access violation), подскажите как исправить? С++
Posted by Krychun Ivan 14 Mar 2016 16:28
#include <stdio.h>
#include <tchar.h>
#include <math.h>
int charToNumber(char charValue)
{
    if (charValue >= '0' && charValue <= '9') {
        return charValue - '0';
    }
}
int main()
{
    int CountOfAllNumbers = 1;
    int CountOfNumbers = 0;
    bool flag = 0;
    double *numbers = new double[4];
    numbers[CountOfAllNumbers] = 0;
    char *A = new char [100];
    scanf("%s", &A);
    for (int i = strlen(A)-1; i >= 0; i--)
    {
        if (((A[i] == ' ') || (A[i] == '\t'))|| (A[i] == '\n'))
        {
            CountOfNumbers = 0;
            if (flag)
            {
                CountOfAllNumbers++;
                numbers[CountOfAllNumbers] = 0;
                flag = 0;
            }
        }
        else
        {
            flag = 1;
            numbers[CountOfAllNumbers] = numbers[CountOfAllNumbers] + charToNumber(A[i])*pow(10.0, CountOfNumbers);
            CountOfNumbers++;
        }
    }
    for (int i = 1; i < CountOfAllNumbers; i++)
    {
        printf("%lf\n", sqrt(numbers[i]));
    }
    delete[] numbers;
    return 0;
}

Edited by author 14.03.2016 16:28
Re: ошибка: Runtime error (access violation), подскажите как исправить? С++
Posted by ToadMonster 14 Mar 2016 21:11
You are trying to read WHOLE input in one read, into 100 bytes buffer.
But input is up to 256Kb; scanf "%s" reads non-white-space data and stops on the first white-space.

You expect not more then 4 numbers. There are up to 256K/2 numbers.

You should better use scanf "%lld" / scanf "%lf" in the cycle. You should allocate big enough "numbers" array (or use std::vector).

You also should run locally at least test in the task sample and receive EXACTLY the sample output.