C++ Write a program that assignment 40 random numbers between 50 and 100 to an a
ID: 3661809 • Letter: C
Question
C++
Write a program that assignment 40 random numbers between 50 and 100 to an array. Print out the average of the maximum and minimum values in the array. Use the following main(). You will have to write 4 functions.
int main()
{
const int ArraySize = 40;
int array[ArraySize];
populateArray(array, ArraySize);
int minimum = arrayMin(array, ArraySize);
int maximum = arrayMax(array, ArraySize);
cout << setprecision(1) << fixed << averageOf2Ints(minimum,maximum) << endl;
}
Your output should look something like this:
74.5
Explanation / Answer
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
using namespace std;
//function declaration
int arrayMin(int [],int );
int arrayMax(int [],int );
float averageOf2Ints(int,int);
void populateArray(int[],const int);
int main()
{
const int ArraySize = 40;
int array[ArraySize];
populateArray(array, ArraySize);
int minimum = arrayMin(array, ArraySize);
int maximum = arrayMax(array, ArraySize);
cout << setprecision(1) << fixed << averageOf2Ints(minimum,maximum) << endl;
}
//function definition
void populateArray(int array[], int ArraySize)
{
int r;
cout<<"Array generated is: ";
srand(time(NULL));
for(int i=0;i<ArraySize;i++)
{
r=rand() % 50 + 50;
array[i]=r;
cout<<array[i]<<" ";
}
}
int arrayMin(int array[],int ArraySize)
{
int min=array[0];
for(int i=0;i<ArraySize;i++)
{
if(array[i]<min){min=array[i];}
}
cout<<" minimum value: "<<min;
return min;
}
int arrayMax(int array[],int ArraySize)
{
int max=array[0];
for(int i=0;i<ArraySize;i++)
{
if(array[i]>max){max=array[i];}
}
cout<<" maximum value: "<<max<<" ";
return max;
}
float averageOf2Ints(int minimum,int maximum)
{
float avg=float(minimum+maximum)/2;
return avg;
}
1. 'arrayMax and arrayMin' are simple functions which iterate through an array linearly and find a smallest or largest of array. Straightforward iterating 'for loop' is used in this purpose which changes value of min/max variable if necessary.
2. 'averageOf2Ints' function calulate average of minimum and maximum value that are passed to it.
'float' is written on right-hand-side as a casting operator, which if not written gives result in 'int'(not in float)
3. 'populateArray' function fills the array with 40 random no within range of 50-100 as follows
rand() is function (defined within <stdlib.h>) generates any random no within very large range. To confine those random no. within our required range (i.e. 50-100) we take modulus(i.e. remainder) of generated random no. by 51,so that now random no. will be within 0-50 range and then add it with 50, so that it will come within the range of 50-100. Such randomly generated no. is then put in array.
4. 'srand' function: seed the random no. generator
Note that, rand function generates random value depending on some 'seed' value which is predefined by default. So that random no. generated remains same each time you run the program. To get actullay randomized no. we need to change that seed value, by using 'srand' function(defined in <stdlib.h>). To change 'seed' value randomly I've used current time of system to as a 'srand' argument(using time(NULL) function which is defined within <ctime> library).
So,if you remove 'srand(time(NULL));' line , you will always get same no. , as rand() function is generating no. depending on same by default 'seed' value.
5.<iomanip> is included as a header, which has 'setprecision()' method defined within it.
Note: Program is written and tested in Windows environment, using 'dev-cpp' compiler
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.