0% found this document useful (0 votes)
18 views5 pages

Helper cs-1

The document contains code for methods to work with two-dimensional integer arrays. It includes methods to fill arrays with user input or random numbers, print arrays, check if an array is square or symmetric, print main and secondary diagonals, calculate row/column sums, surrounding sums, and circularly shift the array.

Uploaded by

TOM9J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Helper cs-1

The document contains code for methods to work with two-dimensional integer arrays. It includes methods to fill arrays with user input or random numbers, print arrays, check if an array is square or symmetric, print main and secondary diagonals, calculate row/column sums, surrounding sums, and circularly shift the array.

Uploaded by

TOM9J
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

 sing System;

u
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrDoMemadi
{
internal class Helper
{
public static void ArrFill(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.WriteLine("enter the next number: ");
arr[i, j] = int.Parse(Console.ReadLine());
}
}
}
public static void ArrRandomFill(int[,] arr)
{
Random rnd = new Random();
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = rnd.Next(0, 100);
}
}
}
public static void PrintArr(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write("{0,3}",arr[i, j]);
}
Console.WriteLine();
}
}
public static bool IsSquare(int[,] arr)
{
if (arr.GetLength(0) != arr.GetLength(1))
{
return false;
}
return true;
}
public static bool IsSymmetric(int[,] arr)
{
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);

if (rows == cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols / 2; j++)
{
if (arr[i, j] != arr[i, cols - 1 - j] || arr[i, j] != arr[rows - 1 - i, j])
{
return false;
}
}
}
return true;
}
return false;
}
public static void PrintMainDiagonal(int[,] arr)
{

if (arr.GetLength(0) == arr.GetLength(1))
{
Console.WriteLine("numbers in main diagonal are: ");
for (int i = 0; i < arr.GetLength(1); i++)
{
if (i == arr.GetLength(0) - 1)
{
Console.Write(arr[i, i]);
Console.WriteLine();
}
else
{
Console.Write(arr[i, i] + ", ");
}
}
}
}
public static void printSecondaryDiagonal(int[,] arr)
{
if (arr.GetLength(0) == arr.GetLength(1))
{
int j = 0;
Console.WriteLine("numbers in Secondary diagonal are: ");
for (int i = arr.GetLength(0) - 1; i >= 0; i--)
{
if (j == arr.GetLength(1) - 1)
{
Console.Write(arr[i,j]);
Console.WriteLine();
}
else
{
Console.Write(arr[i, j] + ", ");
}
j++;
}
}
}
public static int sumRow(int[,] arr,int row)
{

int sum = 0;
Console.Write("the sum of row {0} is: ",row);

for (int j = 0;j < arr.GetLength(1);j++)


{
sum += arr[row,j];
}
return sum;
}
public static int sumCol(int[,] arr,int col)
{
int sum = 0;
Console.Write("the sum of col {0} is: ",col);

for (int i = 0;i < arr.GetLength(0);i++)


{
sum += arr[i,col];
}
return sum;
}
public static int CalculateSurroundingSum(int[,] arr, int row, int col)
{
int sum = 0;
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);
for (int i = row - 1; i <= row + 1; i++)
{
for (int j = col - 1; j <= col + 1; j++)
{
if (i >= 0 && i < rows && j >= 0 && j < cols && !(i == row && j == col))
{
sum += arr[i, j];
}
}
}
return sum;
}
public static int[,] CircularShiftArray(int[,] arr)
{
int rows = arr.GetLength(0);
int cols = arr.GetLength(1);
int[,] result = new int[rows, cols];

for (int i = 0; i < cols - 1; i++)


{
result[0, i + 1] = arr[0, i];
}

for (int i = 0; i < rows - 1; i++)


{
result[i + 1, cols - 1] = arr[i, cols - 1];
}

for (int i = cols - 1; i > 0; i--)


{
result[rows - 1, i - 1] = arr[rows - 1, i];
}

for (int i = rows - 1; i > 0; i--)


{
result[i - 1, 0] = arr[i, 0];
}
for (int i = rows - (rows - 1); i < rows - 1; i++)
{
for (int j = cols - (cols - 1); j < cols - 1; j++)
{
result[i, j] = arr[i, j];
}
}

return result;
}

}
}

You might also like