dhows

zad 401

Feb 24th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             int n;
  6.  
  7.             do
  8.             {
  9.                 Console.Write("Enter n: ");
  10.                 n = int.Parse(Console.ReadLine());
  11.             } while (n > 31 || n <= 0);
  12.             Console.WriteLine();
  13.  
  14.             double[] A, B, C;
  15.             Console.WriteLine("For station A:\n");
  16.             A = enterValues(n);
  17.  
  18.             Console.WriteLine("For station B:\n");
  19.             B = enterValues(n);
  20.  
  21.             Console.WriteLine("For station C:\n");
  22.             C = enterValues(n);
  23.  
  24.             Console.WriteLine();
  25.  
  26.             //a
  27.             Console.Write("Average values for station A:\n{0:0.00} => ", findAvrgValueForMonth(A));
  28.             Console.WriteLine();
  29.  
  30.             //b
  31.             Console.Write("Average values for station B:\n{0:0.00} => ", findAvrgValueForMonth(B));
  32.             Console.WriteLine();
  33.  
  34.             //c
  35.             Console.Write("Average values for station C:\n{0:0.00} => ", findAvrgValueForMonth(C));
  36.             Console.WriteLine();
  37.         }
  38.  
  39.         static double findAvrgValueForMonth(double[] month)
  40.         {
  41.             double sum = 0;
  42.             int counter = 0;
  43.             for (int i = 0; i < month.Length; i++)
  44.             {
  45.                 if (month[i] > 0)
  46.                 {
  47.                     sum += month[i];
  48.                     counter++;
  49.                 }
  50.             }
  51.  
  52.             return sum/counter;
  53.         }
  54.  
  55.         static double[] enterValues(int n)
  56.         {
  57.             double[] days = new double[n];
  58.             for (int i = 0; i < n; i++)
  59.             {
  60.                 enterValue:
  61.                 Console.Write("Enter liters for m2 for day {0}: ", i + 1);
  62.                 days[i] = double.Parse(Console.ReadLine());
  63.                 if (days[i] < 0)
  64.                 {
  65.                     Console.WriteLine("You can't enter negative value!");
  66.                     goto enterValue;
  67.                 }
  68.             }
  69.  
  70.             return days;
  71.         }
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment