Advertisement
kyamaliev

PHP arrays homework

Jun 20th, 2022
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2.  
  3. namespace misc
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             // average(arithmetic mean, средно аритметично)
  10.             int[] arr = { 3,4,5,6,7,8,9};
  11.             int sum = 0;
  12.             for (int i = 0; i < arr.Length; i++)
  13.             {
  14.                 sum += arr[i];
  15.             }
  16.  
  17.             int average = sum / arr.Length;
  18.             string arrToString = string.Join(' ', arr);
  19.             Console.WriteLine($"average of [{arrToString}] is: {average:0.00}"); //:0.00 forces the output to be printed with 2 decimals.
  20.  
  21.             // geometric mean (средно геометрично)
  22.            
  23.  
  24.             int product = 1;
  25.             for (int i = 0; i < arr.Length; i++)
  26.             {
  27.                 product *= arr[i];
  28.             }
  29.  
  30.             double geometricMean = Math.Pow((double)product, (1/(double)arr.Length)); //(double) is for casting to double since Math.Pow works with floats and doubles and not ints.
  31.             // 1/power == nth-root
  32.             Console.WriteLine($"geometric mean of [{arrToString}] is: {geometricMean:0.00}");
  33.  
  34.             //root mean square - средноквадратично (wiki) - домашното по PHP
  35.             int sumOfSquares = 0;
  36.             for (int i = 0; i < arr.Length; i++)
  37.             {
  38.                 sumOfSquares += arr[i] * arr[i];
  39.             }
  40.  
  41.             //homework assignment states sqrt(sum num^2)
  42.             double rootMeanSquare = Math.Sqrt(sumOfSquares) / arr.Length; // since math.sqrt returns a double, , '/' is considered a floating point division and not integer division (5.0/2=2.5)
  43.             Console.WriteLine($"geometric mean of [{arrToString}] is: {rootMeanSquare:0.00}");
  44.  
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement