Using these three functions write a program with the correct calls, prototypes a
ID: 3881022 • Letter: U
Question
Using these three functions write a program with the correct calls, prototypes and function implementations that will fill up two arrays with integers using calls to the fillArray function: int tiny[55]; int huge(55000) The program should compare the ranges of the two arrays using the range function and, then, print the array with the largest range (using the printArray function). As a little hint, as implementation of the printArray function, you can use the following void printArray (int anyArray, int anyLen) t for (int k = 0; kExplanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
// function to fill the array with random numbers
void fillArray(int arr[], int n);
// function to print the array
void printArray(int anyAray[], int anyLen);
// find the range of the array
// rand = max element - min element
int range(int arr[], int n);
int main()
{
int tiny[55];
int huge[55000];
// cal the fillArray() function to fill the
// array with random numbers
fillArray(tiny, 55);
fillArray(huge, 55000);
// get the range of tiny array
int range_tiny = range(tiny, 55);
// get the range of huge array
int range_huge = range(huge, 55000);
if(range_huge > range_tiny)
{
cout<<"Larger Array ... ";
printArray(huge, 55000);
}
else
{
cout<<"Smallger Array ... ";
printArray(tiny, 55);
}
}
// function to fill the array with random numbers
void fillArray(int arr[], int n)
{
int i;
for( i = 0 ; i < n ; i++ )
// generate a random number using rand() function
arr[i] = rand();
}
// function to print the array
void printArray(int anyArray[], int anyLen)
{
int k;
for( k = 0 ; k < anyLen ; k++ )
cout<<anyArray[k]<<" ";
}
// find the range of the array
// rand = max element - min element
int range(int arr[], int n)
{
// store the maximum value of array
int max = INT_MIN;
// store the minimum value of array
int min = INT_MAX;
int i;
for( i = 0 ; i < n ; i++ )
{
// if the current element is smaller than the min element
if(arr[i] < min)
min = arr[i];
// if the current element is larger than the max element
if(arr[i] > max)
max = arr[i];
}
// calculate the range
int range = max -min;
return range;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.