### closest to the average

I wrote a program to get 10 numbers then find their average and then to cout the number that is closest to the average. for some reason when I entered ( 1.1 8.76 9.23 7.5 98.4 45 12.9 45.2 50 2.4) it gives out 28.049 which is the average but how is that possible what did I do wrong?

 ``12345678910111213141516171819202122232425262728293031`` `````` #include using namespace std; int main (){ int i,j; float y,sum=0,min,x1,y1,x[10]; for(i=0;i<10;i++){ cin>>x[i]; sum=sum+x[i]; } y=sum/10; for(i=0;i<10;i++){ x1=y-x[i]; y1=y-x[i+1]; if(y-x[i]<0){ x1=x[i]-y; } if(y-x[i+1]<0){ y1=x[i+1]-y; } if(x1>y1){ min=x[i+1]; } else if(x1
 what did I do wrong

IMO, the first thing you did wrong was to use meaningless variable names. Using a bunch of single letter variable names, with the added bonus of having a variable that only differs by a numeric suffix is going to make following your logic difficult if not impossible.

The next thing is that you seem to have forgotten that there is such a thing as a space character. Again writing all of your formulas without any spaces makes reading the program more difficult.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include using namespace std; int main() { int i, j; float y, sum = 0, min, x1, y1; // Added this to avoid the typing, remove when progrem is working. float x[10] {1.1, 8.76, 9.23, 7.5, 98.4, 45, 12.9, 45.2, 50, 2.4}; for(i = 0; i < 10; i++) { //cin>>x[i]; sum = sum + x[i]; } y = sum / 10; for(i = 0; i < 10; i++) { x1 = y - x[i]; y1 = y - x[i + 1]; if(y - x[i] < 0) { x1 = x[i] - y; } if(y - x[i + 1] < 0) { y1 = x[i + 1] - y; } if(x1 > y1) { min = x[i + 1]; } else if(x1 < y1) { min = x[i]; } } cout << min; return 0; }``````