In: Computer Science
**** In C++ ****Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7 8 9 becomes 9 8 7 ) The program main method creates a single-dimensional array of length 5 elements and initialize it with random integers between 1 and 100. The program displays the original array, then calls each of the above methods and displays their results as shown below. Document your code and organized your output following these sample runs. Sample run 1: Original array: 3, 5, 2, 6, 1 Max value: 6 Min value: 1 Squared array: 9, 25, 4, 36, 1 Reversed array: 1, 36, 4, 25, 9 Sample run 2: Original array: 3, 2, 3, 7, 2 Max value: 7 Min value: 2 Squared array: 9, 4, 9, 49, 4 Reversed array: 4, 49, 9, 4, 9 Sample run 3: Original array: 2, 2, 2, 2, 2 Maxvalue: 2 Min value: 2 Squared array: 4, 4, 4, 4, 4 Reversed array: 4, 4, 4, 4, 4
#include <iostream>
using namespace std;
// finds max element in array
int arrayMax(int arr[]){
int m=arr[0];
for(int i=1;i<5;i++)
if(m<arr[i])
m=arr[i];
return m;
}
//finds mine element in array
int arrayMin(int arr[]){
int m=arr[0];
for(int i=1;i<5;i++)
if(m>arr[i])
m=arr[i];
return m;
}
//squared every element in array
void arraySquared(int arr[]){
for(int i=0;i<5;i++)
arr[i]=arr[i] * arr[i];
}
//reverse the given array
void arraReverse(int arr[]){
int temp;
for(int i=0,j=4;i<2;i++,j--){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
int main(){
int arr[5];
for(int i=0;i<5;i++){
arr[i]=rand() % 101+1;
cout<<arr[i]<<"
";
}
cout<<"\nMax in Array:
"<<arrayMax(arr)<<endl;
cout<<"Min in Array:
"<<arrayMin(arr)<<endl;
cout<<"Squared Array: "<<endl;
arraySquared(arr);
for(int i=0;i<5;i++)
cout<<arr[i]<<"
";
arraReverse(arr);
cout<<"\n Reversed Array: "<<endl;
for(int i=0;i<5;i++)
cout<<arr[i]<<"
";
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me