Advertisement
Zneeky

zad1Изпит

Dec 2nd, 2021
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace zad1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.  
  12.             // Console.Write("Write the directory of the wanted .txt file: ");
  13.             //  string txtFile="";
  14.             Console.Write("Write the directory of the wanted .txt file: ");
  15.             string input = Console.ReadLine();
  16.             string txt = File.ReadAllText($@"{input}");
  17.             int counter = 0;
  18.             foreach (var ch in txt.Split(","))
  19.             {
  20.                 counter++;
  21.             }
  22.  
  23.             int[] numarr = new int[counter];
  24.             ReadArrayFromFile(numarr,txt);
  25.             Console.WriteLine();
  26.             ReverseInPlace(numarr);
  27.             Console.WriteLine();
  28.             MostCommonCount(numarr);
  29.  
  30.            // string txtFile = File.ReadAllText($@"{input}");
  31.  
  32.  
  33.  
  34.  
  35.             //E:\Tekst.txt
  36.         }
  37.         public static void ReadArrayFromFile(int[] numarr, string txtFile)
  38.         {
  39.             int i = 0;
  40.             foreach (var ch in txtFile.Split(","))
  41.             {
  42.                 numarr[i] = int.Parse(ch);
  43.                 i++;
  44.             }
  45.             foreach (var num in numarr)
  46.             {
  47.                 Console.Write(num + " ");
  48.             }
  49.  
  50.         }    
  51.         public static void ReverseInPlace(int[] numarr)
  52.         {
  53.             for (int i = 0; i<numarr.Length/2; i++)
  54.             {
  55.                 int curr = numarr[i];
  56.                 numarr[i] = numarr[numarr.Length - 1 - i];
  57.                 numarr[numarr.Length - i - 1] = curr;
  58.             }
  59.             foreach (var item in numarr)
  60.             {
  61.                 Console.Write(item+" ");
  62.             }
  63.         }
  64.         public static void MostCommonCount (int[] numarr)
  65.         {
  66.             int commonNum=0;
  67.             int count = 0;
  68.             for (int i = 0; i < numarr.Length; i++)
  69.             {
  70.                 int currcount=0;
  71.                 int number = numarr[i];
  72.                 foreach (var num in numarr)
  73.                 {
  74.                     if (num == number)
  75.                     {
  76.                         currcount++;
  77.                     }
  78.                 }
  79.                 if (currcount > count)
  80.                 {
  81.                     count = currcount;
  82.                     commonNum = number;
  83.                 }
  84.  
  85.  
  86.             }
  87.             Console.WriteLine($"{commonNum} is contained {count} times!");
  88.         }
  89.  
  90.  
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement