I can not figure out an algorithm for this task, maybe you have an idea?
Task: Move the array to the array in the first half would be all the elements of the odd lines and the second all elements of the pair of lines.
The simplest option would be to move elements to another array, as it could make one using temp variable?
Input data array = {1,2,3,4,5,6,7,8}
output data array = {2,4,6,8,1,3,5,7}
Is this homework? If not, use std::stable_partition():
struct IsEven {
template<class T>
bool operator()(const T& v) const { return v % 2 == 0; }
};
int* arr = {1,2,3,4,5,6,7,8};
int* mid = std::stable_partition(arr, arr+8, IsEven());
If it is a homework, then your instructor probably expects you to write the algorithm. If you don't have to maintain the ordering as in the input sequence then you can do it rather efficiently:
Find the first element that doesn't satisfy the predicate (i.e., is odd).
Find that last element that does satisfy the predicate (i.e., is even)
swap the two elements.
Repeat, starting from the positions you just found, until the two positions meet.
The point where the two positions meet is the middle of the partition, where even numbers stop and odd numbers begin.
This is roughly how std::partition() works. If you do have to maintain the relative ordering in the input array then you can still do it in-place, but it will be faster if you use a temporary buffer. Copy the elements that don't match the predicate into that buffer, and squeeze in place those that do. Finally bring back in the elements that don't match, at the end of the array, in order.
I'm reading this question such that the output array should have the elements at even indices followed by those at odd indices, though I may be incorrect.
If not, then this is how I would do it.
template<typename T>
void moveValues(const T[] input, T[] output, std::size_t length)
{
int current outputIndex = 0;
if (length > 1) // in case we have a single element array.
{
for (int i = 1; i < length; i+=2)
{
output[++outputIndex] = input[i];
}
}
for (int j = 0; j < length; j+=2)
{
output[++outputIndex] = input[j];
}
assert(outputIndex == length); // should have filled up array
}
/* *********************************************************************** */
/* Author: Bigyan Shrestha */
/* Description: C++ source code for arranging even numbers */
/* numbers of an array to one half and odd */
/* numbers to the other half. */
/* *********************************************************************** */
#include <iostream>
using namespace std;
inline bool isEven( int value ){
if( value % 2 == 0 ) {
return true;
}
else {
return false;
}
}
inline void swap( int sourceIndex, int destIndex, int **sourceArray ) {
int temp;
temp = (*sourceArray)[sourceIndex];
(*sourceArray)[sourceIndex] = (*sourceArray)[destIndex];
(*sourceArray)[destIndex] = temp;
}
void displayArray( int *sourceArray, int size ){
for( int i = 0; i < size ; i++ ) {
cout << sourceArray[i] << " " ;
}
cout << endl;
}
int main( void ){
int size;
int *input;
int evenIndex = 0; // for keeping track of even numbers
cout << "Enter the size of input array" << endl ;
cin >> size;
input = new int[size];
for( int i = 0; i < size ; i++ ) {
cout << "Please enter the input value ( " << size-i << " remaining )" << endl;
cin >> input[i] ;
}
cout << endl;
cout << "Original Input Array " << endl;
displayArray( input,size );
cout << endl;
for( int i = 0; i <size ; i++ ) {
if( isEven(input[i]) && i > evenIndex ) {
for( int j = i ; j > evenIndex; j-- ){
swap( j, j-1, &input);
}
++evenIndex;
}
}
cout << "Modified Array " << endl;
displayArray( input,size );
cout << endl;
return 0;
}
Related
Here's a c++ code to sort the data of various types using template function. Can someone elaborate what does BOOL do here?
I've read about bool data type. It basically assumes only two values TRUE and FAlSE. But I've no idea, what is the role of BOOL here ?
using namespace std;
#include <iostream>
template <class T>
void sort (T array[], int size)
{ int j;
T temp;
int pass;
static int call = 0;
bool xchange = true;
for (pass = 1; pass < size && xchange == true; pass++)
{ xchange = false;
for (j=0; j < size - pass; j++)
{ if (array[j] > array[j+1])
{ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp;
xchange = true;
} /* end of if */
} /* end of inner for loop */
};
call ++;
cout << " within sort : value of call is " << call << endl;
/* end of outer for loop */
} /* end of bubble sort */
int main() {
int a[] = {1,5,6,4,0,8,5,7,9,2};
int a1[] = {1,5,6,4,0,8,5,7,9,2};
char b[] = { 'a', 'c', 'f', 'd', 'b' };
sort (a, 10);
for ( int i=0; i < 10; i++ ) cout << a[i] << " ";
cout << endl;
sort (b, 5);
for ( int i=0; i < 5; i++ ) cout << b[i] << " ";
cout << endl;
sort (a1, 10);
for ( int i=0; i < 10; i++ ) cout << a1[i] << " ";
cout << endl;
return 0;
}
The bool in the example is there to check wether any exchanges happened in that iteration of the for-loop. If no exchanges happened == the array is sorted, the sorting algorithm is done.
Giving this function a already sorted array will show the purpose of the boolean. In the first iteration through the whole array, no exchanges will take place (because the array is already sorted) -> the xchange was never set to true -> the sorting algorithm is done == the condition in the for-loop condition is false.
It is there to not do work that you dont have to do.
Hi i'm the beginner of c++.
This time, I try to reverse Array 's element-order using dynamic Allocation Operator.
For example an array{1,2,3,4} will be rearranged {4,3,2,1} through calling function 'reverseArray'.
Everything works fine but somehow i got unwanted integer '-1' along with rearranged Array.
For example {-1,4,3,2,1}.
It means i did wrong and i really want to learn my fault.
Here is my code, please help me to figure out.
#include<iostream>
#include<new>
using namespace std;
void reverseArray(int [] , int);
int main(){
int num=0;
cout << "enter size of pointer : " ;
cin >> num ;
int *pointerArray = new int [num];
cout << "enter integer numbers in pointer" << endl;
for(int index = 0 ; index < num ; index++){
cin >> pointerArray[index];
}
reverseArray(pointerArray, num);
delete[] pointerArray;
return 0 ;
}
void reverseArray(int reverse[], int Size){
int*ptArray[Size];
cout << "the reverse order of entered numbers is : " << endl;
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
return ;
}
This function
void reverseArray(int reverse[], int Size){
int*ptArray[Size];
cout << "the reverse order of entered numbers is : " << endl;
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
return ;
}
does not make sense.
For starters variable length arrays
int*ptArray[Size];
is not a standard C++ feature.
Secondly in this loop
for(int index = 0 ; Size >= 0 ; index++) {
ptArray[index] = &reverse[Size];
cout << *ptArray[index] << " " ;
--Size;
}
there is access beyond the arrays.
For example let's assume that Size is equal to 1.
In this case within the ,loop we have
ptArray[0] = &reverse[1];
and then
ptArray[1] = &reverse[0];
However the only valid index for such arrays is 0.
It is unclear what you are trying to do.
If you want to reverse an array in place then the function can look like
void reverseArray( int a[], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
// you can use the standard function std::swap here
int tmp = a[i];
a[i] = a[Size - i - 1];
a[Size - i - 1] = tmp;
}
}
And in main after calling the function you can output the reversed array .
Pay attention to that there is standard algorithm std::reverse that can do the rask.
You could just write
std::reverse( reverse, reverse + num );
Here is a demonstrative program.
#include <iostream>
#include <algorithm>
void reverseArray( int a[], size_t Size )
{
for ( size_t i = 0; i < Size / 2; i++ )
{
// you can use the standard function std::swap here
int tmp = a[i];
a[i] = a[Size - i - 1];
a[Size - i - 1] = tmp;
}
}
int main()
{
const size_t N = 5;
int *a = new int[5] { 1, 2, 3, 4, 5 };
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
reverseArray( a, N );
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
std::reverse( a, a + N );
for ( size_t i = 0; i < N; i++ ) std::cout << a[i] << ' ';
std::cout << '\n';
delete [] a;
return 0;
}
Its output is
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
In your code you access the array out-of-bounds on the very first iteration, because for an array of size Size valid indices are 0 up to Size-1
It is not clear why you create an array of pointers, and int*ptArray[Size]; is a variable lenght array (VLA) which is not standard C++. Further, you do not need to include <new>.
To print a dynamically sized (ie size is taken from input) array you would use a std::vector and a loop:
#include <vector>
#include <iostream>
int main(){
size_t size;
std::cout << "enter size: ";
std::cin >> size;
std::vector<int> data(size);
for (auto& element : data) std::cin >> element;
for (size_t i = 0; i < data.size(); ++i) {
std::cout << data[ data.size()-1-i ]; // first element printed is data[data.size()-1]
}
}
If you want to reverse the array, not just print it in reverse order, there is std::reverse in <algorithm>. The algorithm also works with dynamically allocated arrays:
#include <iostream>
#include <algorithm>
int main(){
size_t size;
std::cout << "enter size: ";
std::cin >> size;
int* data = new int[size];
for (size_t i=0; i<size; ++i) std::cin >> data[i];
std::reverse(data,data+size);
for (size_t i=0; i<size; ++i) std::cout << data[i];
delete [] data;
}
#include
using namespace std;
int main() {
int n;
cin>>n;
int temp=n;
int rem=0;
int i=0;
while (n>0) {
n= n/10;
i++;
}
int *arr = new int(i);
i=0;
while (temp>0) {
rem=temp%10;
arr[i]=rem;
i++;
temp= temp/10;
}
int t=0;
while(t<i) {
cout<<arr[t]<<" ";
t++;
}
return 0;
}
I have big problem.
The task is:
Write a function in C ++ that takes a array of ints tab, array size n, and the number k. The function returns returns true if each of the numbers in the table tab, at least k digits long, and false otherwise. Checking how many digits has the number should be included in the additional auxiliary functions that call from inside a basic function. You should also write the main function that reads the data, calls the base and outputs its result.
For calls (record [] is an array):
f ([123,4425,2224,222,55553], 5, 3)
The function should return true. Since each of the numbers 123,4425,2224,222,55553 at least three digital
Calls for:
f ([123,4425,2,222,5], 5, 2)
The function should return false
Because there are a number, for example 2 which is a digital one and is less than 1 k = 2
My code :
#include <iostream>
int ile_cyfr(int a)
{
int temp=0;
do
{
a = a/10;
temp++;
} while(a>0);
return temp;
}
bool funkcja(int *tab, int n, int k)
{
bool stan = false;
for (int i=0; i<n; i++)
{
if (ile_cyfr(tab[i])<k)
{
stan = false;
if (stan == false)
{
return stan;
return 0;
}
}
else
{
stan = true;
return stan;
}
}
}
int main() {
using namespace std;
int n=0, k=0;
int *tab = new int[n];
cout << "Podaj ilosc liczb: " << endl;
cin >> n;
cout << "\nPodaj liczby: " << endl;
for (int i=0; i<n; i++) {
cin >> tab[i];
}
cout << "\nPodaj minimalna ilosc liczb: " << endl;
cin >> k;
cout << funkcja([444,856,671,321], n, k);
return 0;
}
The problem is that the line:
cout << funkcja([444,856,671,321], n, k);
For starters the function funkcja is invalid. It returns true in case when the first element of the array has the number of digits greater than or equal to k. As I have understood you have to check that all elements of the array satisfy the condition.
The function can be written the following way
bool funkcja( const int *tab, int n, int k )
{
int i = 0;
while ( i < n && !( ile_cyfr( tab[i] ) < k ) ) i++;
return n != 0 && i = n;
}
As for this statement
cout << funkcja([444,856,671,321], n, k);
then it has an incorrect syntax.
You have to pass the variable tab as the first argument of the function call. These values 444,856,671,321 should be assigned to elements of the array pointed to by the pointer tab.
Thus write
cout << funkcja( tab, n, k );
I need some help, I know this question was asked before but I don't get it and I cant solve it, so I need help. I need to move the elements of my array to a position to left. So if the input will be 1,2,3,4,5 then the output will be 2,3,4,5,1. I have done the same to right but to left I cant figure it out, please also explain the logic , thanks.
#include <iostream>
using namespace std;
int a[100],n,i,tempr,templ;
int main()
{
cin>>n;
for(i=1;i<=n;i++) cin >> a[i];
for(i=1;i<=n;i++)
{
tempr = a[n];
a[n] = a[i];
a[i] = tempr;
cout<<"Right: "<<a[i]<<endl;
}
for(i=1;i<=n;i++)
{
templ = a[2];
a[2] = a[i];
a[i] = templ;
cout<<"Left: "<<a[i]<<endl;
}
return 0;
}
Please help!
First problem is bad indexing:
for(i=1;i<=n;i++) cin >> a[i]; //wrong logic, C++ indexing start from 0
Correct approach:
for(i=0;i<n;i++) //all your loops
Second problem is wrong logic for shifting elements:
Corrected version:
//input example: 1 2 3 4 5
//to the left
int temp = a[0]; //remember first element
for(i=0;i<n-1;i++)
{
a[i] = a[i+1]; //move all element to the left except first one
}
a[n-1] = temp; //assign remembered value to last element
//output: 2 3 4 5 1
cout << "To left: " << endl;
for(i=0;i<n;i++)
cout << a[i] << endl;
//to the right
temp = a[n-1]; //remember last element
for(i=n-1;i>=0;i--)
{
a[i+1] = a[i]; //move all element to the right except last one
}
a[0] = temp; //assign remembered value to first element
//output: 1 2 3 4 5 because elements are shifted back by right shift
cout << "To right: " << endl;
for(i=0;i<n;i++)
cout << a[i] << endl;
EDIT:
How to display both shifts:
#include <iostream>
using namespace std;
int to_left[5], to_right[5],n,i,tempr,templ;
int main()
{
cout << "Input array size: ";
cin >> n;
for(i=0;i<n;i++)
{
cin >> to_left[i]; //read values to first array
to_right[i]=to_left[i]; //then copy values to second one
}
//shift first array to left
int temp = to_left[0];
for(i=0;i<n-1;i++)
{
to_left[i] = to_left[i+1]; //move all element to the left except first one
}
to_left[n-1] = temp; //assign remembered value to last element
//output: 2 3 4 5 1
cout << "To left: " << endl;
for(i=0;i<n;i++)
cout << to_left[i] << endl;
//shift second array to right
temp = to_right[n-1]; //remember last element
for(i=n-1;i>=0;i--)
{
to_right[i+1] = to_right[i]; //move all element to the right except last one
}
to_right[0] = temp; //assign remembered value to first element
//output: 1 2 3 4 5 because elements are shifted back by right shift
cout << "To right: " << endl;
for(i=0;i<n;i++)
cout << to_right[i] << endl;
return 0;
}
Note that your code look very much like C code. In C++, you can declare variables in any segment of code, not just at the beginning. In C++, you can declare variable in for loop like this: for(int i=0; i<...) - no need for global variable i
For reference, this would be good C++ code example that satisfies problem you are facing:
#include <iostream>
#include <vector>
int main()
{
std::size_t n; //size_t is unsiged type used for various sizes of containers or types
std::cout << "Input array size: ";
std::cin >> n;
std::vector<int> to_left(n), to_right(n); //two dynamic arrays containing integers, takin n as their size
for(std::size_t i=0;i<to_left.size();++i) //use vector size(), instead of n, also ++i in considered better for loops that i++ (may be faster)
{
std::cin >> to_left[i];
to_right[i]=to_left[i];
}
int temp = to_left[0]; //declare temp here, not at the begining of code
for(std::size_t i=0;i<n-1;++i)
to_left[i] = to_left[i+1];
to_left[n-1] = temp;
std::cout << "To left: " << std::endl;
for(std::size_t i=0;i<n;++i)
std::cout << to_left[i] << std::endl;
temp = to_right[n-1]; //reuse temp
for(int i=to_right.size()-1;i>=0;--i) //note int, not std::size_t, because size_t is always >=0, loop would never end.
to_right[i+1] = to_right[i];
to_right[0] = temp;
std::cout << "To right: " << std::endl;
for(std::size_t i=0;i<n;i++)
std::cout << to_right[i] << std::endl;
return 0;
}
And here would be ideal C++ code:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::size_t n;
std::cout << "Input array size: ";
std::cin >> n;
std::vector<int> to_left(n), to_right(n);
for(std::size_t i=0;i<to_left.size();++i)
{
std::cin >> to_left[i];
to_right[i]=to_left[i];
}
// rotate first array to the left
std::rotate(to_left.begin(), to_left.begin() + 1, to_left.end());
// rotate second array to right
std::rotate(to_right.rbegin(), to_right.rbegin() + 1, to_right.rend());
std::cout << "To left:" << std::endl;
for(auto x : to_left) //C++11 feature, x iterates through container
std::cout << x << std::endl;
std::cout << "To right:" << std::endl;
for(auto x : to_right)
std::cout << x << std::endl;
return 0;
}
Or you can use memmove(...) projected exactly for those purpose, here your sample:
#include <iostream>
#include <cstring>
using namespace std;
//rotate Left
void r_left(int *a,int n)
{
int tmp=a[0];
memmove(a,a+1,sizeof(int)*(n-1));
a[n-1]=tmp;
}
//rotate right
void r_right(int *a,int n)
{
int tmp=a[n-1];
memmove(a+1,a,sizeof(int)*(n-1));
a[0]=tmp;
}
void show(int *a,int n)
{
while(n--)
cout<<*a++<<' ';
cout<<endl;
}
int main()
{
int ar[]={1,2,3,4,5};
int n=sizeof(ar)/sizeof(ar[0]);
r_left(ar,n);
show(ar,n);
r_right(ar,n);
show(ar,n);
return 0;
}
easiest way to swap elements in C++ is to use std::iter_swap()
so for an array of 4 elements to swap elements 1 and 4 you would do the following
int a[4];
std::iter_swap(a, a+3);
note that you also need to #include <algorithm> for this to work
the basic logic of the function is that you give the location in memory of the 2 elements, so as the first element of an array is also its location in memory, you can pass a + n, when n is equal to the n-1 index number of the element you want to swap
As other already have stated it's all about indices. In a for-loop you are almost always in trouble if your stop condition is i <= size, because arrays in C++ are zero-indexed.
Where Black Moses alogrithm is far the easiest to understand (and probably the fastes), I read your code as if you try to swap the first value of the array through the array to the last position. Below I have tried to pin out this approach.
#include <stdio.h>
#include <tchar.h>
#include <iostream>
void ShiftLeft(int* pArr, size_t length)
{
for (size_t i = 1; i < length; i++)
{
int tmp = pArr[i - 1]; // Preserves the previous value
pArr[i - 1] = pArr[i]; // Overwrites the previous position with the current value
pArr[i] = tmp; // Stores the previous value in the current position
// All in all the first value is swapped down the array until it is at the length - 1 position
// and all the other values are swapped to the left.
/* For an array with 4 values the progression is as follows:
i = 0: 1 2 3 4
i = 1: 2 1 3 4
i = 2: 2 3 1 4
i = 3: 2 3 4 1
*/
}
}
void ShiftRight(int* pArr, size_t length)
{
for (size_t i = length - 1; i > 0; i--)
{
// This code does exactly the same as for ShiftLeft but the loop is running backwards
int tmp = pArr[i - 1];
pArr[i - 1] = pArr[i];
pArr[i] = tmp;
}
}
void Print(int* pArr, size_t length)
{
for (size_t i = 0; i < length; i++)
{
std::cout << pArr[i] << " ";
}
std::cout << std::endl;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
size_t length = sizeof(arr) / sizeof(arr[0]);
Print(arr, length);
ShiftLeft(arr, length);
Print(arr, length);
ShiftRight(arr, length);
Print(arr, length);
return 0;
}
#include <iostream>
using namespace std;
int a[100], outR[100], outL[100], n, i;
int main() {
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
// Right
for (i = 0; i < n; i++) {
outR[i+1]= a[i];
}
outR[0] = a[n-1]; // add first number
// Left
for (i = 1; i < n; i++) {
outL[i-1]= a[i];
}
outL[n-1] = a[0]; // add last number
// Answer
cout << "Right:\n";
for(i=0; i<n; i++) {
cout << outR[i] << endl;
}
cout << "Left:\n";
for(i = 0; i < n; i++) {
cout << outL[i] << endl;
}
return 0;
}
Simple answer where you can easily see everything, good luck.
You may be interested in ,,vector coding", it seems be easier if you spend some time on this:
#include <iostream>
#include <vector>
using namespace std;
vector <int> a, outR, outL;
size_t i;
int main () {
int n, temp_int;
cin >> n;
while (n--) {
cin >> temp_int; // here you read number to your vector
a.push_back(temp_int); // here you add this to vector
// remember that vector start from element 0 as like arrays
}
// Left
// remember that last element will be first
// you may have acces to size of your vector easily
for (i = 0; i < (a.size()-1); i++) {
outL.push_back(a.at(i+1)); // here you create new vector
}
outL.push_back(a.at(0)); // add last elemet which rotated
// Right
// to rotate left first you have push last element so
outR.push_back(a.at(a.size()-1)); // add first elemet which rotated
for (i = 1; i < a.size(); i++) {
outR.push_back(a.at(i-1)); // here you push rest
}
cout << "Left" << "\n";
for (i = 0; i < a.size(); i++) {
cout << outL.at(i) << endl; // here you print value
}
cout << "Right" << "\n";
for (i = 0; i < a.size(); i++) {
cout << outR.at(i) << endl; // here you print value
}
return 0;
}
int* leftShiftOneByOneWIthoutTemp(int arr[], int sz)
{
for (int i=0 ;i < sz-1; i++)
{
arr[i] = arr[sz-1] + arr[i];
arr[sz-1] = arr[i] - arr[sz-1] ;
arr[i] = arr[i] - arr[sz-1] ;
std::cout << "iter "<< i << std::endl;
printArray(arr,5);
}
std::cout << "final "<< std::endl;
printArray(arr,5);
return arr;
}
Replace your code (to shift array left) with below code.
templ = a[0];
for(i=0;i<n-1;i++)
{
a[i] = a[i+1];
cout<<"Left: "<<a[i]<<endl;
}
a[n-1] = templ;
cout<<"Left: "<<a[n-1]<<endl;
So i'm trying to insert a number into an array in ascending order and then print the array by using only pointer notation. I tried doing this by finding the position of where the number would be inserted and then I try to store all the values at that position and after in positions further down the array. Then I want to insert the number at it's proper position and then move all of the numbers back to their position+ 1. However I think I am missing something in my pointer notation because none of my checks are showing up, so my for loops arent even being used. Any help or advice would be appreciated.
using namespace std;
int main(int argc, char *argv[])
{
int spot; // spot holder for the added number
int *pointer = NULL;
cout << "How many numbers do you want in your array" << endl;
int input;
cin >> input;
pointer = new int[input * 2 ];
for (int index = 0; index < input; index ++)
{
cout << "Enter integer number" << index + 1 << endl;
cin >> *(pointer + index);
}
for (int index = 0; index < input; index ++)
{
cout << *(pointer + index);
}
cout << endl;
cout << "What number would you like to add?" << endl;
int added;
cin >> added;
for (int index = 0; added < *(pointer + index); index++)
{
spot = index;
cout << "check .5: " << spot;
}
for (int index = spot; index < input + 1; index++)
{
*(pointer + input + index) = *(pointer + index); //& added
cout << "check 1: " << *(pointer + input + index);
}
*(pointer + spot) = added;
for (int index = spot + 1; index < input + 1; index++)
{
*(pointer + index) = *(pointer + index + input);
cout << "check 2" ;
}
for (int index = 0; index < input + 1; index ++)
{
cout << *(pointer + index);
}
cout << endl;
}
Here is a demonstrative program that shows how to do the assignment by means of standard algorithms
#include <iostream>
#include <algorithm>
int main()
{
const size_t N = 5;
int a[N] = { 2, 5, 1, 4, 3 };
int b[N];
int *first = b;
int *last = b;
for ( int x : a )
{
auto p = std::upper_bound( first, last, x );
if ( p != last )
{
std::copy_backward( p, last, last + 1 );
}
*p = x;
++last;
}
for ( int x : b ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The output is
1 2 3 4 5
The approach is that you need to fill the array placing numbers in ascending order. In this case you should use the binary search method that to determine the position where a next number has to be added. And then you simply need to shift right all existent elements starting from this position.