I am performing the following exercise:
Afterafewattempts,Icameupwiththefollowingfinalcode:
intproximo_da_media(int*vec,intdim){inti,*pos;floatmedia,soma=0,diferenca;pos=vec;//inicializarponteirofor(i=0;i<dim;i++)soma+=*(vec+i);//fazasomadetodososnumeorsmedia=0.5+(soma/dim);//fazamediaesoma0.5,comoaseguirfazsubtraçãocomumintseamediativerapartedecimaligualousuperiora0.5arredondaparacimasenãomantemointoriginaldiferenca=abs((*vec)-media);//calculaadiferençaentreamediaeoprimeirovalordovetoremvalorabsolutofor(i=0;i<dim;i++){if(abs(*(vec+i)-media)<diferenca){//seovalorabsolutoentreamediaevec[i]formenorqueadiferencaanteriorentãofoidescobertoumnumeromaisproximodamedialogoatualizoadiferencaeoponteiroparaonumeroencontradodiferenca=abs(*(vec+i)-media);pos=vec+i;}}returnpos;}intmain(){inttabela[10]={20,30,43,5,400,1999,9,360,3,8},*posicao;posicao=proximo_da_media(tabela,10);printf("O endreco do ponteiro que aponta para o numero mais proximo da media e o %p e tem o valor %d\n",posicao,*posicao);}
With the vector inserted in the code the sum of all the numbers is 2877, doing the average (2877/10) we got the result of 287.7 that rounds to 288, 360 being the closest number of the average. p>
I made some tests and the program has run as expected, for this example also went well, I got the following output:
WhilerunningasexpectedIhave2warningsandI'mnotsureifIshouldchangeanythinginthecode,thewarningsareasfollows:
I would like to know if the algorithm is correct and should I change something in the code.