w3codeworld logo
  • Home
  • C
  • C++
  • Java
  • Python3
  • JavaScript
  • C#
  • PHP
  • Codeigniter
  • Laravel
  • Articles
  1. Home
  2. Articles
  3. How to rearrange positive and negative numbers in array in c++ language
  • PHP
  • Codeigniter
  • Laravel
  • C
  • C++
  • Java
  • Python3
  • JavaScript
  • C#

How to rearrange positive and negative numbers in array in c++ language

  • Author: Admin
  • Last Updated: 2020-12-30
  • Category: C++

How to rearrange positive and negative numbers in array in c++ language. In this program, you will learn how to rearrange positive and negative numbers using the loops & functions.

 

Take an example to rearrange the elements through a c++ program:

// How to rearrange positive and negative
// numbers in array in c++ language
#include <bits/stdc++.h>
using namespace std;

void alterNumber(int *arru, int i, int j)
{
    int temp = arru[i];
    arru[i] = arru[j];
    arru[j] = temp;
}

void splitNegativeElement(int *arru, int size)
{
    int temp, left = 0, right = size - 1;
    while (right > left)
    {
        while (arru[left] < 0)
            left++;

        while (arru[right] > 0)
            right--;

        if (left < right)
        {
            alterNumber(arru, left, right);
        }
    }
}

// It's the driver function of
// array element arrangements
void reArrangeNumbers(int *arru, int size)
{
    int i, j;
    splitNegativeElement(arru, size);

    for (i = 0; arru[i] < 0; i++);
    for (j = 1; (j < i) && (arru[j] < 0); j += 2)
    {
        alterNumber(arru, i, j);
        i++;
    }
    return;
}

int main()
{
    int i, arru[] = { -24, 28, -25, -26, 25, 39, 37, 31, -31, -31, 39 };
    int arru_size = sizeof(arru) / sizeof(arru[0]);

    // It's the array iteration
    cout << "------This is the given array before arrangement------\n\t";
    for (i = 0; i < arru_size; i++)
    {
        cout << arru[i] << ",\t";
    }
    cout << "\n\n";

    // It's the array iteration
    cout << "------This is the Re-arranged array------\n\t";
    // This will re-arrange the given array
    reArrangeNumbers(arru, 10);
    for (i = 0; i < 11; i++)
    {
        cout << arru[i] << ",\t";
    }
    cout << "\n";

    return 0;
}

 

Output:

------This is the given array before arrangement------
    -24,    28,    -25,    -26,    25,    39,    37,    31,    -31,    -31,    39,    

------This is the Re-arranged array------
    -24,    39,    -25,    37,    -31,    31,    -26,    25,    -31,    28,    39,

 

Tags:

# separate positive and negative numbers in array in c++

# write ac++ program to arrange a group of numbers into positive and negative numbers using the array

# how to segregate positive and negative numbers in an array

 

Related Articles

Write a c++ program to store information of a student using the structure with the for loop How to write a c++ program to find the sum of natural numbers using recursion Write a c++ program to rotate the matrix by k times in a clockwise direction using the function Write a c++ program to print a hollow square star pattern with diagonal using loops (for and while loop) How to rearrange positive and negative numbers in array in c++ language How to print a star pattern without using loop in the c++ programming language C++ program to find the number occurring the odd number of times in an array Write a program of spiral pattern printing in c++ language Write a c++ program to convert an array into zig-zag fashion using the function Write a c++ program to find the sum of a zig-zag pattern in a given matrix using the recursion How to find the sum of each row and column of a matrix in c++ language Write a program to find the transpose of a matrix in c++ language Write a program to make transformation matrix rotation 90 degrees clockwise in c++ language How to print the hollow diamond pattern in c++ language Write a program to find quotient and remainder in c++ language Write a program to draw the half pyramid pattern in c++ language of stars using the for loop How to print the rhombus pattern in the c++ language of the stars using the for loop Write a program to print the left & right arrow pattern in c++ language Write a c++ program to draw a rectangle using for loop Write a program to print the pascal triangle pattern in c++ language Write a program to print Floyd's triangle number pattern in c++ language Write a program to print the matrix pattern in c++ language using the for loop C++ program to print diamond pattern of stars by using the for loop Write a c++ program to print the butterfly pattern of the numbers by using the for loop How to check whether a number is an integer or float in c++ language by using the while loop Write a c++ program to find factors of a number using while loop & for loop How to swap the two integers or real numbers without using the third variable in the c++ language Write a c++ program to convert the reverse case of an input character Write a c++ program to check whether a character is an alphabet or not by using the if-else statement and using the conditional operator Write a c++ program to check the input integer number is an Armstrong number using the for loops & while loops C++ program to check leap year using the if-else and by using the switch case statement C++ program to find the roots of a quadratic equation using the sqrt() function with the if-else condition C++ program to find GCD of two numbers using the while loop and using the for loop C++ program to find LCM of two numbers using for loop and with GCD calculation C++ program to calculate the power of N number using for loop and by using pow function C++ program to find the Normal & Trace of a square matrix by using the for loop C++ program to multiply two same dimension matrices by using the for loop C++ program to find the SUM of the N input numbers using arrays with for loop C++ program to find the largest and smallest element in an array by using for loop C++ program to print the multiplication table of any integer number with multiplication range C++ program to print prime numbers from 1 to N using for loop C++ program to check input number is even or odd using if-else statement and with conditional operator C++ program to find the average of N numbers by using for loop C++ program to reverse a string by using the while & for loop C++ program to find sum of digits of a number using while loop of any integer number C++ program to check palindrome number using while loop or not using function of any number C++ program to generate the fibonacci series using iteration C++ program to find factorial of a number using for loop C++ program to check the input character is a vowel or consonant character C++ program of decision making by using the switch case statement
Recent Articles
  • Write a program to find all the patterns of 0(1+)0 in the given string
  • C# program to enter two numbers and find their sum
  • Write a c++ program to store information of a student using the structure with the for loop
  • Write a c program to store information of a student using the structure with the for loop
  • How to write a c++ program to find the sum of natural numbers using recursion
  • How to write a c program to find the sum of natural numbers using recursion
  • Write a python program to convert an array into zig-zag fashion using the function
  • C program to perform input/output of all basic data types
  • Write a python program to print a hollow square star pattern with diagonal using loops (for and while loop)
  • Write a c program to make a circular rotation of an array by k positions using the loops
View All Articles
  W3Codeworld

Get the latest updates

  • ABOUT US
  • About Company
  • Contact Us
  • Terms Conditions
  • Privacy Policy
  • Top Tutorials
  • PHP
  • Codeigniter
  • Laravel
  • Top Articles
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • PHP

© Copyright 2020-2021. All Rights Reserved.