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.
Im trying to create a function in C where it takes an array and size and...
Im trying to create a function in C where it takes an array and size and returns the largest absolute value in the array (either negative or positive) using only stdio.h. Any help would be greatly appreciated! Ex: if the array is { -2.5, -10.1, 5.2, 7.0}, it should return -10.1 Ex: if the array is {5.1, 2.3, 4.9, 1.0}, it should return 5.1. double getMaxAbsolute(double array[], int size) {     double max = array[0];    double abs[size];    for...
Create a function output() in C that takes the pointer to the array and its size...
Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (function prototype : void output(int *arrayPtr, int size);
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.
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT