Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApp1
- {
- class Program
- {
- static int rows, columns;
- static int[,] array = new int[99, 99];
- static int n = 2;
- static void exn()
- {
- Console.WriteLine($"Exercise {n++}.");
- }
- static void ex1()
- {
- // 1.Import and store the contents of file depth.txt
- // and solve the following exercises using those data.
- StreamReader sr = new StreamReader("depth.txt");
- rows = Convert.ToInt16(sr.ReadLine()); // 1st line
- columns = Convert.ToInt16(sr.ReadLine()); // 2nd line
- array = new int[rows, columns];
- for (int r = 0; r < rows; r++){ // rows
- var line = sr.ReadLine().Split(); // read a line (more values)
- for (int c = 0; c < columns; c++){ // columns
- array[r, c] = Convert.ToInt16(line[c]);
- }
- }
- sr.Close();
- }
- static void ex2()
- {
- // 2.Request the identifier of a row and a column, then
- // display the measurement data at that point on
- // the screen.
- // (Row and column numbering should start with 1.)
- exn();
- int R, C;
- Console.Write("Row identifier of the measurement point = ");
- R = Convert.ToInt16(Console.ReadLine());
- Console.Write("Column identifier of the measurement point = ");
- C = Convert.ToInt16(Console.ReadLine());
- Console.WriteLine($"The measured depth at the given point is {array[R - 1, C - 1]} dm.");
- }
- static void ex3()
- {
- // 3.Determine the surface area of the lake(that is, the grey area)
- // and the average depth of the lake.
- // Display the two results on the screen according to the example.
- // The average depth of the lake should be displayed in metres with an
- // accuracy of two decimal digits.
- exn();
- int count = 0;
- double sum = 0;
- for (int r = 0; r < rows; r++){ // rows
- for (int c = 0; c < columns; c++){ // columns
- if (array[r, c] > 0){ // water
- count++; // count of water surface
- sum += array[r, c]; // sum of depths
- }
- }
- }
- Console.WriteLine($"The surface area of the lake is {count} m2, it's average depth is {sum / count / 10:F2} m.");
- }
- static void ex4() {
- /*4. What is the greatest depth of the lake and where is the deepest point? Display the answer
- on the screen.Display the coordinates of the deepest point according to the example, in
- (row; column) format.If there are several deepest points, then the coordinates of each point
- should be displayed. */
- exn();
- int max = 0;
- for (int r = 0; r < rows; r++){
- for (int c = 0; c < columns; c++){
- if (max < array[r, c]){
- max = array[r, c];
- }
- }
- }
- Console.WriteLine($"The greatest depth of the lake is {max} dm");
- Console.Write("The row-column coordinates of the deepest point(s) are:\n");
- for (int r = 0; r < rows; r++){
- for (int c = 0; c < columns; c++){
- if (max == array[r, c]){
- Console.Write($"({r+1}; {c+1})\t");
- }
- }
- }
- }
- static void ex5() {
- /*5. Find the length of the shore of the lake, that is, the length of the black line bordering the
- grey area. Also include the perimeter of the islands in the lake into the length of the shore.
- Display the result on the screen according to the example. (In the solution you can make
- use of the fact that the data in the first and last rows and columns of the table are all 0.)*/
- Console.WriteLine();
- exn();
- int shore = 0;
- for (int r = 0; r < rows-1; r++){
- for (int c = 0; c < columns-1; c++){
- if (array[r, c] == 0 && array[r, c+1] != 0 || array[r, c] != 0 && array[r, c + 1] == 0){
- shore++;
- }
- if (array[r, c] == 0 && array[r+1, c] != 0 || array[r, c] != 0 && array[r + 1, c] == 0){
- shore++;
- }
- }
- }
- Console.WriteLine($"The length of the shore is {shore} m");
- }
- static void ex6(){
- /*6. Request the identifier of a column from the user and show the depth of the lake in the given
- column in text file chart.txt using a “bar chart” in the following way. The beginning of
- the line should contain the row identifier of the measurement point with exactly two digits,
- then add as many asterisks (*) next to each other as the depth of the lake at the given point
- in metres. Round the measurement data according to the rules of mathematics.
- */
- exn();
- int C;
- Console.Write("Column identifier of the measurement point = ");
- C = Convert.ToInt16(Console.ReadLine());
- StreamWriter sw = new StreamWriter("chart.txt");
- for (int r = 0; r < rows; r++){
- sw.Write($"{r+1:D2} ");
- for (int i = 0; i < array[r, C - 1]; i++)
- {
- sw.Write("*");
- }
- sw.WriteLine();
- }
- sw.Close();
- }
- static void Main(string[] args)
- {
- ex1();
- //ex2();
- ex3();
- ex4();
- ex5();
- ex6();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement