Question

In: Electrical Engineering

Recommend/ Explain a program which uses an array of 20 integers whose input is taken by...

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.

Solutions

Expert Solution

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;
}


Related Solutions

Write a program whose input is two integers, and whose output is the first integer and...
Write a program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. import java.util.Scanner; public class LabProgram {...
Write a C program whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is...
6.25 LAB: Swapping variables Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 The output is: 8 3 Your program must define and call the following function. swap_values() returns the two values in swapped order. def swap_values(user_val1, user_val2) **in Python, please
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
Post a Python program that contains an array variable whose values are input by the user....
Post a Python program that contains an array variable whose values are input by the user. It should the perform some modification to each element of array using a loop and then the modified array should be displayed. Include comments in your program that describe what the program does. Also post a screen shot of executing your program on at least one test case.
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Write a program Write a program whose inputs are three integers, and whose output is the...
Write a program Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 C++ please
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT