c++ - find middle elements from an array -
in c++ how can find middle 'n' elements of array? example if n=3, , array [0,1,5,7,7,8,10,14,20], middle [7,7,8].
p.s. in context, n , elements of array odd numbers, can find middle. thanks!
this quick, not tested basic idea...
const int n = 5; // middle index int arrlength = sizeof(myarray) / sizeof(int); int middleindex = (arrlength - 1) / 2; // sides int side = (n - 1) / 2; int count = 0; int mynewarray[n]; for(int = middleindex - side; <= middleindex + side; i++){ mynewarray[count++] = myarray[i]; }
Comments
Post a Comment