### Outputting simple statistics based on an array

Hi!

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!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````#include using namespace 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; }``````
look at line 63 that is why it is always equal to 101.

Maybe you meant:

 `` `` ``for( int i = 0; i < countTotalValues; ++i )``
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:
 ``12345`` ``````//+= is same as value = value + ... //compound operator //http://www.cplusplus.com/doc/tutorial/operators/ sum += myarray[countTotalValues];``````

*edit
on line 55 also why are you dividing the median value by 2 at the very end?

The median should be the middle element. Not that value divided by 2.
Last edited on
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.
You've been most helpful, giblit! I truly appreciate it! :) Thanks for the last advice as well. Good point!
Topic archived. No new replies allowed.