[URI Online Judge] – 1064 – Positives and Average

Read 6 values that can be floating point numbers. After, print how many of them were positive. In the next line, print the average of all positive values typed, with one digit after the decimal point.

Input

The input consist in 6 numbers that can be integer or point floating values. At least one number will be positive.

Output

The first output value is the amount of positive numbers. The next line should show the average of thepositive values ​typed.

Sample Input Sample Output
7
-5
6
-3.4
4.6
12
4 valores positivos
7.4

Solution:

#include <cstdio>
#include <cmath>
int main(){
 float n, s = 0;
 int c = 0;
 while(scanf("%f", &n) != EOF){
 if (n > 0){
 s += n;
 c++;
 }
 }
printf("%d valores positivos\n", c);
 printf("%.1f\n", (float) s / c);
 return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s