Question

In: Computer Science

In C++, create a function exchangesl that takes an argument of an array of integers (...

In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.

Use the following file:

Tester.cpp

#include <vector>
#include <iostream>
using namespace std;

void exchangesl(vector<int>&);

void print(vector<int> v)
{
   for (int i = 0; i < v.size(); i++)
   {
      if (i == 0) cout << "["; else cout << ", ";
      cout << v[i];
   }
   cout << "]" << endl;
}

main()
{
   vector<int> a = { 1, 9, -1, 4, -1, 6, 11, 8 };
   exchangesl(a);
   print(a);
   cout << "Expected: [1, 9, -1, 4, 11, 6, -1, 8]" << endl;

   vector<int> b = { 1, 9, -1, 4, -1, 6, 11, 8, 9, -1 };
   exchangesl(b);
   print(b);
   cout << "Expected: [1, 9, -1, 4, -1, 6, -1, 8, 9, 11]" << endl;
   
   vector<int> c = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
   exchangesl(c);
   print(c);
   cout << "Expected: [1, 2, 3, 4, 1, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 5]" << endl;     
}

Solutions

Expert Solution

Given below is completed code with output. Please do rate the answer if it helped. Thank you.
#include <vector>
#include <iostream>
using namespace std;

void exchangesl(vector<int>& v);

void print(vector<int> v)
{
for (int i = 0; i < v.size(); i++)
{
if (i == 0) cout << "["; else cout << ", ";
cout << v[i];
}
cout << "]" << endl;
}

main()
{
vector<int> a = { 1, 9, -1, 4, -1, 6, 11, 8 };
exchangesl(a);
print(a);
cout << "Expected: [1, 9, -1, 4, 11, 6, -1, 8]" << endl;

vector<int> b = { 1, 9, -1, 4, -1, 6, 11, 8, 9, -1 };
exchangesl(b);
print(b);
cout << "Expected: [1, 9, -1, 4, -1, 6, -1, 8, 9, 11]" << endl;

vector<int> c = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
exchangesl(c);
print(c);
cout << "Expected: [1, 2, 3, 4, 1, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 5]" << endl;   
}

void exchangesl(vector<int>& v){
int smallIdx = 0, bigIdx = 0;
for(int i = 1; i < v.size(); i++){
if(v[i] <= v[smallIdx])
smallIdx = i;
else if(v[i] > v[bigIdx])
bigIdx = i;
}
  
if(smallIdx != bigIdx){
//swap
int temp = v[smallIdx];
v[smallIdx] = v[bigIdx];
v[bigIdx] = temp;
}
}


output
------
[1, 9, -1, 4, 11, 6, -1, 8]
Expected: [1, 9, -1, 4, 11, 6, -1, 8]
[1, 9, -1, 4, -1, 6, -1, 8, 9, 11]
Expected: [1, 9, -1, 4, -1, 6, -1, 8, 9, 11]
[1, 2, 3, 4, 1, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 5]
Expected: [1, 2, 3, 4, 1, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 5]


Related Solutions

In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT