Getting numbers from a matrix - c#

Hi my problem is I can't get the numbers with more than 2 digits from this matrix if anyone can help I would appriciate it here i my code :
Console.Write("x: ");
int x = int.Parse(Console.ReadLine());
Console.Write("y: ");
int y = int.Parse(Console.ReadLine());
int[,] arr = new int[x, y];
int[,] arr2 = new int[x, y];
Random rand = new Random();
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
int randNUm = rand.Next(0, 20);
arr[i, j] = randNUm;
Console.Write(arr[i, j] + " ");
if (arr[i, j] >= 10)
{
arr2[i,j] = arr[i,j]
}
}
}

Actually you have done your job, all it remains is to display the results. If
the task specifies that the printing must be done in 2 steps as you imply,
try:
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
int randNUm = rand.Next(0, 20);
arr[i, j] = randNUm;
Console.Write(arr[i, j] + " ");
if (arr[i, j] >= 10)
{
arr2[i,j] = arr[i,j]
}
}
}
Console.WriteLine("---Proceeding to 2 digit numbers---");
for (int i = 0; i < arr2.GetLength(0); i++)
{
for (int j = 0; j < arr2.GetLength(1); j++)
{
Console.Write(arr2[i, j] + " ");
}
}
EDIT : Consider what Henk comments and try to optimise your solution.

Related

Can't perambulate indexes in matrix the right way

Hello. I have this task to sum the numbers as shown. Tried everything I can, but still not the right answer. Can I have some guidance?
static void Main(string[] args)
{
string input = Console.ReadLine();
int n = (int)char.GetNumericValue(input[0]);
int m = (int)char.GetNumericValue(input[2]);
int[,] matrix = new int[n, m];
int sum = 0;
//fill matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i, j] = (j * 3 + 1) + i * 3;
}
}
for (int i = 0; i < matrix.GetLength(0) - 1; i+=1)
{
for (int j = 0; j < matrix.GetLength(1) - i; j+=1)
{
if (i % 2 == 0)
{
sum += matrix[i, j + i] + matrix[i + 1, j + 1];
}
}
}
Console.WriteLine(sum);
}
I think you would've a easier time hard coding the input (and naming them as "columns" and "rows" instead, much more readable).
What is the expected output? Not sure I'm following this sum. I'm guessing, 297? If so:
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
if(j == 5) Console.WriteLine();
if (matrix[i, j] % 2 != 0)
{
if (i == 0 || i == matrix.GetLength(0) - 1
|| j == 0 || j == matrix.GetLength(0))
{
sum += (matrix[i, j]);
}
else
{
sum += (matrix[i, j] * 2);
}
}
}
}

C# [i, j] add the min "j" values ​of matrix to the new array?

[i, j] add the biggest values ​​of matrix to j the new array
its here:
But smallest not working..
Min values in "j" (not working)
for (int i = 0; i < olay; i++)
{
for (int j = 0; j < state; j++)
{
if (minimax[i] > matris[i, j])
{
minimax[i] = matris[i, j];
}
}
}
Console.WriteLine();
for (int i = 0; i < minimax.Length; i++)
{
Console.WriteLine(i + 1 + ". MINIMAX " + minimax[i]);
}
Console.ReadLine();
}
Max VALUES IN "J" (WORKING)
for (int i = 0; i < olay; i++)
{
for (int j = 0; j < state; j++)
{
if (maximax[i] < matris[i, j])
{
maximax[i] = matris[i, j];
}
}
}
/* Olaylar icin en yuksek State degerleri */
Console.WriteLine();
for (int i = 0; i < maximax.Length; i++)
{
Console.WriteLine(i + 1 + ". MAXIMAX " + maximax[i]);
}
Output console:
IMG Lınk: https://pasteboard.co/IKKQlet.jpg
The default values in your arrays are 0.
To calculate the minimum value in each column and write it to an array you can do something like this:
var resultArray = new int[rowLength];
var matrix = new int[rowLength, columnLength];
for (int i = 0; i < rowLength; i++)
{
// you have to set the first value as a minimum and after that compare
resultArray[i] = matrix[i, 0];
for (int j = 1; j < columnLength; j++)
{
if (matrix[i, j] < resultArray[i])
{
resultArray[i] = matrix[i, j];
}
}
}

