Change of basic array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio>

int main()
{
	char a[16] = "programming c++";
   int i;
   for(i=0;i<=9;i=i+1)
   {
   	a[i]=a[9-i];
      cout<<"i= "<<i<<a<<endl;
	}

   cout<<a;
   getch();
   return 0;
}


Hi, can I know why the above program do not change any value at the ouptut shown below after i=5? Thanks...

i= 0nrogramming c++
i= 1niogramming c++
i= 2nimgramming c++
i= 3nimmramming c++
i= 4nimmaamming c++
i= 5nimmaamming c++
i= 6nimmaamming c++
i= 7nimmaamming c++
i= 8nimmaamming c++
i= 9nimmaamming c++
nimmaamming c++


Last edited on
because you already changes those values. you read from 9 to 0 but write from 0 to 9.
when you reach the middle it start reading data wich you just wrote to it.
At i=5, it sets a[5], which is a, to the same as a[4], which is... a.
At i=6, it sets a[6], which is m, to the same as a[3], which is... m.

Spot the pattern? After i=5, each time you change a[i], you're changing it to the exact same letter is already is.
Ok...I understand now...thanks everyone...^^
Topic archived. No new replies allowed.