Write a C++ program to find the maximum and minimum of a data set. 1. Write a fu
ID: 3699469 • Letter: W
Question
Write a C++ program to find the maximum and minimum of a data set.
1. Write a function to find the maximum of a data set and position of the maximum; write another function to find the minimum of a data set and position of the minimum.
2. Write a C++ program to input a set of experimental data from the keyboard, store them in an array. Find the maximum, minimum, and their positions by calling the functions. Display the results on the screen.
2. Write a C++ program to find the maxim um and minimum of a data set. 1) Write a function to find the maximum of a data set and position of the maximum; write another function to find the minimum of a data set and position of the minim um 2) Write a C++ program to input a set of experimental data from the keyboard, store them in an array. Find the maximum, minimum, and their positions by calling the functions. Display the results on the screenExplanation / Answer
1.
int main()
{
int i, n,position;
position=0;
int arr[] = {10, 20, 30, 40};
smallestElement(arr[]);
largestElement(arr[]);
int largestElement(int arr[]){
// Loop to store largest number to arr[0]
for(i = 1;i < n; ++i)
{
if(arr[0] < arr[i]) // Comparing arr[0] with next possible element inreasing the index by 1
arr[0] = arr[i]; // If true , assign the value to arr[0]
position = i; // set the position also
}
cout << "Largest element = " << arr[0];
cout << " Largest element at position = "<< position;
return 0;
}
int smallestElement(int arr[]){
// Loop to store smallest number to arr[0]
for(i = 1;i < n; ++i)
{
if(arr[0] > arr[i])
arr[0] = arr[i];
position = i;
}
cout << "Smallest element = " << arr[0];
cout << " Smallest element at position = "<< position;
return 0;
}
}
2.
#include <iostream>
using namespace std;
int main()
{
int i, n,position;
position=0;
float arr[10];
cout << "Enter total number of elements(1 to 10): ";
cin >> n;
cout << endl;
// Store input number entered by the user in array
for(i = 0; i < n; ++i)
{
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}
smallestElement(arr[]);
largestElement(arr[]);
int largestElement(float arr[]){
// Loop to store largest number to arr[0]
for(i = 1;i < n; ++i)
{
if(arr[0] < arr[i]) // Comparing arr[0] with next possible element inreasing the index by 1
arr[0] = arr[i]; // If true , assign the value to arr[0]
position = i; // set the position also
}
cout << "Largest element = " << arr[0];
cout << " Largest element at position = "<< position;
return 0;
}
int smallestElement(float arr[]){
// Loop to store smallest number to arr[0]
for(i = 1;i < n; ++i)
{
if(arr[0] > arr[i])
arr[0] = arr[i];
position = i;
}
cout << "Smallest element = " << arr[0];
cout << " Smallest element at position = "<< position;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.