15,790,243 members
See more:
`Write a C program to find the closest element to the average value of an array of some integers. If there exist more than one closest elements in the array, print the closest element which is present at the higher array-index. The array must be represented through a pointer (with DMA). In the program, define a separate function to find the closest array element.`

What I have tried:

```#include <stdio.h>
#include <stdlib.h>
int minelement(int *arr,int n,float avg){
int number=0;
float min,diff1,diff2,x,y;
int j=0;
for(;j<n;++j){
min=12384.000;
x=*(arr+j)-avg;
diff1=abs(x);
if(diff1<min){
min=diff1;

}
}
int m=0;
for(;m<n;++m){
y=*(arr+m)-avg;
diff2=abs(y);

if(diff2==min){
number=*(arr+m);
break;
}

}
return number;

}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(){
float avg;
int *arr,n;
int sum=0;
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
int i=0;
for(;i<n;i++){
int m;
scanf("%d",&m);
*(arr+i)=m;
sum+=m;
}
avg=((float)sum)/n;
int mn=minelement(arr,n,avg);
printf("%d",mn);

}```
Posted
Updated 24-May-21 6:49am
Patrice T 19-May-21 1:14am
And you have a question ?

## Solution 1

Well, a couple of things.
1) Your username. Thanks for that - is it really a good idea to be rude to people you want help from? I'm not saying you should try to suck up to them, but when you start a conversation by telling people to "get lost" and then want help I suspect you are less likely to get anything useful than less rude arrogant folks might.
Just a thought ...

2) Ho do you expect that `minelement` function to work given that you set the minimum value to the same number each time you go round the loop?

3) Where does your question even ask you to find a minimum value? Use relevant names, and read the question carefully - you don't even try to fulfill one of the conditions ...

4) To be honest, even for "beginner homework" that is some pretty poor code: it doesn't look like it was thought out at all before you jumped straight into coding.

## Solution 3

```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int closest_element(int *arr, float avg, int n);

int main(void)
{
int n, *arr, count = 0;
scanf("%d", &n);
arr = (int *)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
count += arr[i];
}
float avg = (float)count / n;
printf("%d", closest_element(arr, avg, n));

return 0;
}

int closest_element(int *arr, float avg, int n)
{
int value;
float min = 100000.0;

for (int i = 0; i < n; i++)
{
if (min == fabs((float)arr[i] - avg))
{
if (arr[i] > value)
{
value = arr[i];
}
}
if (min > fabs((float)arr[i] - avg))
{
min = fabs((float)arr[i] - avg);
value = arr[i];
}
}
return value;
}```

v3

## Solution 2

#include <stdio.h>
#include <stdlib.h>
int HigherArray(int *arr,int n,float avg){
int HVal=0;
int j=0;
for(;j<n;++j){
if(arr[j]="">=avg)
{
HVal=arr[j];
break;
}
}

return HVal;

}

int main(){
float avg;
int *arr,n;
int sum=0;
printf("Enter the numbers of elements: ");
scanf("%d",&n);
arr=(int*)malloc(n*sizeof(int));
int i=0;
for(i = 0; i < n; ++i){
int m;
scanf("%d",&m);
*(arr+i)=m;
sum+=m;

}
avg=((float)sum)/n;
printf("Average no is %f\n ",avg);
printf("Highest no is ");
int HA=HigherArray(arr,n,avg);
printf("%d",HA);

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Maciej Los 105 Graeme_Grant 80 Andre Oosthuizen 80 CPallini 70 Dave Kreskowiak 40
 Richard MacCutchan 1,490 Dave Kreskowiak 1,400 Maciej Los 840 Richard Deeming 763 CPallini 720

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900