0
double a [3]={1.1,2.2,3.3};
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
a[1]=a[2];  
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;

ans to the above is
1.1 2.2 3.3
1.1 3.3 3.3

I am new to array. Please expalin why a[1]=a[2] gave me 3.3 3.3.
thank you

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by jaskij
0

Thats because u put the value of a[2] into a[1] thats why. Luk at ur 3rd statement...
If u put a[1] = a[0] and then print a[0] and a[1], ur output will be 1.1 1.1 !! I think u shud read up on arrays !!

0

First, a lil bit of history and theory: since C++ is based on C, and C is a pretty low-level language, many things are closer to how computer does them, then they are to human's way.

Arrays (or container) indexing is one such case.
They are indexed beginning at 0 (so you used them properly in the cout statements), so a statement like yours a[1]=a[2]; actually assigns the second element the same value as the third one.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.