Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A loop in write statement

A loop in write statement

Scheduled Pinned Locked Moved C#
csharpdata-structures
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    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 1 Reply Last reply
    0
    • A Anonymous

      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 Offline
      I Offline
      Identity Undisclosed
      wrote on last edited by
      #2

      I havent worked in Fortran. Can u please tell what these statements are exactly doing. Maybe then i can help u

      A 1 Reply Last reply
      0
      • I Identity Undisclosed

        I havent worked in Fortran. Can u please tell what these statements are exactly doing. Maybe then i can help u

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        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

        B 1 Reply Last reply
        0
        • A Anonymous

          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

          B Offline
          B Offline
          Bill Dean
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • B Bill Dean

            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

            J Offline
            J Offline
            jablo
            wrote on last edited by
            #5

            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

            B 1 Reply Last reply
            0
            • J jablo

              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

              B Offline
              B Offline
              Bill Dean
              wrote on last edited by
              #6

              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())

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups