A loop in write statement
-
Hi, I need print a data from an big array in severals columns. I can not find in C# a way to to "write " an array similar to Fortran statement like: WRITE (*;100) (a(i) , i =1,20) 100 FORMAT (1X, 'a= '; 20F10.2) Thanks Ted
I havent worked in Fortran. Can u please tell what these statements are exactly doing. Maybe then i can help u
-
I havent worked in Fortran. Can u please tell what these statements are exactly doing. Maybe then i can help u
I need write some values from an array in columns like below for (int np=1; np < NP1 ;np++) { writer1.WriteLine ("{0,8},{1,8},{2,8},{3,8},{4,8},{5,8},{6,8},{7,8}, PARAM[1],PARAM[2],PARAM[3],PARAM[4],etc... } The problem is that the number of columns is calculated by the code /it can varied/ . Second only a part of array should will be printed. Regards Ted
-
I need write some values from an array in columns like below for (int np=1; np < NP1 ;np++) { writer1.WriteLine ("{0,8},{1,8},{2,8},{3,8},{4,8},{5,8},{6,8},{7,8}, PARAM[1],PARAM[2],PARAM[3],PARAM[4],etc... } The problem is that the number of columns is calculated by the code /it can varied/ . Second only a part of array should will be printed. Regards Ted
Can't you just nest another loop? Like:
int cols=3; //for example; int testCount=PARAMS.length; for (int rowIndex=0;rowIndex < Math.Ceiling (Convert.ToDouble (testCount) / Convert.ToDouble (cols));rowIndex++) { for (int colIndex=0;colIndex < cols;colIndex++) { if (rowIndex * cols +colIndex < testCount) { writer1.Write(PARAMS[rowIndex * cols +colIndex].ToString () + " "); } } writer1.WriteLine(); }
That should write it one row at a time: 0 1 2 3 4 5 6 etc. Hope this helps Bill -
Can't you just nest another loop? Like:
int cols=3; //for example; int testCount=PARAMS.length; for (int rowIndex=0;rowIndex < Math.Ceiling (Convert.ToDouble (testCount) / Convert.ToDouble (cols));rowIndex++) { for (int colIndex=0;colIndex < cols;colIndex++) { if (rowIndex * cols +colIndex < testCount) { writer1.Write(PARAMS[rowIndex * cols +colIndex].ToString () + " "); } } writer1.WriteLine(); }
That should write it one row at a time: 0 1 2 3 4 5 6 etc. Hope this helps BillMy problem is now much more trivial: how I can get my output as a row?, instead for column from example as below: int []PARAM = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}; int zmax =11; for (int z = 0; z < zmax+1 ; z++) writer1.WriteLine(PARAM[z].ToString()+ " "); === output 1 2 3 4 5 6 7 8 9 10 11 12 //Ted teja
-
My problem is now much more trivial: how I can get my output as a row?, instead for column from example as below: int []PARAM = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}; int zmax =11; for (int z = 0; z < zmax+1 ; z++) writer1.WriteLine(PARAM[z].ToString()+ " "); === output 1 2 3 4 5 6 7 8 9 10 11 12 //Ted teja
Oh! That is easier. ;) I do not know what
writer1
is, since none of your previous posts contain a declaration...but in many cases there is a .Write() method as well as a .WriteLine. Is so you can do:for (int i=0;i < PARAMS.Length; i++) writer1.Write(PARAMS[i].ToString() + " "); writer1.WriteLine() //to get the line-feed
If you have no Write, just build the string up before you print it.System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i=0;i < PARAMS.Length; i++){ SB.Append(PARAMS[i].ToString() ); SB.Append(" "); } writer1.WriteLine(SB.ToString())