Advertisement
darighteous1

FunWithMatrices

Mar 15th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Lab_FunWithMatrices
  5. {
  6.     class Matrix
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             double startValue = double.Parse(Console.ReadLine());
  11.             double step = double.Parse(Console.ReadLine());
  12.             double[,] matrix = new double[4, 4];
  13.             double currentValue = startValue;
  14.  
  15.             //fill matrix
  16.             matrix[0, 0] = startValue;
  17.             for (int i = 0; i < 4; i++)
  18.             {
  19.                 for (int j = 0; j < 4; j++)
  20.                 {
  21.                     if (i == 0 && j == 0)
  22.                     {
  23.                         matrix[i, j] = currentValue;
  24.                     }
  25.                     else
  26.                     {
  27.                         matrix[i, j] = currentValue + step;
  28.                         currentValue = matrix[i, j];
  29.                     }
  30.                 }
  31.             }
  32.             string input = Console.ReadLine();
  33.  
  34.             //calculations
  35.             while (input != "Game Over!")
  36.             {
  37.                 int row = int.Parse(input.Split()[0]);
  38.                 int col = int.Parse(input.Split()[1]);
  39.                 double num = double.Parse(input.Split()[3]);
  40.  
  41.                 if (input.Split()[2] == "multiply")
  42.                 {
  43.                     matrix[row, col] *= num;
  44.                 }
  45.                 else if (input.Split()[2] == "sum")
  46.                 {
  47.                     matrix[row, col] += num;
  48.                 }
  49.                 else //power
  50.                 {
  51.                     matrix[row, col] = Math.Pow(matrix[row, col], num);
  52.                 }
  53.                 input = Console.ReadLine();
  54.             }
  55.  
  56.             //print matrix
  57.             //Console.WriteLine();
  58.             //for (int i = 0; i < 4; i++)
  59.             //{
  60.             //    for (int j = 0; j < 4; j++)
  61.             //    {
  62.             //        Console.Write("{0} ", matrix[i, j]);
  63.             //    }
  64.             //    Console.WriteLine();
  65.             //}
  66.  
  67.             ////calculate max sums
  68.             //double[] maxSums = { double.MinValue, double.MinValue, double.MinValue };
  69.  
  70.             //double leftDiagonalSum = matrix[0, 0] + matrix[1, 1] + matrix[2, 2] + matrix[3, 3];
  71.             //double rightDiagonalSum = matrix[0, 3] + matrix[1, 2] + matrix[2, 1] + matrix[3, 0];
  72.             //maxSums[2] = rightDiagonalSum > leftDiagonalSum ? rightDiagonalSum : leftDiagonalSum;
  73.             //string maxDiag = rightDiagonalSum > leftDiagonalSum ? "RIGHT-DIAGONAL" : "LEFT-DIAGONAL";
  74.  
  75.             //int maxRow = 0;
  76.             //int maxCol = 0;
  77.  
  78.             //for (int i = 0; i < 4; i++)
  79.             //{
  80.             //    double currentRowSum = 0;
  81.             //    double currentColSum = 0;
  82.  
  83.             //    for (int j = 0; j < 4; j++)
  84.             //    {
  85.             //        currentRowSum += matrix[i, j];
  86.             //        currentColSum += matrix[j, i];
  87.  
  88.             //        if (currentColSum > maxSums[1])
  89.             //        {
  90.             //            maxSums[1] = currentColSum;
  91.             //            maxCol = j;
  92.             //        }
  93.             //    }
  94.  
  95.             //    if (currentRowSum > maxSums[0])
  96.             //    {
  97.             //        maxSums[0] = currentRowSum;
  98.             //        maxRow = i;
  99.  
  100.             //    }
  101.             //}
  102.             double[] rowSumArr = new double[4];
  103.             rowSumArr[0] = matrix[0, 0] + matrix[0, 1] + matrix[0, 2] + matrix[0, 3];
  104.             rowSumArr[1] = matrix[1, 0] + matrix[1, 1] + matrix[1, 2] + matrix[1, 3];
  105.             rowSumArr[2] = matrix[2, 0] + matrix[2, 1] + matrix[2, 2] + matrix[2, 3];
  106.             rowSumArr[3] = matrix[3, 0] + matrix[3, 1] + matrix[3, 2] + matrix[3, 3];
  107.             double maxRowSum = rowSumArr.Max();
  108.             int maxRowIndex = Array.IndexOf(rowSumArr, maxRowSum);
  109.             string maxRow = "ROW[" + maxRowIndex+ "]";
  110.  
  111.             double[] colSumArr = new double[4];
  112.             colSumArr[0] = matrix[0, 0] + matrix[1, 0] + matrix[2, 0] + matrix[3, 0];
  113.             colSumArr[1] = matrix[0, 1] + matrix[1, 1] + matrix[2, 1] + matrix[3, 1];
  114.             colSumArr[2] = matrix[0, 2] + matrix[1, 2] + matrix[2, 2] + matrix[3, 2];
  115.             colSumArr[3] = matrix[0, 3] + matrix[1, 3] + matrix[2, 3] + matrix[3, 3];
  116.             double maxColSum = colSumArr.Max();
  117.             int maxColIndex = Array.IndexOf(colSumArr, maxColSum);
  118.             string maxCol = "COLUMN[" + maxColIndex + "]";
  119.  
  120.             double leftDiagonal = matrix[0, 0] + matrix[1, 1] + matrix[2, 2] + matrix[3, 3];
  121.             double rightDiagonal = matrix[0, 3] + matrix[1, 2] + matrix[2, 1] + matrix[3, 0];
  122.             double maxDiagSum = rightDiagonal > leftDiagonal ? rightDiagonal : leftDiagonal;
  123.             string maxDiag = rightDiagonal > leftDiagonal ? "RIGHT-DIAGONAL" : "LEFT-DIAGONAL";
  124.  
  125.             double[] maxSums = { maxRowSum, maxColSum, maxDiagSum };
  126.             double max = maxSums.Max();
  127.             int maxType = Array.IndexOf(maxSums, max);
  128.  
  129.             switch (maxType)
  130.             {
  131.                 case 0:
  132.                     Console.WriteLine("{0} = {1:F2}", maxRow, max);
  133.                     break;
  134.                 case 1:
  135.                     Console.WriteLine("{0} = {1:F2}", maxCol, max);
  136.                     break;
  137.                 case 2:
  138.                     Console.WriteLine("{0} = {1:F2}", maxDiag, max);
  139.                     break;
  140.             }
  141.             //print matrix
  142.             //Console.WriteLine();
  143.             //for (int i = 0; i < 4; i++)
  144.             //{
  145.             //    for (int j = 0; j < 4; j++)
  146.             //    {
  147.             //        Console.Write("{0} ", matrix[i, j]);
  148.             //    }
  149.             //    Console.WriteLine();
  150.             //}
  151.             ////output strings
  152.             //double maxValue = maxSums.Max();
  153.             //string max = string.Empty;
  154.  
  155.             //if (maxValue == maxSums[0])
  156.             //{
  157.             //    max = "ROW[" + maxRow + "]";
  158.             //}
  159.             //else if (maxValue == maxSums[1])
  160.             //{
  161.             //    max = "COLUMN[" + maxCol + "]";
  162.             //}
  163.             //else
  164.             //{
  165.             //    max = maxDiag;
  166.             //}
  167.             //Console.WriteLine("{0} = {1:F2}", max, maxSums.Max());
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement