|
|
back to boardwrong answer (C++) 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++) There will be more than 1000 numbers. double stack[1000]; - Your error is here Use dynamic storage like stl vector Re: wrong answer (C++) #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++) 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 |
|
|