EDITED CODE
the text file with the rows I am trying to reverse contains these numbers:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
so far my array looks like this:
static void Main()
{
int numbers;
StreamReader fileReader = new StreamReader(fileDirectory);
string line = fileReader.ReadLine();
fileReader.Close();
string[] Split = line.Split();
int.TryParse(line, out numbers);
int[,] Table = new int [10,10];
for (int row = 0; row < Table.GetUpperBound(0); row++)
{
for (int column = 0; column < Table.GetUpperBound(0); column++)
{
}
}
for (int row = 0; row < Table.GetUpperBound(0); row++)
{
for (int column = 0; column < Table.GetUpperBound(0); column++)
{
int tempHolder = Table[row, column];
Table[row, column] = Table.GetUpperBound(0);
Table[row, Table.GetUpperBound(0) - column] = tempHolder;
Console.WriteLine(Table[row, column]);
Console.Write(" ");
}
Console.WriteLine();
}Console.Write(" ");
}
can you help me with adding the int numbers variable to the array and then I will attempt the reversing. Right now if I run this I get about 24 lines of the number 9. Also can someone show me how to do a 2d array without having to specify the size of it? I have looked online but cant find out how. Thanks
I assume this is homework, so I’ll give some advice but stop short of actually providing code.
Firstly, you’ll have to Split each line to get a list of strings representing each number.
After that you need to Convert the string representation of each number to an integer.
Then it shouldn’t be too hard to loop through and add each int to the array.
You will need to keep track of the current horizontal index while processing a line, and vertical index across all lines.
After that you should be much closer. Have a go at doing the reversal and come back if you have problems again.
Edit:
For creating the array, you can specify the size from int variables. If you had
String[] lines(one for each line) andString[] line(a line fromlinessplit by the space character), you could declare the array like so:You’re getting pretty close with this code.
For String.Split, you need to tell it which character separates the elements you want:
This needs to be done once for each line in the file, so it should be moved inside the outer loop. Similarly,
int.TryParseneeds to be done once for each number, so it should be inside the inner loop.You should also watch your
GetUpperBoundcalls.GetUpperBound(d)returnsGetLength(d) - 1so you need to loop while the index is les than or equal toGetUpperBound. You could also replace theGetUpperBoundcalls withGetLength(0)(Columns) andGetLength(1)(rows).The number passed to
GetUpperBoundandGetLengthis the direction you want to get the size in. You’re passing 0 to all of these, which won’t work unless the array is square.In the reversing loops, there’s an error on the second line of the inner loop. You’re setting
Table[row, column]to the size of the array, instead of the element to be swapped. You also only need to loop in to the middle of each row, otherwise you’ll just swap the elements back to their original positions on the last half.