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.

Solutions

Expert Solution


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

void exchangesl(vector<int>& a){
   int minIndex = 0;
   int maxIndex = 0;
   for(int i = 0;i<a.size();i++){
      if(a[i] < a[minIndex]){
         minIndex = i;
      }
      if(a[i] > a[maxIndex]){
         maxIndex = i;
      }
   }
   int t = a[minIndex];
   a[minIndex] = a[maxIndex];
   a[maxIndex] = t;
}

int main()
{
   vector<int> v;
   v.push_back(4);
   v.push_back(5);
   v.push_back(1);
   v.push_back(7);
   v.push_back(2);
   
   exchangesl(v);
   
   for(int i = 0;i<v.size();i++){
      cout<<v[i]<<endl;
   }
   return 0;
}

4
5
7
1
2

==============================

void exchangesl(vector<int>& a){
   int minIndex = 0;
   int maxIndex = 0;
   for(int i = 0;i<a.size();i++){
      if(a[i] < a[minIndex]){
         minIndex = i;
      }
      if(a[i] > a[maxIndex]){
         maxIndex = i;
      }
   }
   int t = a[minIndex];
   a[minIndex] = a[maxIndex];
   a[maxIndex] = t;
}


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. Use the following file:...
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