在主函数中定义一维数组double array10,从键盘上读入数组元素,自定义以下函数,输出数组 15
在主函数中定义一维数组doublearray10,从键盘上读入数组元素,自定义以下函数,输出数组所有元素,求平均值,寻找最大元素,查找某元素是否存在,排序,并在主函数中对...
在主函数中定义一维数组double array10,从键盘上读入数组元素,自定义以下函数,输出数组所有元素,求平均值,寻找最大元素,查找某元素是否存在,排序,并在主函数中对各函数进行调用和测试
展开
展开全部
# include <stdio.h>
# define LEN 10 //数组长度
//函数声明
void outArray(double *);
double avgArray(double *);
double findMax(double *);
int findDOM(double * , double );
void sortArray(double * , double );
int main(void)
{
double array[LEN];
short i;
double a;
short temp;
short flag1 = 1, flag2 = 1;
printf("请输入%d个数字:\n", LEN);
for (i=0; i<LEN; ++i)
scanf("%lf", &array[i]);
while ( flag1 )
{
printf("\n\n\t\t1. 求平均值\n\t\t2. 查找数组中最大元素\n\t\t3. 查找数组中的某个元素\n\t\t4. 排序\n\t\t5. 输出数组中的所有元素\t\t9999. 退出程序\n\t\t->");
scanf("%d", &temp);
switch ( temp )
{
case 9999:
flag1 = 0;
break;
case 1:
printf("\t数组的平均值为:%lf\n", avgArray(array));
break;
case 2:
printf("\t数组中最大的元素为:%lf\n", findMax(array));
break;
case 3:
flag2 = 1;
while ( flag2 )
{
printf("\t999999. 返回上一级\n\t请输入要查找的数:\t->");
scanf("%lf", &a);
if ( findDOM(array,a) )
printf("数组中存在%lf这个元素\n", a);
else if (a == 999999)
flag2 = 0;
else
printf("数组中不存在%lf这个元素\n", a);
}
break;
case 4:
flag2 = 1;
while ( flag2 )
{
printf("\t1. 升序\t2. 降序\t999999. 返回上一级\n\t请选择:->");
scanf("%lf", &a);
if (a==1||a==2)
{
sortArray(array, a);
printf("数组排序后为:\n");
outArray( array );
}else if (a == 999999)
flag2 = 0;
else
printf("请输入正确的选项\n");
}
break;
case 5:
printf("数组中的元素为:\n");
outArray( array );
break;
default:
printf("\n\t请输入正确的选项\n");
}
}
return 0;
}
void outArray(double * pArray)
{
short i;
for (i=0; i<LEN; ++i)
printf("%lf ", pArray[i]);
printf("\n");
return;
}
double avgArray(double * pArray)
{
short i;
double s=0;
for(i=0; i<LEN; ++i)
s += pArray[i];
s = s/LEN;
return s;
}
double findMax(double *pArray)
{
double a = pArray[0];
short i;
for (i=1; i<LEN; ++i)
if (a < pArray[i])
a = pArray[i];
return a;
}
int findDOM(double * pArray, double a)
{
short i;
for (i=0; i<LEN; ++i)
if (a == pArray[i])
return 1;
return 0;
}
void sortArray(double * pArray, double a)
{
short i,j;
double t;
if (a == 1.0)
{
for (i=0; i<LEN-1; ++i)
for (j=0; j<LEN-1-i; ++j)
if (pArray[j] > pArray[j+1])
{
t = pArray[j];
pArray[j] = pArray[j+1];
pArray[j+1] = t;
}
}
else if (a == 2.0)
{
for (i=0; i<LEN-1; ++i)
for (j=0; j<LEN-1-i; ++j)
if (pArray[j] < pArray[j+1])
{
t = pArray[j];
pArray[j] = pArray[j+1];
pArray[j+1] = t;
}
}
return;
}
# define LEN 10 //数组长度
//函数声明
void outArray(double *);
double avgArray(double *);
double findMax(double *);
int findDOM(double * , double );
void sortArray(double * , double );
int main(void)
{
double array[LEN];
short i;
double a;
short temp;
short flag1 = 1, flag2 = 1;
printf("请输入%d个数字:\n", LEN);
for (i=0; i<LEN; ++i)
scanf("%lf", &array[i]);
while ( flag1 )
{
printf("\n\n\t\t1. 求平均值\n\t\t2. 查找数组中最大元素\n\t\t3. 查找数组中的某个元素\n\t\t4. 排序\n\t\t5. 输出数组中的所有元素\t\t9999. 退出程序\n\t\t->");
scanf("%d", &temp);
switch ( temp )
{
case 9999:
flag1 = 0;
break;
case 1:
printf("\t数组的平均值为:%lf\n", avgArray(array));
break;
case 2:
printf("\t数组中最大的元素为:%lf\n", findMax(array));
break;
case 3:
flag2 = 1;
while ( flag2 )
{
printf("\t999999. 返回上一级\n\t请输入要查找的数:\t->");
scanf("%lf", &a);
if ( findDOM(array,a) )
printf("数组中存在%lf这个元素\n", a);
else if (a == 999999)
flag2 = 0;
else
printf("数组中不存在%lf这个元素\n", a);
}
break;
case 4:
flag2 = 1;
while ( flag2 )
{
printf("\t1. 升序\t2. 降序\t999999. 返回上一级\n\t请选择:->");
scanf("%lf", &a);
if (a==1||a==2)
{
sortArray(array, a);
printf("数组排序后为:\n");
outArray( array );
}else if (a == 999999)
flag2 = 0;
else
printf("请输入正确的选项\n");
}
break;
case 5:
printf("数组中的元素为:\n");
outArray( array );
break;
default:
printf("\n\t请输入正确的选项\n");
}
}
return 0;
}
void outArray(double * pArray)
{
short i;
for (i=0; i<LEN; ++i)
printf("%lf ", pArray[i]);
printf("\n");
return;
}
double avgArray(double * pArray)
{
short i;
double s=0;
for(i=0; i<LEN; ++i)
s += pArray[i];
s = s/LEN;
return s;
}
double findMax(double *pArray)
{
double a = pArray[0];
short i;
for (i=1; i<LEN; ++i)
if (a < pArray[i])
a = pArray[i];
return a;
}
int findDOM(double * pArray, double a)
{
short i;
for (i=0; i<LEN; ++i)
if (a == pArray[i])
return 1;
return 0;
}
void sortArray(double * pArray, double a)
{
short i,j;
double t;
if (a == 1.0)
{
for (i=0; i<LEN-1; ++i)
for (j=0; j<LEN-1-i; ++j)
if (pArray[j] > pArray[j+1])
{
t = pArray[j];
pArray[j] = pArray[j+1];
pArray[j+1] = t;
}
}
else if (a == 2.0)
{
for (i=0; i<LEN-1; ++i)
for (j=0; j<LEN-1-i; ++j)
if (pArray[j] < pArray[j+1])
{
t = pArray[j];
pArray[j] = pArray[j+1];
pArray[j+1] = t;
}
}
return;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询