Input a sequence of real numbers and create a dynamic array of
numbers with absolute values outside the interval (20, 40]. Using the function, calculate the number of elements smaller then the arithmetic mean of all elements
When they say dynamic array, do you have to create and manage this yourself or can you use the standard vector class?
I always find it best to read the assignment and pick out the details of pieces that you need to do. Write them as code comments. Then reread the assignment, adjust the comments as needed. Repeat until the comments describe code that will work:
First try:
1 2 3 4
// Input a sequence of real numbers
// create a dynamic array of a subset of the numbers.
// using the function? What function? Maybe they mean create a function
// to calculate the nnumber of elements smaller than the arithmetic mean of all elements.
Okay that's a start. Now what's with that function? What does it need for parameters? What does it return? And I guess we have to add the items to this array while inputting them:
Second try:
1 2 3 4 5 6 7 8 9 10 11
// Input the sequence of real numbers in a loop
// add each number outside the interval (20,40] to the dynamic array.
// Function to compute the number of elements smaller than arithmetic mean
int countSmallerThan(dynamicArray, arithmetic mean)
{
// for each item in array
// if it's smaller than arithmetic mean then increment counter
//
// return counter
}
Hey, we need to compute the arithmetic mean of ... all elements. Rather than storing all elements, which requires another dynamic array, we can compute the sum and count of all elements as we input them. Also, we'll have to call countSmallerThan() and print the result. Let's add the main function.
int main()
{
// Input the sequence of real numbers in a loop
// add each number outside the interval (20,40] to the dynamic array.
// add each element to the sum of elements
// count the number of elements.
// compute the average
double average = sum / count;
// output the count of items smaller than average
cout << "countSmallerThan(array, average) << '\n';
}
// Function to compute the number of elements smaller than arithmetic mean
int countSmallerThan(dynamicArray, arithmetic mean)
{
// for each item in array
// if it's smaller than arithmetic mean then increment counter
//
// return counter
}