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

wrong answer (C++)
Posted by lhyx1990 11 Jan 2014 12:28
Please help me


#include <iostream>
#include <math.h>


using namespace std;

int main(int argc, const char * argv[])
{

    double stack[1000];

    int index = 0;
    while (cin >> stack[index]) {
        index++;
    }


    index--;
    for (; index > 0; index--) {
        printf("%.4lf\n", sqrt(stack[index]));
    }


}
Re: wrong answer (C++)
Posted by Alexander Paltsev 15 Jan 2014 03:24
There will be more than 1000 numbers.

double stack[1000]; - Your error is here

Use dynamic storage like stl vector
Re: wrong answer (C++)
Posted by lhyx1990 15 Jan 2014 11:58
#include <stdio.h>
#include <math.h>
#include <vector>


using namespace std;

int main(int argc, const char * argv[])
{

    vector <double> stack;

    int index = 0;

    while (scanf("%lf", &stack[index++]) != EOF) {

    }

    for (index--; index > 0; index--) {
        printf("%.4lf\n", sqrt(stack[index]));
    }

}
still wrong answer, access violation. please help me
Re: wrong answer (C++)
Posted by morbidel 15 Jan 2014 17:08
You cannot access stack[index] since its size is 0. Use an intermediary variable for the read number, and then use stack.push_back(x). Use stack.size() to retrieve the number of elements in the stack.
Or you can use a <list> instead of <vector>, and use push_front, so that in the end, you don't need to loop in reverse order.
Take a look on these STL containers, to get familiarized with them:
http://www.cplusplus.com/reference/vector/vector/push_back/
http://www.cplusplus.com/reference/list/list/push_front/

Edited by author 15.01.2014 17:11

Edited by author 15.01.2014 17:11