C
C#3mo ago
Array

Reading in a File C# 2D arrays

I have to code a program that will read in a 2D array of 9 x 9 integers from a file that represents a Sudoku board solution My goals are to 1. Read in the file and display it on the form (it is a text file) 2. After this, I have to write a validator for the solution Right now, I’m focusing on part 1 which is reading in the file Usually, the goal when reading in a file is to do the following (Writing code to select the file) 1. OpenFileDialog oFD = new OpenFileDialog(); 2. if (oFD.ShowDialog() == DialogResult.OK) (File selected so open to read it) 3. StreamReader oSR = new StreamReader(oFD.OpenFile()); 4. int Records = int.Parse(oSR.ReadLine()); (Initializing the arrays) 5. Set array = new int [Records]; 6. for (int i = 0; i < array.length; i++) Array[i] = int.Parse(oSR.ReadLine()); This is the procedure I usually go through to read in a file My issue is since this is a 2D array, I get the error cannot implicitly (My code for this 2D array, I get an error when setting SudokuBoard to Records): https://paste.mod.gg/ttybylvutrvb/0 https://paste.mod.gg/gokonoflypqv/0( here is how I normally code it in) Anyone have ideas on how I code it in to read in the file?
BlazeBin - ttybylvutrvb
A tool for sharing your source code with the world!
BlazeBin - gokonoflypqv
A tool for sharing your source code with the world!
No description
No description
7 Replies
Angius
Angius3mo ago
If it's a 2D array, you will need to treat the lines as one dimension, and number columns as another dimension So, with an int[9, 9] array, go through the first dimension, and for each of them, go through the second one In pseudocode:
for line_idx = 0; line_idx < 9; line_idx++
line = file_lines[line_idx]
for number_idx = 0; number_idx < 9; number_idx++
array[line_idx, number_idx] = line[number_idx]
for line_idx = 0; line_idx < 9; line_idx++
line = file_lines[line_idx]
for number_idx = 0; number_idx < 9; number_idx++
array[line_idx, number_idx] = line[number_idx]
Array
Array3mo ago
Thanks for responding! Can I ask, what do you mean with the for line_idx = 0; ?
Angius
Angius3mo ago
The index of the line
| 0 1 2
——+———————
0 | 1 2 3
1 | 4 5 6
2 | 7 8 9
| 0 1 2
——+———————
0 | 1 2 3
1 | 4 5 6
2 | 7 8 9
Use the fact, that a string is just a char[] under the hood
MODiX
MODiX3mo ago
Angius
REPL Result: Success
var lines = new[]{
"CAT",
"DOG"
};

// v line index
var x = lines[0][2];
var y = lines[1][1];
var z = lines[1][0];
// ^ column/character index

new {x, y, z}
var lines = new[]{
"CAT",
"DOG"
};

// v line index
var x = lines[0][2];
var y = lines[1][1];
var z = lines[1][0];
// ^ column/character index

new {x, y, z}
Result: <>f__AnonymousType0#1<char, char, char>
{
"x": "T",
"y": "O",
"z": "D"
}
{
"x": "T",
"y": "O",
"z": "D"
}
Compile: 274.397ms | Execution: 76.017ms | React with ❌ to remove this embed.
Array
Array3mo ago
Gotcha, I kinda understand this, I can use this to loop through (after I set it equal to records), my only problem is that I have to make it = to records and I'm not sure how to
Array
Array3mo ago
I did something similar to that with the loops hough, it makes sense given it is a row and column
No description
Array
Array3mo ago
I'll try this out I tried this out and my error was that when setting my array it couldn’t convert it to the single again The same type of implicit error cane up Or can anyone help me out