C语言Matrix编程题——[Pointers and C-String]D. Liang 7.1 Analyzing input

[Pointers and C-String]D. Liang 7.1 Analyzing input

Description:

Use pointers on array to write a program that reads n numbers, computes their average, and finds out how many numbers are above the average.

Input:

The first line is a positive integer t for the number of test cases.
Each test case contains two lines. The first line is an integer n (0<n<=100). The second line contains n numbers.

Output:

For each test case, output how many numbers above the average in one line.

Sample Input:

Copy sample input to clipboard2 3 2 1.5 2.5 4 2 -6 2 6

Sample Output:

1 3

Programme:

//Date:2020/5/7
//Author:Kamenrider Justice
#include<stdio.h>
int main()
{
   int t,i;
   int compar(double* p,double ave,int size);//比较有几个比平均值大
   scanf("%d",&t);
   for(i=0;i<t;i++)
   {
      int n=0,j,count;
      double ave=0,sum=0;
      scanf("%d",&n);
      double num[n];
      for(j=0;j<n;j++)
      {
         scanf("%lf ",&num[j]);
         sum+=num[j];//求和
      }
      ave=sum/n;//求平均值
      count=compar(num,ave,n);//求出有几个比平均值大
      printf("%d\n",count);
   }
   return 0;
}
int compar(double* p,double ave,int size)
{
   int i,count=0;
   for(i=0;i<size;i++)
   {
      if(*(p+i)>ave)
      {
         count++;
      }
   }
   return count;
}

Java面试Offer直通车

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值