910. The arithmetic mean of positive

 

The sequence of real numbers is given. Find the arithmetic mean of positive numbers.

 

Input. The first line contains amount n (0 < n ≤ 100) of real numbers. Next line contains n real numbers, each of them is not greater than 100 by absolute value.

 

Output. Print the arithmetic mean of positive numbers with 2 digits after decimal point. If the positive numbers do not appear in the sequence, print Not Found (without quotes).

 

Sample input 1

Sample output 1

3

5.2 -2 4

4.60

 

 

Sample input 2

Sample output 2

3

-5.2 -2 -4

Not Found

 

 

 

SOLUTION

loops

 

Algorithm analysis

Ñount the sum of positive numbers in the variable s, and their amount in the variable cnt. The answer is s / cnt.

 

Algorithm realization

Read the value of n.

 

s = cnt = 0;

scanf("%d",&n);

 

Read n input numbers.

 

for(i = 0; i < n; i++)

{

  scanf("%lf",&val);

 

If the number val is positive, then add it to the sum s and increase the number of positive numbers cnt by 1.

 

  if (val > 0)

  {

    s += val;

    cnt++;

  }

}

 

Depending on the amount of positive numbers cnt, print the answer.

 

if (cnt == 0) printf("Not Found\n");

else printf("%.2lf\n",s / cnt);

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    double s = 0;

    int cnt = 0;

    for(int i = 0; i < n; i++)

    {

      double val = con.nextDouble();

      if (val > 0)

      {

        s += val;

        cnt++;

      }

    }

    if (cnt == 0) System.out.println("Not Found");

    else

      System.out.printf("%.2f\n", s / cnt);

    con.close();

  }

}

 

Python realization

 

n = int(input())

lst = list(map(float, input().split()))

 

l = []

for i in range(n):

  if lst[i] > 0: l.append(lst[i])

 

if len(l) > 0:

  average = float(sum(l) / len(l))

  print('%.2f' % average)

else:

  print('Not Found')