Everything works well with this program except from the calculations of the sum, count (countTotalValues which always outputs 101), average and variance. Can anyone point me to any errors in the code in regards to these issues? Also, I'm not supposed to use a function for the variance, but I can't figure out how to calculate the variance in C++ after Googling it for a while. Any assistance is highly appreciated!
#include <iostream>
usingnamespace std;
int main()
{
float value, sum = 0, largestValue, smallestValue, average, median,
var = 0;
float myarray[100];
int countTotalValues = 0;
char answer = 'y';
while (countTotalValues <= 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 >> myarray[countTotalValues];
sum = sum + countTotalValues;
++countTotalValues;
}
if (answer == 'n' || answer == 'N')
break;
if (answer != 'y' && answer != 'n')
cout << "Invalid input. Please enter Y or N." << endl;
}
for (int i = (countTotalValues-1); i >= 0; i--)
for (int j = 0; j < i; j++)
if (myarray[j] > myarray[j + 1])
{
myarray[j] = myarray[j] + myarray[j + 1];
myarray[j + 1] = myarray[j] - myarray[j + 1];
myarray[j] = myarray[j] - myarray[j + 1];
}
for (int i = 0; i < countTotalValues; i++)
cout << myarray[i] << endl;
cout << endl;
average = sum / countTotalValues;
smallestValue = myarray[0];
largestValue = myarray[countTotalValues-1];
if (countTotalValues % 2 == 0)
{
median = (myarray[countTotalValues / 2 - 1] + myarray[countTotalValues / 2]) / 2;
}
else
{
median = myarray[countTotalValues / 2];
}
for (countTotalValues = 0; countTotalValues <= 100; countTotalValues++)
{
var += pow(myarray[100] - average, 2);
}
cout << "You entered " << countTotalValues << " 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;
}
You're right! Thanks so much for pointing that out! The count is now correct. I know that the average is incorrect as it's based on the sum which is incorrect. Any chance you can give me a pointer as to where the issue lies with the sum? It's probably a silly mistake, but C++ is brand new to me. I really appreciate it!
line 23 shouldn't you be adding the number you inputted to the total sum instead of the current number of elements that have been added to the array of values?
ex:
Thank you so much for your help! Everything runs smoothly at this point except from the variance. I found this online 'variance=sum/(n-1);'. Does this look correct to you? I'm have to calculate it without using a function. Again, thank you!!
To be honest I don't know what a variance and I guess I could google it but I would assume the function is correct. Just remember that if you want to return a float type you must do a float division and not integer division.