帮忙写个电脑程序

来源:百度知道 编辑:UC知道 时间:2024/05/23 01:23:51
a) Write a function generating N random integer elements of an array which are more than xMin and less than xMax.
b) Write a function finding element in array which is the closest to the average value of array elements.
c) Write main function defining N, xMin and xMax, calling functions from a) and b) and printing result of calculation from b).

// Program below had beed compled & tested by Turbo c++ 3.0

#include "stdio.h"
#include "stdlib.h"
#include "_null.h"

void generate(unsigned int n,int xmx,int xmn,int *p)
{
int a,b,i,j;

for (b=0;b<n;b++)
{

for(i=0;i<5000;i++)
for(j=0;j<10000;j++) ;
// here is a delay to generate different numbers
// without this, all elements may be same.
// delay time depend on cpu frequency
srand(time(NULL));
a=rand();
*(p+b)=a%((xmx-xmn-1))+xmn+1; // range from min+1 to max-1;
printf("%d ",*(p+b));
if((b%6==0)&&(b!=0)) printf("\n");
}
}
int closetoav(int *p,unsigned int n)
{
long sum=0;
int differ;
int close_numb=0;
float average;
int b=0;
char close_flag=0;
for(b=0;b<n;b++)
sum+=*(p+b);
average=sum/n;
differ=abs(av