c语言怎么能让我输出的数据5个一换行
#include<stdio.h>voidsort(doublearr[],intn);doubleaverage(doublearr[],intn);main(){in...
#include <stdio.h>
void sort(double arr[],int n);
double average(double arr[], int n);
main()
{ int n;
double arr[]={0};
printf("Please enter at least 3 and up to 30 positive values\n");
printf("*You have to enter the number of the values first before you enter the value.*\n");
printf("The number of the value is :");
scanf("%d",&n);
if(n>=3)
printf("Please enter the values:");
else
{
printf("You have to enter at least 3 values. Thank you!\n");
printf("The number of the value is :");
scanf("%d",&n);
printf("Please enter the values:");
}
sort(arr,n);
getch();
}
void sort(double arr[],int n)
{ int j,i,t;
if(n>=3)
for(i=0;i<n;i++)
scanf("%lf",&arr[i]);
for(j=0;j<n-1;j++)
for(i=0;i<n-j;i++)
if(arr[i]<arr[i+1])
{
t=arr[i];
arr[i]=arr[i+1];
arr[i+1]=t;
}
printf("the sorted values:\n");
for(i=0;i<n;i++)
printf("%.1lf",arr[i]);
}
另外利用double average(double arr[],int n)找出平均值呢?最大值呢? 展开
void sort(double arr[],int n);
double average(double arr[], int n);
main()
{ int n;
double arr[]={0};
printf("Please enter at least 3 and up to 30 positive values\n");
printf("*You have to enter the number of the values first before you enter the value.*\n");
printf("The number of the value is :");
scanf("%d",&n);
if(n>=3)
printf("Please enter the values:");
else
{
printf("You have to enter at least 3 values. Thank you!\n");
printf("The number of the value is :");
scanf("%d",&n);
printf("Please enter the values:");
}
sort(arr,n);
getch();
}
void sort(double arr[],int n)
{ int j,i,t;
if(n>=3)
for(i=0;i<n;i++)
scanf("%lf",&arr[i]);
for(j=0;j<n-1;j++)
for(i=0;i<n-j;i++)
if(arr[i]<arr[i+1])
{
t=arr[i];
arr[i]=arr[i+1];
arr[i+1]=t;
}
printf("the sorted values:\n");
for(i=0;i<n;i++)
printf("%.1lf",arr[i]);
}
另外利用double average(double arr[],int n)找出平均值呢?最大值呢? 展开
3个回答
展开全部
for(i=0;i<n;i++){
printf("%.1lf",arr[i]);
if(i%5==4) printf("\n");
}
因为这里是从0开始,0到4为一行,5到9为一行,以此类推。
double average(double arr[],int n){
double sum=0;
int j;
for(j=0;j<n;j++)
sum+=arr[j];
return sum/n;
}
double max(double arr[],int n){
double m=arr[0];
int j;
for(j=1;j<n;j++)
if(arr[j]>m) m=arr[j];
return m;
}
printf("%.1lf",arr[i]);
if(i%5==4) printf("\n");
}
因为这里是从0开始,0到4为一行,5到9为一行,以此类推。
double average(double arr[],int n){
double sum=0;
int j;
for(j=0;j<n;j++)
sum+=arr[j];
return sum/n;
}
double max(double arr[],int n){
double m=arr[0];
int j;
for(j=1;j<n;j++)
if(arr[j]>m) m=arr[j];
return m;
}
展开全部
定义一个记录个数的变量
例如
int count=0;
循环每走一次就执行一次
count++;
判断条件为
if(count%5==0)
printf("\n");
这样既可
例如
int count=0;
循环每走一次就执行一次
count++;
判断条件为
if(count%5==0)
printf("\n");
这样既可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(i%5 == 0)
printf("\n");
这函数是求平均值的,不求最大,要求最大随便写个函数就行了,那多简单,学会自己动手!
printf("\n");
这函数是求平均值的,不求最大,要求最大随便写个函数就行了,那多简单,学会自己动手!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询