Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Negatives to the rear Goal of the code: Move all negative numbers to back of the vector while maintaining the relative order of the

C++ Negatives to the rear

Goal of the code: Move all negative numbers to back of the vector while maintaining the relative order of the non-negative numbers. Input: The input will consist of space separated integers (there will be no zeros). Output: Print out the vector contents where each element is separated by a space. End the output on a new line.

Template to follow and complete:

#include #include

void moveNegatives( std::vector & vec ){ /* Type your code here */ // Two pointer tech // Use the two pointer technique. Traverse left to right with lead pointer. Anytime the lead pointer encounters a positive number // swap it with the rear pointer. Don't forget to increment the rear pointer after swapping. /* Two pointer technique refers to using two indexes to traverse a container. An example would be: int i = 0; int j = vec.size() 1; while( i < j ){ // do something i++; j--; } */ }

}

int main() {

// Initialize a vector // Use a while loop to read the input, std::vector has the push_back() function you can use to fill the vector. // Call the moveNegatives function on the vector. // Print the contents of the vector.

std::cout << std::endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How Do I Use A Database Research Tools You Can Use

Authors: Laura La Bella

1st Edition

1622753763, 978-1622753765

More Books

Students also viewed these Databases questions