In: Computer Science
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result.
Next, write a function getMinMax that accepts an array of floats and the size of the array, determines the minimum and maximum values in the array and displays the result. It returns the range. Range = max – min.
C++ Coding Please. Thank you!
Code:
#include <iostream>
using namespace std;
float getMinMax(float a[], int size) //Defining getMinMax
function
{
float min = a[0], max = a[0]; //Initializing min, max
variables
int i=0;
//Finding min, max values from array
for(i=0;i<size;i++)
{
if(a[i]>max)
{
max = a[i];
}
if(a[i]<min)
{
min = a[i];
}
}
//Returning range
return (max-min);
}
int main()
{
int i=0,j=0,count=0,n=0;float a[50];
//We use for loop to loop statements upto 50 times.
for(i=0;i<50;i++)
{
//Taking user input
cout<<"Enter Number "<<i+1<<" (or Enter 0 To
Exit): ";
cin>>n;
//If user enter 0, we break the loop
if(n==0)
{
break;
}
//Assigning number to array if divisible by 5
if(n%5==0)
{
a[j]=n;
j++;
}
//Incrementing count if number divisible by 10
if(n%10==0)
{
count++;
}
}
//Printing count value
cout<<"Numbers Divisible By 10:
"<<count<<endl;
//Calling getMinMax to print range of array a[]
cout<<"Range: "<<getMinMax(a,j)<<endl;
return 0;
}
Sample Output: