Advertisement
stanevplamen

02.7.5.MatrixWriteAndRead

Jun 7th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. class MatrixWriteAndRead
  5. {
  6.     /// <summary>
  7.     /// First enter the matrix size: N
  8.     /// On the next N rows enter N numbers separated with spaces
  9.     /// The program writes a text file with the matrix and calculates the max platform 2/2
  10.     /// </summary>
  11.     static void Main()
  12.     {
  13.         // 1. read the size of the matrix
  14.         Console.Write("Please enter the size of the square matrix [n,n], n = ");
  15.         int n = int.Parse(Console.ReadLine());
  16.  
  17.         // 2. making the matrix
  18.         int[,] matrix = LoadMatrix(n);
  19.  
  20.         // 3. Writing the matrix on the txt file
  21.         PrintMatrixToFile(matrix);
  22.  
  23.         // 4. Reading the matrix from the txt file
  24.         int[,] matrixFromFile = MatrixFromText(n);
  25.  
  26.         // 5. Max platform 2/2 sum calculation
  27.         int maxSum = MaxSumCalc(matrixFromFile);
  28.  
  29.         // 6. Writing max sum to a txt file
  30.         IntegerWriting(maxSum);
  31.     }
  32.  
  33.     // 2
  34.     static int[,] LoadMatrix(int n)
  35.     {
  36.         int[,] matrix = new int[n, n];
  37.  
  38.         for (int row = 0; row < n; row++)
  39.         {
  40.             Console.Write("Please enter the {0} row ({1} numbers separated with spaces):", row, n);
  41.             string[] strNumbers = Console.ReadLine().Split();
  42.             for (int col = 0; col < strNumbers.Length; col++)
  43.             {
  44.                 matrix[row, col] = int.Parse(strNumbers[col]); ;
  45.             }
  46.         }
  47.         return matrix;
  48.     }
  49.  
  50.     // 3
  51.     static void PrintMatrixToFile(int[,] matrix)
  52.     {
  53.         string fileNameMatrix = @"..\..\InputMatrix.txt";
  54.         StreamWriter streamWriterOne = new StreamWriter(fileNameMatrix);
  55.         using (streamWriterOne)
  56.         {
  57.             for (int row = 0; row < matrix.GetLength(0); row++)
  58.             {
  59.                 for (int col = 0; col < matrix.GetLength(1); col++)
  60.                 {
  61.                     streamWriterOne.Write("{0} ", matrix[row, col]);
  62.                 }
  63.                 streamWriterOne.WriteLine();
  64.             }
  65.         }
  66.         Console.WriteLine("The matrix is loaded");
  67.     }
  68.  
  69.     // 4
  70.     static int[,] MatrixFromText(int matrixLenght)
  71.     {
  72.         int[,] matrixFromFile = new int[matrixLenght, matrixLenght];
  73.         string fileToOpen = @"..\..\InputMatrix.txt";
  74.         StreamReader streamReaderOne = new StreamReader(fileToOpen);
  75.  
  76.         using (streamReaderOne)
  77.         {
  78.             string currentLine = streamReaderOne.ReadLine();
  79.  
  80.             for (int row = 0; row < matrixLenght; row++)
  81.             {
  82.                 string[] strNumbers = currentLine.Split();
  83.                 for (int col = 0; col < matrixLenght; col++)
  84.                 {
  85.                     matrixFromFile[row, col] = int.Parse(strNumbers[col]); ;
  86.                 }
  87.                 currentLine = streamReaderOne.ReadLine();
  88.             }
  89.         }
  90.  
  91.         return matrixFromFile;
  92.     }
  93.  
  94.     // 5
  95.     static int MaxSumCalc(int[,] matrix)
  96.     {
  97.         int maxSum = int.MinValue;
  98.  
  99.         for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  100.         {
  101.             for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  102.             {
  103.                 int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row + 1, col] + matrix[row + 1, col + 1];
  104.                 if (maxSum < sum)
  105.                 {
  106.                     maxSum = sum;
  107.                 }
  108.             }
  109.         }
  110.         return maxSum;
  111.     }
  112.  
  113.     // 6
  114.     static void IntegerWriting(int maxSum)
  115.     {
  116.         string fileToWrite = @"..\..\ResultMaxSum.txt";
  117.         StreamWriter streamWriterOne = new StreamWriter(fileToWrite);
  118.  
  119.         using (streamWriterOne)
  120.         {
  121.             streamWriterOne.WriteLine(maxSum);
  122.         }
  123.         Console.WriteLine("Result loaded");
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement