If the expectation is to just print the elements of array in the given order:
Keep the track of current index of array element to be print, say indx -
- If the position of current element processing is equal to row number then
print the element at
givenIndex.
- If
indx is equal to givenIndex skip it and print indx + 1 element, otherwise print element at indx and increase indx by 1.
Implementation:
#include <iostream>
#include <array>
int main() {
std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
std::size_t givenIndex = 6;
for (std::size_t i = 0, indx = 0; i < array.size(); indx = 0, ++i) {
std::cout << '[';
for (std::size_t j = 0; j < array.size(); ++j) {
if (j == i) {
std::cout << array[givenIndex] << ',';
continue;
}
if (indx == givenIndex) {
++indx;
}
std::cout << array[indx++] << ',';
}
std::cout << ']';
std::cout << '\n';
}
return 0;
}
Output:
# ./a.out
[7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,]
If the expectation is to alter the order of elements in the array and then print the array:
First move the element at givenIndex to the 0th index of array and then -
- Print array
- In every iteration swap the current element with its next element in the array and print it.
Implementation:
#include <iostream>
#include <array>
void print_array (std::array<int, 16>& array) {
std::cout << '[';
for (std::size_t indx = 0; indx < array.size(); ++indx) {
std::cout << array[indx] << ',';
}
std::cout << ']';
std::cout << '\n';
}
void rearrange_array_elem (std::array<int, 16>& array, std::size_t givenIndx) {
// move the element at givneIndx to first position in array
for (std::size_t j = givenIndx; j > 0; --j) {
std::swap (array[j], array[j - 1]);
}
// print array
print_array (array);
for (std::size_t indx = 0; indx < array.size() - 1; ++indx) {
// swap current element with its next element
std::swap (array[indx], array[indx + 1]);
print_array (array);
}
}
int main() {
std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
std::size_t givenIndex = 6;
rearrange_array_elem (array, givenIndex);
return 0;
}
Output:
# ./a.out
[7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,]