Question

In: Computer Science

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++

Solutions

Expert Solution

void findIndex(int *arr, int size, int a, int b) {
int i;
int max = arr[0];
int min = arr[0];
for (i = 1; i < size; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
}
arr[a] = min;
arr[b] = max;
}


Related Solutions

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 ‘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,...
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.
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
Write a function named timesOfLetter that reads an array and returns the number of times of...
Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase letter and each uppercase letter appear in it, using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it, using reference parameter. • Write a function named isChar() that determines if the input is alphabetic or not during inputting. • Write a function...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a boolean function named isMember that takes two arguments: an array of type char and...
Write a boolean function named isMember that takes two arguments: an array of type char and a value. It should return true if the value is found in the array, or false if the value is not found in the array. PLEASE WRITE FULL PROGRAM IN C++ AND USE RECURSION AND DO NOT USE LOOPS
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT