Advertisement
Xsufu

Считывание цифр из файла в матрицу

Sep 11th, 2020 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace Matrix {
  6.     class Program {
  7.         static void Main(string[] args) {
  8.             string path = @"somefile.txt";  //указываем путь к файлу
  9.             string[] lines = File.ReadAllLines($"{path}");                 //считываем строки в элементы string массива
  10.             int[,] matrix = new int[3, 3];                                 //создаём матрицу
  11.             int r = 0;
  12.  
  13.             //построчно записываем из lines в matrix
  14.             for (int i = 0; i<3; i++) {                                    
  15.                 for (int j = 0; j<3; j++) {
  16.                     matrix[i, j] = Int32.Parse(lines[r]);
  17.                     r++;
  18.                 }
  19.             }
  20.  
  21.             //выводим матрицу в консоль
  22.             for (int i = 0; i < 3; i++) {
  23.                 for (int j = 0; j < 3; j++) {
  24.                     Console.Write(matrix[i, j] + " ");
  25.                 }
  26.                 Console.WriteLine();
  27.             }
  28.         }
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement