slemiba

L04-04 Térkép

Sep 28th, 2022
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2.  
  3. namespace sztf1labor04
  4. {
  5.     class Program
  6.     {
  7.        
  8.         static int[,] GenMap(int n)
  9.         {
  10.             int[,] temp = new int[n, n];
  11.             Random rnd = new Random();
  12.  
  13.             for (int j = 0; j < n; j++)
  14.             {
  15.                 for (int i = 0; i < n; i++)
  16.                 {
  17.                     temp[j, i] = rnd.Next(0, 1001);
  18.                 }
  19.             }
  20.  
  21.             return temp;
  22.  
  23.  
  24.         }
  25.         static void KiMap(ref int[,] t)
  26.         {
  27.             for (int j = 0; j < t.GetLength(0); j++)
  28.             {
  29.                 for (int i = 0; i < t.GetLength(1); i++)
  30.                 {
  31.                     Console.Write(t[j, i] + " ");
  32.                 }
  33.                 Console.WriteLine();
  34.             }
  35.  
  36.  
  37.         }
  38.         static double AVG(ref int[,] t)
  39.         {
  40.             int sum = 0;
  41.             int db = 0;
  42.             for (int i = 0; i < t.GetLength(0); i++)
  43.             {
  44.                 for (int j = 0; j < t.GetLength(1); j++)
  45.                 {
  46.                     db++;
  47.                     sum += t[i, j];
  48.                 }
  49.             }
  50.             return (double)sum / (double)db;
  51.         }
  52.  
  53.         static int NumAboveAVG(ref int[,] t)
  54.         {
  55.             double avg = AVG(ref t);
  56.             int db = 0;
  57.             for (int i = 0; i < t.GetLength(0); i++)
  58.             {
  59.                 for (int j = 0; j < t.GetLength(1); j++)
  60.                 {
  61.                     if ((double)t[i, j] < avg)
  62.                     {
  63.                         db++;
  64.                     }
  65.                 }
  66.             }
  67.             return db;
  68.         }
  69.         static void Main(string[] args)
  70.         {
  71.             int[,] Térkép = GenMap(3);
  72.             KiMap(ref Térkép);
  73.             Console.WriteLine(AVG(ref Térkép));
  74.             Console.WriteLine(NumAboveAVG(ref Térkép));
  75.         }
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment