Screenshot #4: Menu Option Listing Please select one of the following options fo
ID: 3606943 • Letter: S
Question
Screenshot #4: Menu Option Listing Please select one of the following options for data retrieval from the array: 1: Retrieve a specific value from an array (based a specific position in the array) 2: Retrieve the highest value in the array 3: Retrieve the lowest value in the array 4: Retrieve the sum of all values in the array 5: Retrieve the average of all values in the array ENTER AN OPTION: I Screenshot #5: Option #1-Retrieve a specific value from an array ENTER AN OPTION: 1 Enter the position of the array to retrieve this number: 3 The number retrieved is: 11.3 Screenshot #6: Option #2-Retrieve the highest value in the array ENTER AN OPTION: 2 The highest number in array is: 87.2 Screenshot #7: Option #3-Retrieve the lowest value in the array ENTER AN OPTION: 3 The lowest number in array is: 11.3 Screenshot #8: Option #4-Retrieve the sum of all values in the array ENTER AN OPTION: 4 The sum of the values in the array is: 220.9 Screenshot #9: Option #5-Retrieve the average of all values in the array ENTER AN OPTION: 5 The average of the values in the array is: 44.2 Final Notes: For Option 5, also use the appropriate procedures (ex. fixed, showpoint, setprecision(), etc.) to output the average using one decimal place As a reminder, submit your solution as a .cpp to Blackboard no later than 11:59pm on Wednesday, November 1t. Failure to submit your solution as a .cpp file will result in a penalty to your grade for this assignment.Explanation / Answer
Given below is the code with output. Please don't forget to rate the answer if it helped.
Note: Since I am not sure if you know dynamic allocation of memory using new operator, I have used static arrays. Let me know if you want to have dynamic allocation of array. Then the constructor and destructor will be different. Post a comment if any issues, I will help.
compile: g++ Array.cpp main.cpp
run: ./a.out
Array.h
#ifndef Array_h
#define Array_h
#include <iostream>
class Array
{
private:
static int const MAX_SIZE = 100;
float numbers[MAX_SIZE]; //declare an array of max size;
int n; //the number of elemetns in array
public:
Array(int n);
void set(int index, float value);
float get(int index);
float max();
float min();
float sum();
float avg();
int size();
};
#endif /* Array_h */
Array.cpp
#include "Array.h"
Array::Array(int n)
{
if(n <= 0)
n = 1;
this->n = n;
//initialize all elemetns to 0
for(int i = 0; i < n; i++)
numbers[i] = 0;
}
void Array::set(int index, float value)
{
if(index >= 0 && index < n)
numbers[index] = value;
}
float Array::get(int index)
{
if(index >= 0 && index < n)
return numbers[index];
else
return 0;
}
float Array::max()
{
float maximum = 0;
for(int i = 0 ; i < n; i++)
if(numbers[i] > maximum)
maximum = numbers[i];
return maximum;
}
float Array::min()
{
float minimum = numbers[0];
for(int i = 1 ; i < n; i++)
if(numbers[i] < minimum)
minimum = numbers[i];
return minimum;
}
float Array::sum()
{
float sum = 0;
for(int i = 0 ; i < n; i++)
sum += numbers[i];
return sum;
}
float Array::avg()
{
return sum() / n;
}
int Array::size()
{
return n;
}
main.cpp
#include <iostream>
#include <iomanip>
#include "Array.h"
using namespace std;
int main()
{
int n;
int index;
double value;
cout << "Please enter the size of your array: ";
cin >> n;
Array arr(n);
for(int i = 1; i <= n; i++)
{
cout << "Enter number #" << i << ": ";
cin >> value;
cout << "What position of the array should this number be stored? ";
cin >> index;
arr.set(index, value);
cout << endl;
}
cout << fixed << showpoint << setprecision(1);
cout << "Below are the values stored in the array" << endl;
for(int i = 0; i < n; i++)
cout << arr.get(i) << " " ;
cout << endl;
bool stop = false;
int option;
while(!stop)
{
cout << "Please select one of the following options -" << endl;
cout << "---------------------------------------------" << endl;
cout << "1. Retrieve a specific value based on position" << endl;
cout << "2. Retrieve highest value in array" << endl;
cout << "3. Retrieve lowest value in array" << endl;
cout << "4. Retrieve the sum of all values in array" << endl;
cout << "5. Retrieve the average of all values in array" << endl;
cout << "6. Exit" << endl;
cout << "Your option: ";
cin >> option;
switch(option)
{
case 1:
cout << "Enter the index of element needed: ";
cin >> index;
cout << "Value at index " << index << " is " << arr.get(index) << endl << endl;
break;
case 2:
cout << "Highest value is " << arr.max() << endl << endl;
break;
case 3:
cout << "Lowest value is " << arr.min() << endl << endl;
break;
case 4:
cout << "Sum of all elemnents is " << arr.sum() << endl << endl;
break;
case 5:
cout << "Average of all elements is " << arr.avg() << endl << endl;
break;
case 6:
stop = true;
break;
default:
cout << "Invalid option!" << endl;
}
}
}
output
Please enter the size of your array: 5
Enter number #1: 43.5
What position of the array should this number be stored? 2
Enter number #2: 87.2
What position of the array should this number be stored? 0
Enter number #3: 23.2
What position of the array should this number be stored? 1
Enter number #4: 55.7
What position of the array should this number be stored? 4
Enter number #5: 11.3
What position of the array should this number be stored? 3
Below are the values stored in the array
87.2 23.2 43.5 11.3 55.7
Please select one of the following options -
---------------------------------------------
1. Retrieve a specific value based on position
2. Retrieve highest value in array
3. Retrieve lowest value in array
4. Retrieve the sum of all values in array
5. Retrieve the average of all values in array
6. Exit
Your option: 1
Enter the index of element needed: 3
Value at index 3 is 11.3
Please select one of the following options -
---------------------------------------------
1. Retrieve a specific value based on position
2. Retrieve highest value in array
3. Retrieve lowest value in array
4. Retrieve the sum of all values in array
5. Retrieve the average of all values in array
6. Exit
Your option: 2
Highest value is 87.2
Please select one of the following options -
---------------------------------------------
1. Retrieve a specific value based on position
2. Retrieve highest value in array
3. Retrieve lowest value in array
4. Retrieve the sum of all values in array
5. Retrieve the average of all values in array
6. Exit
Your option: 3
Lowest value is 11.3
Please select one of the following options -
---------------------------------------------
1. Retrieve a specific value based on position
2. Retrieve highest value in array
3. Retrieve lowest value in array
4. Retrieve the sum of all values in array
5. Retrieve the average of all values in array
6. Exit
Your option: 4
Sum of all elemnents is 220.9
Please select one of the following options -
---------------------------------------------
1. Retrieve a specific value based on position
2. Retrieve highest value in array
3. Retrieve lowest value in array
4. Retrieve the sum of all values in array
5. Retrieve the average of all values in array
6. Exit
Your option: 5
Average of all elements is 44.2
Please select one of the following options -
---------------------------------------------
1. Retrieve a specific value based on position
2. Retrieve highest value in array
3. Retrieve lowest value in array
4. Retrieve the sum of all values in array
5. Retrieve the average of all values in array
6. Exit
Your option: 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.