2018. március 9., péntek

2018.03.09. Mátrix fájlból való beolvasása


A matrix.txt tartalma:

11 32 44 43 22
27 13 42 50 14
19 20 45 12 48



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

namespace matrix
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = 3;
            int M = 5;
            int[,] tomb = new int[N, M];

            StreamReader sr = new StreamReader("matrix.txt");
            string[] atmeneti;
            int i = 0;
            while (!sr.EndOfStream)
            {
                atmeneti = sr.ReadLine().Split(' ');
                tomb[i, 0] = Convert.ToInt32(atmeneti[0]);
                tomb[i, 1] = Convert.ToInt32(atmeneti[1]);
                tomb[i, 2] = Convert.ToInt32(atmeneti[2]);
                tomb[i, 3] = Convert.ToInt32(atmeneti[3]);
                tomb[i, 4] = Convert.ToInt32(atmeneti[4]);
                i++;
            }
            sr.Close();

            //mátrix kiíratása:
            int j;
            for (i = 0; i < N; i++)
            {
                for (j = 0; j < M; j++)
                {
                    Console.Write("{0} ", tomb[i, j]);
                }
                Console.WriteLine();               
            }
            Console.ReadLine();
        }
    }
}