i have been trying to insert a new element into a given array at a given index. though i am able to insert new element but i have few doubts..
- why it got inserted at 7th index instead at 3?
- how value of variable k become 7?
- why size of old and new array is same though new array has 1 more element?
thanks in advance. any help would be appreciated.
int main()
{
int arr[] = {1,2,3,4,5,6,7};
int n = sizeof(arr)/sizeof(arr[0]);
int k = 3;
cout << "elements of old array" << endl;
for (int i=0; i<n; i++){
cout << arr[i] << endl;
}
cout << "size of old array " << sizeof(arr) << endl << endl << endl;
// inserting new element
for (int j=n; j>k; j--){
arr[j] = arr[j-1];
}
arr[k] = 10;
cout << "elements of new array " << endl;
for (int i=0; i<=n; i++){
cout << arr[i] << endl;
}
cout << "size of new array " << sizeof(arr)<< endl ;
}