[Loops]D. Liang 4.2 Counting positive and negative number and computing the average of numbers.c

Description
Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros).
Your program ends with the input 0.
Display the average as a floating-point number with precision 2. (For example, if you entered 1, 2, and 0, the average should be 1.50.)
Input
Integers seperated by one blank, ends with 0.
Output
The count of positive number, the count of negative number, the total, and the average, each seperated by one blank. There is no space or a newline at the end.
Sample Input
1 2 0
Sample Output
2 0 3 1.50

***The combined usage of while and scanf:
在这里插入图片描述

common problem:

在这里插入图片描述This problem code as follow:

//  Date:2020/3/13
//  Author:xiezhg5 
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    long int a=0;
    long int b=0;
    long int c1=0;
    long int num;
    long int t=0;
    double v=0;
    while(scanf("%ld",&num),num) //当读入的num为0时跳出循环 
    {
        if(num>0)
        {
            a++;       //a表示正数个数 
            t=t+num;  //正数和 
            c1++;     //c1计总数变量 
        }
        if(num<0)
        {
            b++;     //b表示负数个数 
            t=t+num;  //负数和 
            c1++;    //c1计负数变量 
        }
        
    }
    //非常重要的一个条件
	//防止除数为零程序错误情况 
    if(c1==0)
    {
    v=0;
    printf("%ld %ld %ld %.2lf\n",a,b,t,v);
    }
    else
    {
    	v=(double)(t)/(a+b);
    	printf("%ld %ld %ld %.2lf\n",a,b,t,v);
	}
    return 0;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值