Question

In: Computer Science

Write code that finds the largest number in data and stores it in the variable max...

Write code that finds the largest number in data and stores it in the variable max and the smallest number in data and stores it in min.

Test Cases

Test case #1

Expected result: max is 52.66; min is 15.53

Test case #2

Expected result: max is 56.95; min is 5.77

Test case #3

Expected result: max is 77.02; min is 24.24

Test case #4

Expected result: max is 90.48; min is 35.94.

Solutions

Expert Solution

Given, An array having some elements and we have to find the maximum of all the elements in that array and also the minimum of all elements and storing their corresponding value in max and min.

We will first apply a simple logic to store maximum of all elements in the given data (array). Similarly further by doing one change, we can also find the minimum of all elements. Let's see the same in following steps:

1. Create a variable max_ele and store the value 0.0 initially in it.

2. Traverse through the array and compare the next element each time whether it is greater than max_ele or not, and if it is, then simply assign that value of element in our max_ele variable.

3. Repeat the same process again for all elements of array and finally we'll be having our maximum element in that array.

4. Print the max_ele out of the loop.

Above Process is same for minimum also, just change the variable to min_ele and store any big value i.e 32768.0. Also, each time compare whether each element is smaller or not and if it is, store the same in min_ele and print the min_ele.

The following code is in C++ language.

Example : A = {46.87, 32.54, 52.66, 15.53}

#include <iostream>
using namespace std; 

int main(void){

double A[] = {46.87, 32.54, 52.66, 15.53};
double max_ele = 0.0, min_ele = 329484.0;
for(int i=0; i<4; i++){
if(A[i] > max_ele) max_ele = A[i];
if(A[i] < min_ele) min_ele = A[i];
}
cout << "max is " << max_ele << "\n";
cout << "min is " << min_ele ;
return 0;
}

Output : max is 52.66; min is 15.53.

Same is for all other test cases also. Hope you understand. Thank You !


Related Solutions

Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
For this problem, let ke = max{2,the largest even number in your student number}, ko =...
For this problem, let ke = max{2,the largest even number in your student number}, ko = max{3,the largest odd number in your student number} So, e.g., if your student number is 5135731, then ke = 2 and ko = 7. The data in Table 2 represents points (xi,yi) sampled from an experimentally generated triangular wave function with period 2π. (a) Use the least-squares technique we developed in class to estimate the coefficients a, b and c for the optimal least-squares...
Write an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
assume that the data are stored in variable names x and y. write a matlab code...
assume that the data are stored in variable names x and y. write a matlab code to plot the quadratic spline and along with the data points x= 1 2 3 4 5 6 y= 40 78 125 256 348 425
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
what is the best code to construct a C++ program that finds a five digit number;...
what is the best code to construct a C++ program that finds a five digit number; This number should reverses the order of its digits when multiplied by four. Also, how many five digits numbers are there in which the sum of the digits is even.
Using the data set as a pre-defined variable in your program, write code that uses the...
Using the data set as a pre-defined variable in your program, write code that uses the dataset to print the first names of people who have BOTH above average math grades AND below average age from the dataset. The solutions for the textbook examples assume you are able to export in a framework like node.js, which is why in the data set I provide, I simply set the array of objects as a variable. Your code will be in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT