In: Electrical Engineering
Recommend/ Explain a 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.
Here as the language is not specified so I am providing the code in C. And by observing the functions it is also clear that the code should be in C.
Here is the code :
#include <stdio.h>
// you can change the value of N if you want to take more number of
input
// it tells how many numbers you want to enter
#define N 20
// this function takes the whole array as index and returns
the
// maximum integer in the list
int max(int arr[]){
int max_num = arr[0];
for (int i = 0; i < N; i++){
if (arr[i] > max_num){
max_num = arr[i];
}
}
return max_num;
}
// this function takes the whole array as index and returns
the
// minimum integer in the list
int min(int arr[]){
int min_num = arr[0];
for (int i = 0; i < N; i++){
if (arr[i] < min_num){
min_num = arr[i];
}
}
return min_num;
}
// this function takes the array to be modified, the index at
which the value
// need to be modified and the changed value at that index
void modify(int * arr, int index,int changed_value){
*(arr + index) = changed_value;
return;
}
int main()
{
int numbers[N];
int max_of_all_number;
int min_of_all_number;
printf("Enter %d numbers:\n", N);
//take input from the users and store in an array
for (int i = 0 ; i < N; i++){
printf("\nEnter the %d th number: ",i);
scanf("%d", &numbers[i]);
}
//find the maximum using the max() functiona and print it
finally
max_of_all_number = max(numbers);
printf("\nThe maximum of all the numbers entered:
%d",max_of_all_number );
//find the minimum using the min() functiona and print it
finally
min_of_all_number = min(numbers);
printf("\nThe minimum of all the numbers entered:
%d",min_of_all_number );
//here we are modifing the number at index(eg.2nd as here)
// by the changed_number(eg.876 here)
int index_to_modify = 2;
int changed_number = 876;
modify(numbers, index_to_modify , changed_number);
printf("\nAll the numbers in the array after modifing
are\n");
//print the array again to see that the numbers are changed
properly
for (int i = 0 ; i < N; i++){
printf("%d\n",numbers[i]);
}
return 0;
}