Hi guys, i'm new to C#. I have been trying to create a matrix or size 3x3 with the following code:

using System;
class matrix
    {
        static void Main(string[] args)
        {
            Console.Write("Enter values for the matrix: ");
            int [,]matrx=new int[3,3];
            for(int i=0;i<3;i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    matrx[i, j] = Console.Read();
                }
            }
            for(int i=0;i<3;i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(" " +matrx[i, j]);
                }
                Console.WriteLine();
            }
        }
    }

The problem is that it accepts only 3(should be 9) values but gives an output of complete 3x3 matrix with random values. For eg. if i entered the first three numbers (elements) as 1,2 & 3 (after which the loop exits itself n goes to the output), it gives the following output:

Enter values for the matrix: 1
2 
3

49 13 10
50 13 10
51 13 10
Press any key to continue...

It is my belief that the problem is with the Console.Read() line. However, i would be really grateful if you guys point out as to why i'm not getting the desired result. Thanking you all in advance.

modifiy your code to this

Console.Write("Enter values for the matrix: ");
int[,] matrx = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
matrx[i, j] = int.Parse(Console.ReadLine());
}
}

Thanks so much @RamyMahrous, the code you gave worked flawless..:icon_cheesygrin:
Repped ya.

Thanks so much @RamyMahrous, the code you gave worked flawless..:icon_cheesygrin:
Repped ya.

yes this is perfect

using System;
namespace Matrix 
{ 
    public class matrix 
    { 
        public static void Main(string[] args) 
        { 
            int i; int j; int[,] matrix = new int[3, 3]; 
            for (i = 0; i < 3; i++) 
            { 
                for (j = 0; j < 3; j++) 
                { 
                    Console.Write("Enter integer :"); 
                    matrix[i, j] = int.Parse(Console.ReadLine()); 
                } 
            } 
            System.Console.Clear();
            for (i = 0; i < 3; i++) 
            {
                for (j = 0; j < 3; j++) 
                { 
                    Console.Write("{0} ", matrix[i, j]);
                }
                Console.Write("\n");
            } 
            Console.Read();
        }
    }
}

Edited 5 Years Ago by Aydinozkan: n/a

BTW Hi Ramy!:)

@plsoft
The numbers
49 13 10
50 13 10
51 13 10
Are not so random as you think. They are ASCII codes.
49 = '1'
50 = '2'
51 = '3'
13 = 'cr' or '\n'
10 = 'lf' or '\r'

Edited 5 Years Ago by ddanbe: n/a

CMatrix.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Matrix
{
    class CMatrix
    {
        private int[,] a;
        private int r, c;
        
        public void Input()
        {
            Console.Write("Input number of row: ");
            this.r = Convert.ToInt32(Console.ReadLine());
            Console.Write("Input number of column: ");
            this.c = Convert.ToInt32(Console.ReadLine());

            this.a = new int[r, c];
            for (int i = 0; i < this.r; i++)
            {
                for (int j = 0; j < this.c; j++)
                {
                    Console.Write("Nhap phan tu a[{0}, {1}]: ", i+1, j+1);
                    a[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public void Output()
        {
            Console.WriteLine("\nResult: \n");
            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    Console.Write("{0}\t", a[i, j]);
                }
                Console.WriteLine("\n");
            }
        }

    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    using Matrix;
    class Program
    {
        static void Main(string[] args)
        {
            CMatrix m = new CMatrix();
            m.Input();
            m.Output();
        }
    }
}

Good Luck

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] mat1=new int[3,3];
            int[,] mat2 = new int[3, 3];
            int[,] mat3 = new int[3, 3]; 
            int i, j;
            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.WriteLine("enter data for matrix 1");
                    mat1[i,j] = Convert.ToInt32(Console.ReadLine());
                }
            }


            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.Write(mat1[i,j]);
                }
                Console.Write("\n");
            }

            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.WriteLine("enter data for matrix 2");
                    mat2[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }


            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.Write(mat2[i, j]);
                }
                Console.Write("\n");
            }

            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    mat3[i, j] = mat1[i, j] + mat2[i, j];
                }
                Console.Write("\n");
            }

            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.Write(mat3[i, j]);
                }
                Console.Write("\n");
            }
            Console.ReadLine();
        }
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] mat = new int[3, 4];
            int i, j;
            int sum=0;
            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.WriteLine("enter values for matrix 1");
                    mat[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    sum = sum + mat[i, j];
                    Console.Write(mat[i, j]);
                }
                Console.Write(sum);
                Console.Write("\n");
                sum = 0;
            }
            Console.ReadLine();
        }
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

sir i need to find eigenvectors and values for the 3x3 martix its really hard to find it for me... can u pls provide me how to find using program plz...!!!!:-) for the matrix A=
0.08284 0.0204 0.0198
0.0204 0.03704 0.0926
0.0198 0.0926 0.03704

Edited 4 Years Ago by Ishwarya...: forgot to give matrix values

This article has been dead for over six months. Start a new discussion instead.