Write a c++ program to shift all the zero present in the array in the alternative order, For example.
Input array: -
1 2 0 8 5 0 0 6
Output array: -
1 0 2 8 0 5 0 6
Answers
#include <iostream>
using namespace std;
int main()
{
int a[50], n;
cout << "enter the lemght of the array::";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "enter the your" << i + 1 << "no digit at index" << i << "::";
cin >> a[i];
}
for (int i = 0; i < n; i++)
{
cout << "your number at position " << i + 1 << "::";
cout << a[i] << endl;
}
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
{
if (i % 2 == 0)
{
for (int j = 1; j < n; j = j + 2)
{
if (a[j] != 0)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
cout << "after shifting position of zeros"<<endl;
for (int i = 0; i < n; i++)
{
cout << "the digit at position" << i << "::";
cout << a[i] << endl;
}
return 0;
}