Question

Write a method that take in an array of integers, find the maximum and minimum, and...

Write a method that take in an array of integers, find the maximum and minimum, and return an array containing four elements in the following order.

max integer, max integer index, min integer, min integer index

Homework Answers

Answer #1

Solution:

Function:

int* max_min(int *arr){
int max=arr[0],min=arr[0]; // initialises the max and min to first value of the array.
int *array=new int[4]; // resultant array.
int max_integer_index=0,min_integer_index=0;// indexes are initialised to 0.
for(int i=1;i<n;i++){
if(arr[i]>max){ // checks if current element is greater than max.
max=arr[i];
max_integer_index=i;
}
if(arr[i]<min){ // checks if current element is less than min.
min=arr[i];
min_integer_index=i;
}
}
// initialises the resultant array.
array[0]=max;
array[1]=max_integer_index;
array[2]=min;
array[3]=min_integer_index;
return array; // returns the array.
}

Program implementation:

#include <iostream>
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int n; // variable n to decide the size of array.
int* max_min(int *arr){
int max=arr[0],min=arr[0]; // initialises the max and min to first value of the array.
int *array=new int[4]; // resultant array.
int max_integer_index=0,min_integer_index=0;// indexes are initialised to 0.
for(int i=1;i<n;i++){
if(arr[i]>max){ // checks if current element is greater than max.
max=arr[i];
max_integer_index=i;
}
if(arr[i]<min){ // checks if current element is less than min.
min=arr[i];
min_integer_index=i;
}
}
// initialises the resultant array.
array[0]=max;
array[1]=max_integer_index;
array[2]=min;
array[3]=min_integer_index;
return array; // returns the array.
}

int main()
{
cout<<"Enter n value: ";
cin>>n;
int arr[n];// declares the array.
cout<<n<<endl;
cout<<"Enter n elements for array: ";
// takes input array elements and prints out array elements.
for(int i=0;i<n;i++){
cin>>arr[i];
cout<<arr[i]<<" ";
}
cout<<endl;
int* t=max_min(arr);// function call max_min() method.
// prints the contents of resulatant array returned from the function.
cout<<"Max element: "<<t[0]<<endl;
cout<<"Max Integer Index: "<<t[1]<<endl;
cout<<"Min element: "<<t[2]<<endl;
cout<<"Min Integer Index: "<<t[3]<<endl;
return 0;
}

Program screenshot:

Program input and output screenshot:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java method to print the maximum and minimum value of an array containing elements...
Write a Java method to print the maximum and minimum value of an array containing elements [7,20,29,0,4,30,24,100]
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
Write a program that uses an array of integers initialized to whatever values you wish. Write...
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10) In Java
Java Lab Assignment Write a method called convertToString. The method will take an array of chars...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars as an argument and it will return a String. Each index of the array will be part of the each index of the String. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for *...
Write a program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
USING C++. The following divide-and-conquer algorithm is proposed for finding the simultaneous maximum and minimum: If...
USING C++. The following divide-and-conquer algorithm is proposed for finding the simultaneous maximum and minimum: If there is one item, it is the maximum and minimum, and if there are two items, then compare them, and in one comparison you can find the maximum and minimum. Otherwise, split the input into two halves, divided as evenly as possibly (if N is odd, one of the two halves will have one more element than the other). Recursively find the maximum and...
Using Java write all 4 methods in one class 1) Write a value-returning method that returns...
Using Java write all 4 methods in one class 1) Write a value-returning method that returns the number of elements in an integer array. 2) Write a void method that multiples by 2 all the elements in an array of float. 3) Write a value- returning method that returns the product of all elements in an integer array. 4) Write a method that returns the total # of elements greater or equal to 90 in an array of integers.
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an argument and it will return an array of chars. Each index of the String will be stored in each index of the array. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for * @return...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT