Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose input is taken by user, the array is passed to a functions named i.e. smallest(int A[20) and largest(int B[20]) to determine minimum and maximum values respectively.
Also create a function named modify(int *p) which modifies the value at the index given by user.
//C++ code:
#include <bits/stdc++.h>
using namespace std;
int largest(int B[20]) {
int i;
// Initializing maximum element
int max = B[0];
// Traverse array elements
// from second and compare
// every element with current max
for (i = 1; i < 20; i++)
if (B[i] > max)
max = B[i];
return max;
}
int smallest(int A[20]) {
int i;
// Initializing minimum element
int min = A[0];
// Traverse array elements
// from second and compare
// every element with current min
for (i = 1; i < 20; i++)
if (A[i] < min)
min = A[i];
return min;
}
void modify(int *p){
int index,val,i;
cout<<"Enter the index of element to be
modified: ";
cin>>index;
cout<<"Enter the value to modify it with:
";
cin>>val;
p[index]=val;
cout << "The array after modification:
";
//printing array elements
for(i=0;i<20;i++){
cout <<
p[i]<<" ";
}
return;
}
// Driver Code
int main()
{
int a[20],i;
cout<<"Enter the elements of array:
";
for(i=0;i<20;i++){
cin>>a[i];
}
cout << "Largest value in the array is
"
<<
largest(a)<<endl;
cout << "Smallest value in the array is
"
<<
smallest(a)<<endl;
modify(a);
return 0;
}
OUTPUT:
Hope this helped. Please do upvote and if there are any queries please ask in the comments section.
Get Answers For Free
Most questions answered within 1 hours.