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++, using pass by reference

Solutions

Expert Solution

#include <iostream>
using namespace std;
void findindex(int ptr_array[],int size,int var1,int var2)
{
int Max,Min;
Min = Max =ptr_array[0];
int max_index_val = 0,min_index_val=0;
//Finding The Minimum and Maximum index value in Array
for(int i=0;i<size;i++)
{
if(ptr_array[i]>Max)
max_index_val = i;
else if(ptr_array[i]<Min)
min_index_val = i;
}
//Inserting the val1 and val2 in desired index location
ptr_array[max_index_val] = var2;
ptr_array[min_index_val] = var1;
}
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(array)/sizeof(array[0]); //Calculating the Size of array
int var1,var2;
cout<<"Enter the var1 value to Insert at lowest value index:";
cin>>var1;
cout<<"Enter the var2 value to Insert at lowest value index:";
cin>>var2;
cout<<"======Before Function call elements in array======\n";
for(int i =0;i<size;i++)
cout<<array[i]<<" ";
findindex(array,size,var1,var2);
cout<<"\n======After Function call elements in array======\n";
for(int i =0;i<size;i++)
cout<<array[i]<<" ";
return 0;
}


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++
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 "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
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(...
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.
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT