calculate matrix east west diagonal in C# - c#

any body can help me for a program
I want to make a code that can fill a matrix that way in C#
0 0 0 0 5
0 0 0 4 0
0 0 3 0 0
0 2 0 0 0
1 0 0 0 0
i am working on it for a week but i dident find a solution
thank you

Here is your solution. At least try to understand how it works.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var n = 5;
var result = Enumerable.Range(0,n*n).GroupBy(k=> k/n).Select(g=> g.Select(k=> (k % n == (n-1-k/n)) ? k%n+1 : 0).ToArray()).ToArray();
foreach(var r in result)
{
foreach(var a in r)
{
Console.Write(" {0}", a);
}
Console.WriteLine();
}
}
}
}

thank you i found a solution
using System;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[5, 5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (i + j == 4)
{
arr[i, j] = j + 1;
}
else
{
arr[i, j] = 0;
}
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}

Here's my attempt:
int[,] arr = new int[n,n];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i+j-1 == n)
{
arr[i,j] = j+1;
}
else
{
arr[i,j] = 0;
}
Console.Write(arr[i,j] + " ");
}
Console.WriteLine();
}
I haven't checked if this specific code works, but something like this should do the trick.

Related

C# 2DArray IndexOutOfBound, Why?

In the following C# code I want to create a 2D Array with the size of [5,2].
If I understand it correctly, that should look like this:
0 1 2
0[0,0] [0,1] [0,2]
1[1,0] [1,1] [1,2]
2[2,0] [2,1] [2,2]
3[3,0] [3,1] [3,2]
4[4,0] [4,1] [4,2]
But why does my program throw out an
IndexOutOfRangeException
...?
using System;
namespace program {
class program{
Random random = new Random();
int[,] board = new int[5,2];
public program(){
Print2DArray(randomBoard(0.5,board));
}
public int[,] randomBoard(double chance, int[,] board){
int[,] temp = board;
for(int y = 0; y < board.GetLength(1); y++){
for(int x = 0; x < board.GetLength(0); x++){
if(random.NextDouble() <= chance){
board[y,x] = 1;
} else {
board[y,x] = 0;
}
}
}
return temp;
}
public static void Print2DArray<T>(T[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i,j] + "\t");
}
Console.WriteLine();
}
}
static void Main(string[] args){
program program1 = new program();
}
}
}
In your code, y goes from 0 to number of elements in dimension 1
while x goes 0 to number of elements in dimension 0
However, inside your for loop you seem to have the dimensions swapped,
board[y,x] = 1; // change this to board [x,y] = 1;
similarly for the else condition it should be board[x,y] = 0

Custome Size 2D Array with Fitting For Loops

I am creating a 2D array that can have any custom size 1x1, 2x2, 3x3 .... nxn and depending on what the size of the array is, it gets filled with 0 to (n^2)-1, so if it is 2x2 it outputs:
3 2
1 0
It should be something like this int[,] arr = new int [n, n];, to create the 2d array, but how would the for loops be constructed?
what I have tried
using System;
namespace moveElement {
public class move {
static void Main(string[] args) {
int n = 3;
int[, ] arr = new int[n, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
The result of the code:
0 0 0
0 0 0
0 0 0
int k = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i,j] = k++;
}
}
full code to the question:
using System;
namespace moveElement
{
public class move
{
static void Main(string[] args)
{
int n = 3;
int[,] arr = new int [n, n];
//Ivan Smyrnov solution
int k = (n * n) - 1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i,j] = k--;
}
}
//Ivan Smyrnov solution
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
Output when n is 3:
8 7 6
5 4 3
2 1 0

Irregular table Issue

I wonder what I do wrong about that task:
I had to create a table[][,] where I enter from the keyboard number of groups, the number of students in each group and how many subjects they have.
For each student, I had to enter a grade for each subject.
I don't actually where is my mistake.
Because the program I wrote can't show properly full table (f.e. if in a group no. 1 I have 3 students and for a group no. 2 I have 1 student it shows for each group 1 student)
Also I used "float" to calculate the Average of all grades for each group but it shows the integer number.
Can you help me?
I attached the code down below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpecialTask
{
class Program
{
static int[][,] tab1;
static int iSub = 0;
static int iGro = 0;
static int iStu = 0;
static int note = 0;
static int iSum = 0;
static float fAvg = 0.0f;
static int Subjects(string sSubjects)
{
do
{
Console.WriteLine("Please enter a number of subjects (2-4):");
}
while ((!int.TryParse(Console.ReadLine(), out iSub)) || (iSub < 2) || (iSub > 4));
return iSub;
}
static int Groups(string sGroups)
{
do
{
Console.WriteLine("Please enter a number of groups (1-3):");
}
while ((!int.TryParse(Console.ReadLine(), out iGro)) || (iGro < 1) || (iGro > 3));
return iGro;
}
static int Students(string sStudents)
{
do
{
Console.WriteLine("Please enter a number of students(1-5): ");
}
while ((int.TryParse(Console.ReadLine(), out iStu) == false) || (iStu < 1) || (iStu > 5));
return iStu;
}
static int Notes(string sNotes)
{
do
{
Console.WriteLine("Please enter a note (2-5): ");
}
while ((!int.TryParse(Console.ReadLine(), out note)) || (note < 2) || (note > 5));
return note;
}
static void TabData()
{
int i, j, k;
Subjects("Please enter a number of subjects (2-4):");
Groups("Please enter a number of groups (1-3):");
tab1 = new int[iGro][,];
for (i = 0; i < iGro; i++)
{
Console.WriteLine("Group NO.{0}", 1 + i);
Students("Please enter a number of students(1-5): ");
tab1[i] = new int[iStu, iSub];
for (j = 0; j < iStu; j++)
{
for (k = 0; k < iSub; k++)
{
Console.WriteLine("Student NO.{0}", 1 + j);
Console.WriteLine("Subject NO.{0}", 1 + k);
tab1[i][j, k] = Notes("Please enter a note (2-5): ");
}
}
}
Console.WriteLine("\n");
Console.WriteLine("\n");
for (i = 0; i < iGro; i++)
{
Console.WriteLine("Group NO.{0}: ", 1 + i);
for (j = 0; j < tab1.Length; j++)
{
Console.WriteLine("Student NO.{0}/Group NO.{1}: ",1+ j, 1 + i);
for (k = 0; k < iSub; k++)
{
Console.Write("Subject NO.{0}: ", 1 + k);
Console.Write(tab1[i][j, k]);
Console.Write("\n");
}
Console.WriteLine("\n");
}
}
}
static void avgG()
{
int i, j, k;
TabData();
for (i = 0; i < iGro; i++)
{
for (j = 0; j < iStu; j++)
{
for (k = 0; k < iSub; k++)
{
iSum = iSum + tab1[i][j, k];
}
}
fAvg = iSum / iSub;
Console.WriteLine("Sum: " + iSum);
Console.WriteLine("Average of all grades for Group NO. {0}: {1}", i, fAvg);
}
}
static void Main(string[] args)
{
avgG();
Console.ReadKey();
Console.ReadKey();
}
}
}

C# updating values in two dimensional array

I'm converting an Eight Queens solution from Java to C# just to get my head around C# a bit more. The problem is that I'm stuck with changing values on the board. I can change the value inside the function but it goes back to what it was immediately even when I'm using ref. At the moment I'm using a global variable rather than passing the board as an argument.
Here is the code:
public static int[,] solution;
static void Main()
{
int n = 8;
solution = new int[8,8];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
solution[i,j] = 0;
}
}
Solve(n);
Console.ReadKey();
}
public static void Solve(int n)
{
if (placeQueens(0, n))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" " + solution[i,j]);
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("No possible solution");
}
}
public static bool placeQueens(int queen, int n)
{
if (queen == n)
{
return true;
}
for (int row = 0; row < n; row++)
{
if (canPlace(row, queen))
{
solution[row,queen] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" " + solution[i, j]);
}
Console.WriteLine();
}
//When I print the current board here the 1 is set
//When placeQueens is called again it is back to being 0
if (placeQueens(queen + 1, n))
{
return true;
}
solution[row,queen] = 0;
}
}
return false;
}
public static bool canPlace(int row, int col)
{ //Just checks that the position is legal}
What can I do to make the 1 stay as a one once I've changed it in placeQueen?
I changed the static variable to being passed in each method instead of being a static variable, and it works perfectly:
static void Main()
{
int n = 8;
var solution = new int[8, 8];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
solution[i, j] = 0;
}
}
Solve(solution, n);
}
public static void Solve(int[,] solution, int n)
{
if (placeQueens(solution, 0, n))
{
Console.WriteLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" " + solution[i, j]);
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("No possible solution");
}
}
public static bool placeQueens(int[,] solution, int queen, int n)
{
if (queen == n)
{
return true;
}
for (int row = 0; row < n; row++)
{
if (canPlace(solution, row, queen))
{
solution[row, queen] = 1;
Console.WriteLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" " + solution[i, j]);
}
Console.WriteLine();
}
if (placeQueens(solution, queen + 1, n))
{
return true;
}
solution[row, queen] = 0;
}
}
return false;
}
public static bool canPlace(int[,] solution, int row, int col)
{
for (var dx = -1; dx <= 1; dx++)
{
for (var dy = -1; dy <= 1; dy++)
{
if (dx == 0 && dy == 0)
continue;
int r = row, c = col;
while (r >= 0 && r <= solution.GetUpperBound(0) && c >= 0 && c <= solution.GetUpperBound(1))
{
if (solution[r, c] == 1)
return false;
r += dy;
c += dx;
}
}
}
return true;
}
The last line it outputs is a correct solution:
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0

diagonal difference OutOfRange Exception C#

I have to write a program that finds the difference between the sums of square matrix diagonals for homework, but my code throws IndexOutOFRange exception and I have no idea how to fix it.
Source code below:
//input 3 11 2 4 4 5 6 10 8 -12 //desired output: 15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class diagonalDifference
{
static void Main()
{
int N = Convert.ToInt16(Console.ReadLine());
int[,] arr = new int[N, N];
string str = string.Empty;
for (int i = 0; i < N; ++i)
{
string[] strArr = Console.ReadLine().Split(' ');
for (int j = 0; j < strArr.Length; ++j)
{
arr[i, j] = Convert.ToInt16(strArr[j]);
}
}
int left = 0, right = N - 1, ldTotal = 0, rdTotal = 0;
while (left <= right)
{
ldTotal += arr[left, left];
rdTotal += arr[left++, right];
}
Console.WriteLine(Math.Abs(ldTotal - rdTotal));
}
}
class diagonalDifference
{
static void Main()
{
int N = Convert.ToInt16(Console.ReadLine());
int[,] arr = new int[N, N];
string str = string.Empty;
for (int i = 0; i < N; ++i)
{
string[] strArr = Console.ReadLine().Split(' ');
for (int j = 0; j < strArr.Length; ++j)
{
arr[i, j] = Convert.ToInt16(strArr[j]);
}
}
int left = 0, right = N - 1, ldTotal = 0, rdTotal = 0;
while (left <= (N-1))
{
ldTotal += arr[left, left];
rdTotal += arr[left, right];
Left++;
Right--;
}
Console.WriteLine(Math.Abs(ldTotal - rdTotal));
}
}
class Result
{
/*
* Complete the 'diagonalDifference' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
public static int diagonalDifference(List<List<int>> arr)
{
int l=arr.Count;
int d1=0;
int d2=0;
for(int i=0;i<l;i++)
{
d1=d1+arr[i][i];
d2=d2+arr[i][l-1-i];
}
return (Math.Abs(d1-d2));
}
}

Categories