I want to sort matrix in ascending order in C# without any function

Matrix entered the result is:
1 5 48
7 11 3
P.S User can enter any number not like above.
It should be in ascending order: 1 3 5 7 11 48
I used loop but it does not work properly
using System;
namespace MainClass
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n, sum = 0;
int count = 0;
Console.Write("The MxN matrix\n");
Console.Write("Enter the number of rows and columns of the matrix :\n");
Console.Write("Rows (M): ");
m = Convert.ToInt32(Console.ReadLine());
Console.Write("Columns (N): ");
n = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[m, n];
Console.Write("Enter elements in the first matrix :\n");
/* Entering matrix elements */
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write("element - [{0}],[{1}] : ", i, j);
arr[i, j] = Convert.ToInt32(Console.ReadLine());
/* Calculating odd numbers of the matrix */
if (arr[i, j] % 2 != 0)
{
sum += arr[i, j];
}
}
}
Console.Write("\nThe matrix is:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
Console.Write("{0} ", arr[i, j]);
Console.Write("\n");
}
Console.Write("\nThe sum of odd numbers is: {0}", sum);
/* Sorting Matrix in ascending order*/
for (i = 0; i < m; i++)
{
for (j = i+1; j < n; j++)
{
for (int j1 = 0; j1 < n; j1++ )
{
if (arr[i, j] > arr[i, j1]){
int temp = arr[i, j];
arr[i, j] = arr[i, j1];
arr[i, j1] = temp;
}
}
}
}
Console.Write("\nAscending order: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
Console.Write("{0} ", arr[i, j]);
}
Console.Read();
}
}
}
Here you are.
using System;
namespace MainClass
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n, sum = 0; int count = 0; Console.Write("The MxN matrix\n"); Console.Write("Enter the number of rows and columns of the matrix :\n"); Console.Write("Rows (M): "); m = Convert.ToInt32(Console.ReadLine()); Console.Write("Columns (N): "); n = Convert.ToInt32(Console.ReadLine()); int[,] arr = new int[m, n]; Console.Write("Enter elements in the first matrix :\n");
/* Entering matrix elements */
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
{
Console.Write("element - [{0}],[{1}] : ", i, j);
arr[i, j] = Convert.ToInt32(Console.ReadLine());
/* Calculating odd numbers of the matrix */
if (arr[i, j] % 2 != 0)
{
sum += arr[i, j];
}
}
}
Console.Write("\nThe matrix is:\n");
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
Console.Write("{0} ", arr[i, j]);
Console.Write("\n");
}
Console.Write("\nThe sum of odd numbers is: {0}", sum);
/* Sorting Matrix in ascending order*/
for (i = 0; i < arr.Length - 1; i++)
{
for (j = i + 1; j < arr.Length; j++)
{
int row1 = i % m;
int col1 = i / m;
int row2 = j % m;
int col2 = j / m;
if (arr[row1, col1] > arr[row2, col2])
{
int temp = arr[row1, col1];
arr[row1, col1] = arr[row2, col2];
arr[row2, col2] = temp;
}
}
}
Console.Write("\nAscending order: ");
for (j = 0; j < n; j++)
{
for (i = 0; i < m; i++)
Console.Write("{0} ", arr[i, j]);
}
Console.Read();
}
}
}

How to make a matrix multiplication in a task

