永发信息网

专家,请帮我解决下

答案:1  悬赏:70  手机版
解决时间 2021-07-29 15:08
  • 提问者网友:遮云壑
  • 2021-07-28 19:34

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

int* makeArray(int n, int xi, int xa)
{
int* x;
int k;

if( (x = (int*)malloc( n * sizeof(int) )) == NULL ) return NULL;
for( k = 0; k < n; k++ )
x[k] = xi + floor( rand() % (xa - xi) );
return x;
}

int getMin(int x[], int n)
{
int i;
int cmin = x[0];
for( i = 1; i < n; i++ )
{
if( cmin > x[i] ) cmin = x[i];
}
return cmin;
}

int main()
{
int n = -1, xMin = -1, xMax = -1;
int res;
int* xarr;

while( n < 1 )
{
printf("Please enter number of elements: ");
scanf("%d", &n);
}
while( xMin < 0 )
{
printf("\nPlease enter minimum value: ");
scanf("%d", &xMin);
}
while( xMax < xMin )
{
printf("\nPlease enter maximum value: ");
scanf("%d", &xMax);
}
if( (xarr = makeArray( n, xMin, xMax )) == NULL )
{
printf("\nError. Can't generate array!\n");
return 1;
}
res = getMin( xarr, n );
printf("\nMinimum value in array is: %d\n", res);
free( xarr );
return 0;
}
这个程序什么意思,每一步什么意思啊

最佳答案
  • 五星知识达人网友:举杯邀酒敬孤独
  • 2021-07-28 20:38
帮你注释上了
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//产生n个在xi-xa之间的随机数
int* makeArray(int n, int xi, int xa)
{
int* x;
int k;

if( (x = (int*)malloc( n * sizeof(int) )) == NULL ) return NULL;
for( k = 0; k < n; k++ )
x[k] = xi + floor( rand() % (xa - xi) );
return x;
}

//获得x[n]中的最小值
int getMin(int x[], int n)
{
int i;
int cmin = x[0];
for( i = 1; i < n; i++ )
{
if( cmin > x[i] ) cmin = x[i];
}
return cmin;
}

int main()
{
int n = -1, xMin = -1, xMax = -1;
int res;
int* xarr;

//输入元素个数n
while( n < 1 )
{
printf("Please enter number of elements: ");
scanf("%d", &n);
}
//输入元素下界
while( xMin < 0 )
{
printf("\nPlease enter minimum value: ");
scanf("%d", &xMin);
}
//输入元素上界
while( xMax < xMin )
{
printf("\nPlease enter maximum value: ");
scanf("%d", &xMax);
}
//创建n个在上界与下界之间的数字
if( (xarr = makeArray( n, xMin, xMax )) == NULL )
{
printf("\nError. Can't generate array!\n");
return 1;
}
//获得n个数之中的最小值并输出
res = getMin( xarr, n );
printf("\nMinimum value in array is: %d\n", res);
free( xarr );
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