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. Display an array elements in 2 columns using the console

Display an array elements in 2 columns using the console

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
3 Posts 2 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.
  • P Offline
    P Offline
    PozzaVecia
    wrote on last edited by
    #1

    Suppose I have an array of n elements ( for example double or string) I want to display them in the console in 2 columns, having the same number of elements and keeping the order. if my

    string[] my = new string[]{"a","b","c","d","e"}

    I need to display a d b e c So first column a b c second column d e First and second column should be spaced It is like a menu Any smart idea?

    Richard DeemingR 1 Reply Last reply
    0
    • P PozzaVecia

      Suppose I have an array of n elements ( for example double or string) I want to display them in the console in 2 columns, having the same number of elements and keeping the order. if my

      string[] my = new string[]{"a","b","c","d","e"}

      I need to display a d b e c So first column a b c second column d e First and second column should be spaced It is like a menu Any smart idea?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Something like this:

      string[] my = new string[]{ "a", "b", "c", "d", "e" };

      // Find the start index of the second column:
      int midPoint = (my.Length / 2) + (my.Length & 1);

      for (int i = 0, j = midPoint; i < midPoint; i++, j++)
      {
      Console.WriteLine("{0}\t{1}", my[i], (j >= my.Length ? null : my[j]));
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Something like this:

        string[] my = new string[]{ "a", "b", "c", "d", "e" };

        // Find the start index of the second column:
        int midPoint = (my.Length / 2) + (my.Length & 1);

        for (int i = 0, j = midPoint; i < midPoint; i++, j++)
        {
        Console.WriteLine("{0}\t{1}", my[i], (j >= my.Length ? null : my[j]));
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        P Offline
        P Offline
        PozzaVecia
        wrote on last edited by
        #3

        Thks a lot

        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