Reverse Root Timus

I'm solving a problem that I've almost completed but I'm stuck with two errors where my knowledge of C++ is limiting me from debugging the code.

So far I have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <math.h>

int main(){
	
	unsigned long long inputstream;
	unsigned long long array[4] = {}; //I used magic numbers to debug
	int arrayPlace = 0;

	while (std::cin >> inputstream ){
			array[arrayPlace] = sqrt(inputstream);
			arrayPlace++;
	}
	std::cout << std::fixed;
	std::cout << std::setprecision(4); 
	for ( size_t i = arrayPlace - 1; i >= 0; i--)////<---Loop goes into random arrays

		std::cout << array[i] << " ";

}



The main problem I have is that
a) When I register the sqrt product into the array for some reason it truncates my number into an integer
b) My for loops goes past the boundaries despite the fact that my condition stops at i >= 0

Problem 1001:

Input
The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.
Output
For each number Ai from the last one till the first one you should output its square root. Each square root should be printed in a separate line with at least four digits after decimal point.
Last edited on
Also on a side note I was wondering if using std::vector would slow the program down or is it just as fast?
1) Your array is an array of integers. So it is natural that any floating point value will ne truncated to integer.
2) size_t is unsigned type. It cannot represent values below 0. When you try to decrement 0, you will get largest possible value.

3) No it would not. Unless you profile your program and fing vector operations being the bottleneck.
Alright thanks! I've solved the problem of accessing random memory in the array.

Going to the point about my array of integers. By declaring my array unsigned long long wouldn't that be enough to make it an array of unsigned long long?

Or am I declaring the array wrong?
By declaring my array unsigned long long wouldn't that be enough to make it an array of unsigned long long?
It would. But unsigned long long is an integer type. It can store only whole numbers. You need to declare it as double to store fractions.
Thank you very much!

I need to look over my data declarations.
Topic archived. No new replies allowed.