case 8:
string[][] Ws1Data = new[]
{
File.ReadAllLines(#"\university\Assignment2Alg\files\YearSorted.txt"),
File.ReadAllLines(#"\university\Assignment2Alg\files\MonthSorted.txt"),
File.ReadAllLines(#"\university\Assignment2Alg\files\WS1_AFSorted.txt"),
File.ReadAllLines(#"\university\Assignment2Alg\files\WS1_RainSorted.txt"),
File.ReadAllLines(#"\university\Assignment2Alg\files\WS1_SunSorted.txt"),
File.ReadAllLines(#"\university\Assignment2Alg\files\WS1_TMaxSorted.txt"),
File.ReadAllLines(#"\university\Assignment2Alg\files\WS1_TMinSorted.txt"),
};
for (int i = 0; i < Ws1Data.Length; i++)
{
string[] innerArray = Ws1Data[i];
for (int a = 0; a < innerArray.Length; a++)
{
Console.WriteLine(innerArray[a] + " ");
}
Console.WriteLine();
}
break;
As you can see I have an array of text files which all have data inside them, I want the data to be showed in the format of columns and not below each other.
So it shows like this.
1920 january 20.2
1923 february 21.0
instead of this
1920
1923
january
february
20.2
24.2
Im stuck and cant find out how to do it, Probably really easy once its pointed out, but you dont learn if you dont ask.
Do your files have the same number of lines?
If so, something like this should work:
int nRows = Ws1Data[0].Length;
int nColumns = Ws1Data.Length;
string tempString = "";
for (int i = 0; i < nRows; i++)
{
tempString = "";
for (int j = 0; j < nColumns; j++)
{
tempString = tempString + Ws1Data[j][i];
tempString = tempString + " ";
}
Console.WriteLine(tempString);
}
Depending on the number of columns, you can have a stack overflow due to tempString. If so, just allocate the size statically
Related
For my problem I am experiencing today comes in the form of a string array. The goal of this assignment is to use Random with a seed value of 243 as a key to access a hidden message from 22 lines. Within these 22 lines are 5 letters that form a word. Here is 22 lines containing 60 characters in each line and the goal is to use the key to pick the 5 letters from each line.
String[] line = { "<?JRrP*h^vtVc^ppOZI#PCAa{ (n1>&VSf~59eI7Tn5We^77O/CEvgdq}gU0",
"G;t#o=#|^ZWV01`a-h{=Js>!z`_j!&7PB9nCgtfHZ:WtWk4e&#k5i7uV{$E/",
"]7zXf&4uA=n8!Sa08IIoKyc~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i{=[b)HEaV`M-[Wb#FlVFxNN0 I9. G?}Z#tKDmu|'gM LLzlT->M TpL",
"mKb^.+i/#NRXa7]XuX>1!gbR LOQ(q}%1H]x+.mz:=D}xB*<$eWDj_J%g/0a",
"[{&NOLF9YcL^iCvcBcY+A2LB:UoQ|V1{s,?>7krK{pb#8w]pgfa#U$tHNbay" };
For the chunk of code that I am working on comes here.
String[] decrypted = new String[22];
var randNum = new Random(243);
int i, k;
for (i = 0; i < 22; i++)
{
String currentLine = line[i];
for (k = 0; k < 5; k++)
{
decrypted[i] = Convert.ToString(currentLine[randNum.Next(0, 60)]);
}
}
printIt(decrypted);
Console.ReadLine();
}
static void printIt(string[] decrypted)
{
var build = new StringBuilder();
for (int h = 0; h < 22; h++)
{
build.Append(Convert.ToString(decrypted[h]));
}
Console.WriteLine(build);
The help I am looking for is to understand how I can store the 5 characters from each line successfully within the decrypted array.
I can gain the correct answer if I insert directly in my nested for loop the Console.WriteLine(decrypted[i]);
However, if I try to pull the same line anywhere after the for loop containing random, I only am able to pull the first letter of each line.
Change your loop to:
String[] decrypted = new String[110];
for (i = 0; i < 22; i++)
{
String currentLine = line[i];
for (k = 0; k < 5; k++)
{
decrypted[k + i * 5] = Convert.ToString(currentLine[randNum.Next(0, 60)]);
}
}
And Print:
static void printIt(string[] decrypted)
{
var build = new StringBuilder();
for (int h = 0; h < decrypted.Length; h++)
{
build.Append(Convert.ToString(decrypted[h]));
}
Console.WriteLine(build);
}
I was able to solve my own problem after doing a bit more research and deduction of what I could possibly be missing. Here is the solution.
for (i = 0; i < 22; i++)
{
String currentLine = line[i];
for (k = 0; k < 5; k++)
{
decrypted[i] = decrypted[i] + Convert.ToString(currentLine[randNum.Next(0, 60)]);
//Adding the previous character to the new character to help build up the string.
}
It was after I realized I wasn't building up the characters properly I chose to add the previous character and it solved my problem.
I have a text file that is being read in and then stored in a string[] which I then then convert into an int[], my bubblesort then should sort it but it doesn't because the values from the text files are decimals. So my question is how do I convert either the string[] or int[] to something that can accept decimal values, such as a double[] if there is such a thing. Thanks.
Code:
string[] sh1OpenData = File.ReadAllLines("SH1_Open.txt");
...
else if(input2.ToLower() == "open") //----
{
int[] intSh1OpenData = new int[sh1OpenData.Length];
for (int x = 0; x < sh1OpenData.Length; x++)
{
intSh1OpenData[x] = Convert.ToInt32(sh1OpenData[x]);
}
Console.WriteLine("\n");
Console.WriteLine("Unsorted");
for (int i = 0; i < intSh1OpenData.Length; i++)
{
Console.Write(intSh1OpenData[i] + " ");
Console.WriteLine(" ");
}
int temp = 0;
for (int write = 0; write < intSh1OpenData.Length; write++)
{
for (int sort = 0; sort < intSh1OpenData.Length - 1; sort++)
{
if (intSh1OpenData[sort] > intSh1OpenData[sort + 1])
{
temp = intSh1OpenData[sort + 1];
intSh1OpenData[sort + 1] = intSh1OpenData[sort];
intSh1OpenData[sort] = temp;
}
}
}
Console.WriteLine("\n\n\nSORTED");
for (int i = 0; i < intSh1OpenData.Length; i++)
Console.Write(intSh1OpenData[i] + "\n");
}
You should not be using int to do comparisons on string. Use String.Compare(return 0 if equal, -1 if less than, or 1 if greater than) or List.Sort() to sort string array
Pretty simple with LINQ
var asDouble = sh1OpenData.Select(x => Double.Parse(x)).OrderBy(x => x).ToArray();
This will give you a sorted (ascending order) array of Double.
Note: this assumes that all of sh1OpenData can be parsed as a Double, and will throw an exception if not.
The only changes that you will need to make are listed below:
double[] intSh1OpenData = new double[sh1OpenData.Length]; // double[] instead of int[]
for (int x = 0; x < sh1OpenData.Length; x++)
{
intSh1OpenData[x] = Convert.ToDouble(sh1OpenData[x]); // Convert to Double
}
also change the declaration of your temp variable to double temp;
Something that you could read through since you mentioned that you are new to programming:
C# Sort Arrays and Lists Examples
MSDN: List.Sort Method
I'm trying to learn how to work with 2D-array and I can't seem to understand how to print them correctly. I want to print them in a "square" like 5x5 but all I get is one line. I've tried both WriteLine and Write and changed some of the variables in the loops but I get either an error or not the result I want to have. The code is supposed to print out a 5x5 with a random sequence of 15 numbers in each column. I get the correct numbers out of it, it's only the layout that is wrong.
static void Main(string[] args)
{
Random rnd = new Random();
int[,] bricka = new int[5, 5];
int num = 0;
int num1 = 1;
for (int i = 0; i < bricka.GetLength(1); i++)
{
num += 16;
for (int j = 0; j < bricka.GetLength(0); j++)
{
bricka[j, i] = rnd.Next(num1, num);
}
num1 += 16;
}
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
}
Console.ReadKey();
}
This is my print, I would like to have the the 12 under the 8 and 14 under the 12 and so on.
http://i.imgur.com/tfyRxf1.png
You need to call WriteLine() after each line, so that each line is printed on a separate line:
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
Console.WriteLine();
}
That would be one way of doing it, anyway.
I am trying to read a text file into a 2d array. However I get an error of
Input string was not in the correct format.
I have checked the text file and it is all as it should be and I cant see why this error is happening?
int[,] numberMatrix = new int[10, 10];
string[] split = null;
for (int rowCount = 1; rowCount < 11; rowCount++)
{
int[] temp1DArray = new int[10];
string fileLocation = "C:\\Week10\\one.txt";
string textFile = File.ReadAllText(fileLocation);
for (int columnCount = 1; columnCount < 11; columnCount++)
{
string delimStr = " ";
char[] delimiter = delimStr.ToCharArray();
//string fileLocation = "C:\\Week10\\1-100.txt";
//string textFile = File.ReadAllLines(fileLocation);
for (int x = 0; x <= 10; x++)
{
split = textFile.Split(delimiter, x);
}
}
for (int rowCount1 = 1; rowCount1 < 11; rowCount1++)
{
for (int columnCount = 1; columnCount < 11; columnCount++)
{
numberMatrix[rowCount1 - 1, columnCount - 1] =Convert.ToInt32(split.ElementAt(columnCount - 1));
}
}
}
for (int rowCount = 10; rowCount > 0; rowCount--)
{
for (int columnCount = 10; columnCount > 0; columnCount--)
{
Console.WriteLine(numberMatrix[rowCount - 1, columnCount - 1]);
}
}
}
So ok, you haven't provided any file contents and exact Exception description (it can fire on any reason possible). I can give much more simple implementation for file parsing. I can't think of an answer, which will magically find the reason of why one number in your file can't be parsed to int
string[] lines = File.ReadAllLines(#"C:\temp\1.txt");
var options = StringSplitOptions.RemoveEmptyEntries;
int[][] numbers = lines.Select(line => line.Split(new[]{' '}, options)
.Select(int.Parse)
.ToArray())
.ToArray();
Console.WriteLine(string.Join(Environment.NewLine,
numbers.Select(n => string.Join(" ", n))));
For file:
1 10 20 30
4234 35 123 543
42 54 345 645
prints:
1 10 20 30
4234 35 123 543
42 54 345 645
If you need rectangle array int[,] use next code to parse it to.
int [,] numbersRect = new int[numbers.Length, numbers[0].Length];
for (int i = 0; i < numbersRect.GetLength(0); i++)
{
for (int j = 0; j < numbersRect.GetLength(1); j++)
{
numbersRect[i,j] = numbers[i][j];
}
}
From where I standing split returns an array. And numberMatrix[rowCount1 - 1, columnCount - 1] is array element - not array itself. So numberMatrix[rowCount1 - 1, columnCount - 1] =Convert.ToInt32(split.ElementAt(columnCount - 1)); will fire an exception.
Also Convert.ToInt32 takes a single value not an array.
I have this loop, which I am using to get the values of all cells within all rows of a gridview and then write it to a csv file. My loop looks like this:
string filename = #"C:\Users\gurdip.sira\Documents\Visual Studio 2008\WebSites\Supressions\APP_DATA\surpressionstest.csv";
StreamWriter sWriter = new StreamWriter(filename);
string Str = string.Empty;
string headertext = "";
sWriter.WriteLine(headertext);
for (int i = 0; i <= (this.GridView3.Rows.Count - 1); i++)
{
for (int j = 0; j <= (this.GridView3.Columns.Count - 1); j++)
{
Str = this.GridView3.Rows[i].Cells[j].Text.ToString();
sWriter.Write(Str);
}
sWriter.WriteLine();
}
sWriter.Close();
The problem with this code is that, when stepping through, the 2nd loop (the one going through the columns) does not begin as the debugger does not hit this loop and thus my file is empty.
Any ideas on what is causing this? The code itself looks fine.
Thanks
I think the inner loop should access the Cells, not the columns:
for (int i = 0; i <= (this.GridView3.Rows.Count - 1); i++)
{
for (int j = 0; j <= (this.GridView3.Rows[i].Cells.Count - 1); j++)
{
Str = this.GridView3.Rows[i].Cells[j].Text.ToString();
sWriter.Write(Str);
}
}
Adding in line numbers, if I understand your question correctly, you're saying that when debugging, you never reach line 8. If that is the case then I would expect your issue is that when you're actually processing through the loop there are no rows in your gridview. To figure out why that is the case, I would recommend looking at where you are in the post cycle for your page
string filename = #"C:\Users\gurdip.sira\Documents\Visual Studio 2008\WebSites\Supressions\APP_DATA\surpressionstest.csv";//1
StreamWriter sWriter = new StreamWriter(filename);//2
string Str = string.Empty;//3
string headertext = ""; //4
sWriter.WriteLine(headertext); //5
for (int i = 0; i <= (this.GridView3.Rows.Count - 1); i++) //6
{ //7
for (int j = 0; j <= (this.GridView3.Columns.Count - 1); j++) //8
{ //9
Str = this.GridView3.Rows[i].Cells[j].Text.ToString();//10
sWriter.Write(Str);//11
}//12
sWriter.WriteLine();//13
}//14
sWriter.Close();//15
}//16