简体   繁体   中英

Printing array of doubles gives unexpected results

In a program I am writing I am experiencing unexpected output when printing data from an array. I have tried with float and double. Here is the code:

#include <iostream>

int main()
{
    double vector[3]{ 193.09375 , 338.5411682 , -4.0 };
    double pVecX{ 193.09375 };
    double pVecY{ 338.5411682 };
    double pVecZ{ -4 };
    
    std::cout << std::dec << vector[1] << '\n' << vector[2] << '\n' << vector[3] << '\n' << '\n';
   
    std::cout << std::dec << pVecX << '\n' << pVecY << '\n' << pVecZ << '\n';

    system("Pause");
    return 0;
}

This is the output:

338.541
-4
1.42292e-306

193.094
338.541
-4
Press any key to continue . . .

Issues: I expected the vectors to print in reverse order from how they were entered into the array. (Even though I ask for [1]..[2]..[3], it is printing [2]..[3]..[1] (I Think that is the order))

When part of the array, the number "193.09375" becomes a (seemingly) random notated number, and is different every time the program runs.

I was reading about variables and understand that a variable stored outside of the range it is initialized as can cause wrap-around, I just do not know why that is happening here. (I assume it is based on the negative notation.)

I am certain that I am missing something simple, and I am fairly new.

An Array s index starts at 0 . So when you say vector[3] you are actually going out of bounds.

You only have 0, 1, and 2 indices or subscripts. Although you do have 3 elements. 0 would refer to your first element, 1 would refer to your second element, and 2 would refer to your 3 element, and so on and so forth.

(Like I mentioned in my comment.)

You should have something like this instead:

std::cout << std::dec << vector[0] << '\n' << vector[1] << '\n' << vector[2] << '\n' << '\n';

This should fix your problem. Also consider using a std::vector .

Also read about why you should not use system("Pause"); .

As mentioned in other answers, the valid indexes for an array of size 3 is 0, 1, and 2. Using any other index invokes undefined behavior.

You can also avoid explicitly indexing into the array, if you use a loop:

for (auto v : vector)
  std::cout << std::dec << v << '\n';

vector[3] is outside the bounds of the array. Behaviour of the program is undefined. Valid indices are 0, 1 and 2.

I expected the vectors to print in reverse order from how they were entered into the array.

Why? The indexes you print are ordered from lower to higher (and the << print in the order of the source code, etc.). If you wanted to reverse them, you need to print first the highest index, 2 , then 1 , then 0 ,

When part of the array, the number "193.09375" becomes a (seemingly) random notated number, and is different every time the program runs.

The vector array goes from 0 to 2 , not from 1 to 3 . When you try to access vector[3] , it is undefined behavior and the program will likely printing whatever memory ends up there. Every time you run the program that memory may contain different things, it is a fairly normal result of undefined behavior.

Arrays in C++ are zero indexed, that means that the first element is accessed by the index 0, eg

int array[3] {5,6,7};

So array[0] == 5, array[1] == 6, array[2] == 7.

The reason you get a random number is that you are trying to print an element of the array which never got defined. In the given example of my array above, if I would try to print element array[3], the corresponds to a certain place in memory (which is not part of my array) which can be filled by any value, thats called undefined bahavior).

If you want to print out every element of an array, you could make use of range based for loops:

for (auto a : my_array) std::cout << a << std::endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM