I have written a fully functioning program that prompts user to type in values, stores them in an array, outputs array in ascending order and then outputs statistics on the values without using any functions. I am currently in the process of modifying the code to include the functions prototyped below. I am a little bit confused as to how to go about defining the functions and calling them. I declared them above the main function and now need to define and call them to make the program execute the exact same tasks as before, only using functions instead. In particular I'm unsure of how to use the ReadData function as the number of values to be read and stored in the array is unknown. I would highly appreciate any input on this as I am completely stuck on how to move forward. Thank you!!
#include <iostream>
usingnamespace std;
//Prototyping functions
void ReadData(float list [], int& count);
float Variance(float list [], int count);
float StandardDev(float list [], int count);
void Sort(float list [], int count);
float SqRoot(float value);
void main()
{
float value, sum = 0, largestValue, smallestValue, average, median,
var = 0;
float list[100];
int count = 0;
char answer = 'y';
while (count <= 100)
{
cout << "Would you like to add a value? (Y or N)." << endl;
cin >> answer;
if (answer == 'y' || answer == 'Y')
{
cout << "Enter a value: ";
cin >> list[count];
sum += list[count];
++count;
}
if (answer == 'n' || answer == 'N')
break;
if (answer != 'y' && answer != 'n')
cout << "Invalid input. Please enter Y or N." << endl;
}
for (int i = (count - 1); i > 0; i--)
for (int j = 0; j < i; j++)
if (list[j] > list[j + 1])
{
list[j] = list[j] + list[j + 1];
list[j + 1] = list[j] - list[j + 1];
list[j] = list[j] - list[j + 1];
}
for (int i = 0; i < count; i++)
cout << list[i] << endl;
cout << endl;
average = sum / count;
smallestValue = list[0];
largestValue = list[count - 1];
if (count % 2 == 0)
{
median = (list[count / 2 - 1] + list[count / 2]) / 2;
}
else
{
median = list[count / 2];
}
for (int i = 0; i < count; i++)
{
var += (list[i] - average)*(list[i] - average) / count;
}
cout << "You entered " << count << " values." << endl;
cout << "The sum of the values is " << sum << endl;
cout << "The largest value entered is " << largestValue << endl;
cout << "The smallest value entered is " << smallestValue << endl;
cout << "The average of the array is " << average << endl;
cout << "The median of the array is " << median << endl;
cout << "The variance of the values is " << var << endl;
return 0;
}
Assuming that you mean "square root" from the function "SqRoot", you could make the function look something like this:
1 2 3
float SqRoot(float value) {
return sqrt(value);
}
I don't know what to tell you to put in ReadData() though. That is something you will have to ask your professor so you can make sure you write the function properly.