Question: placeNumbers is a function that should take a vector of n integers and a string of length n1 as input and return a vector of

placeNumbers is a function that should take a vector of n integers and a string of length n1 as input and return a vector of n integers.placeNumbers should return an ordering of the numbers such that the conditions encoded in the string are met. A value in the string will be < if the number that would be to its left is less than the number that would be to its right, and > otherwise. It may be assumed that the vector of numbers will be in ascending order.

Source code :

#include

#include

using namespace std;

vector placeNumbers( vector numbers , string signs ) {

return numbers;

}

File to test the program :

#include

#include

#include

#include

using namespace std;

int indexOf( vector vec , int val ){

for( int z = 0; z < vec.size(); z++ ){

if( vec[z] == val ){

return z;

}

}

return -1;

}

int main( int argc , char* argv[] ){

int number;

vector numbers;

string signs;

cin >> signs;

while( cin >> number ){

numbers.push_back( number );

}

vector used( numbers.size() , false );

try{

vector result = placeNumbers( numbers , signs );

bool okay = true;

for( int i = 0; i < signs.length(); i++ ){

cout << result[i];

if( signs[i] == '<' ){

cout << " < ";

int ind = indexOf( numbers , result[i] );

okay = okay && result[i] < result[i+1] && ind >= 0 & !used[ind];

used[ind] = true;

}

else{

cout << " > ";

int ind = indexOf( numbers , result[i] );

okay = okay && result[i] > result[i+1] && ind >= 0 & !used[ind];

used[ind] = true;

}

}

cout << result[signs.length()] << endl;

int ind = indexOf( numbers , result[signs.length()] );

okay = okay && ind >= 0 & !used[ind];

used[ind] = true;

for( int i = 0; i < used.size(); i++ ){

okay = okay && used[i];

}

cout << endl;

if( okay ){

cout << "VALID" << endl;

}

else{

cout << "INVALID" << endl;

}

}

catch( exception e ){

cerr << "An error occurred." << endl;

}

}

Input :

<><>>< 10 16 26 51 60 74 96

Expected Output:

10 ^ 96 v 16 ^ 74 v 60 v 26 ^ 51

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!