Advertisement
Levi0227

Minepit Lake

Oct 19th, 2022 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     class Program
  11.     {
  12.  
  13.         static int rows, columns;
  14.         static int[,] array = new int[99, 99];
  15.  
  16.         static int n = 2;
  17.         static void exn()
  18.         {
  19.             Console.WriteLine($"Exercise {n++}.");
  20.         }
  21.  
  22.         static void ex1()
  23.         {
  24.             // 1.Import and store the contents of file depth.txt
  25.             // and solve the following exercises using those data.
  26.             StreamReader sr = new StreamReader("depth.txt");
  27.             rows = Convert.ToInt16(sr.ReadLine());    // 1st line
  28.             columns = Convert.ToInt16(sr.ReadLine()); // 2nd line
  29.             array = new int[rows, columns];
  30.             for (int r = 0; r < rows; r++){           // rows
  31.                 var line = sr.ReadLine().Split();     // read a line (more values)
  32.                 for (int c = 0; c < columns; c++){    // columns
  33.                     array[r, c] = Convert.ToInt16(line[c]);
  34.                 }
  35.             }
  36.             sr.Close();
  37.         }
  38.  
  39.         static void ex2()
  40.         {
  41.             // 2.Request the identifier of a row and a column, then
  42.             // display the measurement data at that point on
  43.             // the screen.
  44.             // (Row and column numbering should start with 1.)
  45.             exn();
  46.             int R, C;
  47.             Console.Write("Row identifier of the measurement point = ");
  48.             R = Convert.ToInt16(Console.ReadLine());
  49.             Console.Write("Column identifier of the measurement point = ");
  50.             C = Convert.ToInt16(Console.ReadLine());
  51.             Console.WriteLine($"The measured depth at the given point is {array[R - 1, C - 1]} dm.");
  52.         }
  53.  
  54.         static void ex3()
  55.         {
  56.             // 3.Determine the surface area of the lake(that is, the grey area)
  57.             // and the average depth of the lake.
  58.             // Display the two results on the screen according to the example.
  59.             // The average depth of the lake should be displayed in metres with an
  60.             // accuracy of two decimal digits.
  61.             exn();
  62.             int count = 0;
  63.             double sum = 0;
  64.             for (int r = 0; r < rows; r++){ // rows
  65.                 for (int c = 0; c < columns; c++){ // columns
  66.                     if (array[r, c] > 0){   // water
  67.                         count++;            // count of water surface
  68.                         sum += array[r, c]; // sum of depths
  69.                     }
  70.                 }
  71.             }
  72.             Console.WriteLine($"The surface area of the lake is {count} m2, it's average depth is {sum / count / 10:F2} m.");
  73.         }
  74.  
  75.         static void ex4() {
  76.             /*4. What is the greatest depth of the lake and where is the deepest point? Display the answer
  77.             on the screen.Display the coordinates of the deepest point according to the example, in
  78.             (row; column) format.If there are several deepest points, then the coordinates of each point
  79.             should be displayed. */
  80.             exn();
  81.             int max = 0;
  82.             for (int r = 0; r < rows; r++){
  83.                 for (int c = 0; c < columns; c++){
  84.                     if (max < array[r, c]){
  85.                         max = array[r, c];
  86.                     }
  87.                 }
  88.             }
  89.             Console.WriteLine($"The greatest depth of the lake is {max} dm");
  90.             Console.Write("The row-column coordinates of the deepest point(s) are:\n");
  91.             for (int r = 0; r < rows; r++){
  92.                 for (int c = 0; c < columns; c++){
  93.                     if (max == array[r, c]){
  94.                         Console.Write($"({r+1}; {c+1})\t");
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.  
  100.         static void ex5() {
  101.             /*5. Find the length of the shore of the lake, that is, the length of the black line bordering the
  102.             grey area. Also include the perimeter of the islands in the lake into the length of the shore.
  103.             Display the result on the screen according to the example. (In the solution you can make
  104.             use of the fact that the data in the first and last rows and columns of the table are all 0.)*/
  105.             Console.WriteLine();
  106.             exn();
  107.             int shore = 0;
  108.             for (int r = 0; r < rows-1; r++){
  109.                 for (int c = 0; c < columns-1; c++){
  110.                     if (array[r, c] == 0 && array[r, c+1] != 0 || array[r, c] != 0 && array[r, c + 1] == 0){
  111.                         shore++;
  112.                     }
  113.                     if (array[r, c] == 0 && array[r+1, c] != 0 || array[r, c] != 0 && array[r + 1, c] == 0){
  114.                         shore++;
  115.                     }
  116.                 }
  117.             }
  118.             Console.WriteLine($"The length of the shore is {shore} m");
  119.         }
  120.  
  121.         static void ex6(){
  122.             /*6. Request the identifier of a column from the user and show the depth of the lake in the given
  123.             column in text file chart.txt using a “bar chart” in the following way. The beginning of
  124.             the line should contain the row identifier of the measurement point with exactly two digits,
  125.             then add as many asterisks (*) next to each other as the depth of the lake at the given point
  126.             in metres. Round the measurement data according to the rules of mathematics.
  127.              */
  128.             exn();
  129.             int C;
  130.             Console.Write("Column identifier of the measurement point = ");
  131.             C = Convert.ToInt16(Console.ReadLine());
  132.             StreamWriter sw = new StreamWriter("chart.txt");
  133.             for (int r = 0; r < rows; r++){
  134.                 sw.Write($"{r+1:D2} ");
  135.                 for (int i = 0; i < array[r, C - 1]; i++)
  136.                 {
  137.                     sw.Write("*");
  138.                 }
  139.                 sw.WriteLine();
  140.             }
  141.             sw.Close();
  142.         }
  143.  
  144.         static void Main(string[] args)
  145.         {
  146.             ex1();
  147.             //ex2();
  148.             ex3();
  149.             ex4();
  150.             ex5();
  151.             ex6();
  152.  
  153.             Console.ReadKey();
  154.         }
  155.     }
  156. }
  157.  
Tags: help
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement