TheFestFever.com

Our aim is to connect students from campuses all over Delhi by making them aware of all Technical, Cultural, Management fests, workshops, Seminars organized by every college in Delhi.

Join Us Now

Program 13: Write a C++ program to rearrange an array in such a manner that no two +ve or two –ve numbers kept together.

#include<iostream>
using namespace std;

int main()
{
	int size;
	cout<<"\nenter the size of an array : ";
	cin>>size;
	int arr[size];
	cout<<"\nenter array elements : ";

	for(int i=0;i<size;i++)
	{
		cin>>arr[i];
	}

	for(int i=0;i<size-1;i++)
	{
		int j=i+1;
		while(((arr[i]>0&&arr[j]>0)||(arr[i]<0&&arr[j]<0))&&(j<size-1))
		{
			j++;
		}
		int temp=arr[i+1];
		arr[i+1]=arr[j];
		arr[j]=temp;

		if(((arr[i]>0&&arr[j]>0&&arr[0]<0)||(arr[i]<0&&arr[j]<0&&arr[0]>0))&&(j=size-1))
		{
			int temp=arr[j];
			for(int k=size-2;k>=0;k--)
			arr[k+1]=arr[k];
			arr[0]=temp;
		}
	}

	cout<<"\narray after rearranging : \n";
	for(int i=0;i<size;i++)
	{
		cout<<arr[i]<<"\t";
	}
	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.