Created
March 13, 2016 08:38
-
-
Save Kiso-blg/8c853cc43734a5bebbaa to your computer and use it in GitHub Desktop.
C# Basics December 2014 Lab - 02.Fun_with_Matrices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Globalization; | |
namespace _02.Fun_with_Matrices | |
{ | |
class FunWithMatrices | |
{ | |
static void Main(string[] args) | |
{ | |
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; | |
double[,] field = new double[4, 4]; | |
double startNumber = double.Parse(Console.ReadLine()); | |
double step = double.Parse(Console.ReadLine()); | |
CreateMatrix(field, startNumber, step); | |
string command = Console.ReadLine(); | |
OperateCommand(field, command); | |
FindLargestSum(field); | |
} | |
static double[,] CreateMatrix(double[,] matrix, double start, double step) | |
{ | |
for (int row = 0; row < matrix.GetLength(0); row++) | |
{ | |
for (int col = 0; col < matrix.GetLength(1); col++) | |
{ | |
matrix[row, col] = start; | |
start += step; | |
} | |
} | |
return matrix; | |
} | |
static double[,] OperateCommand(double[,] matrix, string order) | |
{ | |
while (order != "Game Over!") | |
{ | |
string[] parameters = order.Split(' '); | |
int row = int.Parse(parameters[0]); | |
int col = int.Parse(parameters[1]); | |
double argument = double.Parse(parameters[3]); | |
if (parameters[2] == "multiply") | |
{ | |
matrix[row, col] *= argument; | |
} | |
else if (parameters[2] == "sum") | |
{ | |
matrix[row, col] += argument; | |
} | |
else if (parameters[2] == "power") | |
{ | |
matrix[row, col] = Math.Pow(matrix[row, col], argument); | |
} | |
else | |
{ | |
Console.WriteLine("Error"); | |
} | |
order = Console.ReadLine(); | |
} | |
return matrix; | |
} | |
static void FindLargestSum(double[,] matrix) | |
{ | |
double rowSum = double.MinValue; | |
int rowIndex = 0; | |
for (int row = 0; row < matrix.GetLength(0); row++) | |
{ | |
double sum = 0.0; | |
for (int col = 0; col < matrix.GetLength(1); col++) | |
{ | |
sum += matrix[row, col]; | |
} | |
if (sum > rowSum) | |
{ | |
rowSum = sum; | |
rowIndex = row; | |
} | |
} | |
double colSum = double.MinValue; | |
int colIndex = 0; | |
for (int col = 0; col < matrix.GetLength(0); col++) | |
{ | |
double sum = 0.0; | |
for (int row = 0; row < matrix.GetLength(1); row++) | |
{ | |
sum += matrix[row, col]; | |
} | |
if (sum > colSum) | |
{ | |
colSum = sum; | |
colIndex = col; | |
} | |
} | |
double leftDiagonal = FindLeftDiagonal(matrix); | |
double rightDiagonal = FindRightDiagonal(matrix); | |
PrintLargestSum(rowSum, rowIndex, colSum, colIndex, leftDiagonal, rightDiagonal); | |
} | |
static double FindRightDiagonal(double[,] matrix) | |
{ | |
double sum = 0.0; | |
int col = 0; | |
for (int row = matrix.GetLength(0) - 1; row > -1; row--) | |
{ | |
sum += matrix[row, col]; | |
col++; | |
} | |
return sum; | |
} | |
static double FindLeftDiagonal(double[,] matrix) | |
{ | |
double sum = 0.0; | |
int col = 0; | |
for (int row = 0; row < matrix.GetLength(0); row++) | |
{ | |
sum += matrix[row, col]; | |
col++; | |
} | |
return sum; | |
} | |
static void PrintLargestSum(double rowSum, int row, double colSum, int col, double left, double right) | |
{ | |
if (rowSum >= colSum && rowSum >= left && rowSum >= right) | |
{ | |
Console.WriteLine("ROW[{0}] = {1:F2}", row, rowSum); | |
} | |
else if (colSum > rowSum && colSum >= left && colSum >= right) | |
{ | |
Console.WriteLine("COLUMN[{0}] = {1:F2}", col, colSum); | |
} | |
else if (left > rowSum && left > colSum && left >= right) | |
{ | |
Console.WriteLine("LEFT-DIAGONAL = {0:F2}", left); | |
} | |
else if (right > rowSum && right > colSum && right > left) | |
{ | |
Console.WriteLine("RIGHT-DIAGONAL = {0:F2}", right); | |
} | |
else | |
{ | |
Console.WriteLine("ERROR"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment