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
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
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 !!
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.