I am unable unable to get the product of matrices in a task in order to run it
in several instances. I think the task is not properly doing the multiplication and calling several 0's.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _010029428_Multiplicacion
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n;
Random rnd = new Random((int)DateTime.Now.Ticks & 0x000fff);
Console.WriteLine("Ingrese el numero de renglones y columnas: ");
m = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[m, n];
Console.WriteLine("first Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("First matrix is: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
int[,] b = new int[m, n];
Console.WriteLine("Second Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("The second matrix is:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(b[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("The result is :");
int[,] c = new int[m, n];
var multiplicacion = new Task(() =>
{
for (j = 0; j < n; j++)
{
c[i, j] = 0;
for (int k = 0; k < n; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
});
for (i = 0; i < m; i++)
{
multiplicacion.Start();
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
}
}
}
The code is using multiplicacion.start() to run it asynchronously
Not sure what the problem may be
There are several problems.
First: You cannot create just one task and run it multiple times at once. New Task() must be created for each value of "i".
Second: You cannot use global variable ("j") and mutate it in a task that runs asynchronously, without proper synchronization. This would result in program crash or wrong result. Each task must have it's own (local) variable ("jj") if it needs to change it. Read-only variables (i, a, b, c, m, n) are usually safe to use, but it is better to pass them as arguments.
Third: You must wait for all tasks to finish before you write result to screen.
Four: Running task asynchronously has significant performance overhead. So unless you matrices are really huge (probably at least hundreads of rows and columns), calculating result in several asynchronous tasks is probably much slower than simple synchronous calculation on main thread.
Example of how to make it work follows. Code is still ugly and not wery good, but should work and give you some starting point for further improvements.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _010029428_Multiplicacion
{
class Program
{
static void Main(string[] args)
{
int i, j, m, n;
Random rnd = new Random((int)DateTime.Now.Ticks & 0x000fff);
Console.WriteLine("Ingrese el numero de renglones y columnas: ");
m = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[m, n];
Console.WriteLine("first Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("First matrix is: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
int[,] b = new int[m, n];
Console.WriteLine("Second Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[i, j] = rnd.Next(1, 4);
}
}
Console.WriteLine("The second matrix is:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(b[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("The result is :");
int[,] c = new int[m, n];
Task[] tasks = new Task[m];
for (i = 0; i < m; i++)
{
var multiplicacion = new Task((parameter) =>
{
int ii = (int)parameter;
for (int jj = 0; jj < n; jj++)
{
c[ii, jj] = 0;
for (int k = 0; k < n; k++)
{
c[ii, jj] += a[ii, k] * b[k, jj];
}
}
},
i);
tasks[i] = multiplicacion;
multiplicacion.Start();
}
Task.WaitAll(tasks);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}

Multiplying two matrices but getting the wrong answer

This is my matrix code. I am multiplying two matrices. One of the matrices is scalar (meaning diagonal elements are the same), but when I run this code, I am getting the wrong answer.
static void Main(string[] args)
{
int[,] matrix1 = new int[3, 3];
int[,] matrix2 = new int[3, 3];
int[,] result = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("Enter 1st Matrix: ");
matrix1[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.ReadLine();
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
Console.WriteLine("Enter 2nd Matrix: ");
matrix2[k, l] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine();
Console.WriteLine("Matrix 1: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(matrix1[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Matrix 2: ");
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
Console.Write(matrix2[k, l] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Matrix 1 * Matrix 2: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i, j] = result[i, j] + matrix1[i, j] * matrix2[i, j];
Console.Write(result[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
Console.ReadLine();
}
You're not multiplying the matrices, you're multiplying their values. See https://en.wikipedia.org/wiki/Matrix_multiplication
E.g. {{1, 1}, {0, 0}} x {{1, 0}, {1, 0}} should result in {{2, 0}, {0, 0}}, not {{1, 0}, {0, 0}}.
Here is the right code for matrix multiplication (note that it has the complexity of O(n^3), not O(n^2)):
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i, j] = 0;
for(int k = 0; k < 3; k++)
{
result[i, j] = result[i, j] + matrix1[i, k] * matrix2[k, j];
}
Console.Write(result[i, j] + " ");
}
Console.WriteLine();
}

Categories

Resources