5 Replies - 584 Views - Last Post: 21 July 2013 - 07:43 AM Rate Topic: -----

#1 HeeJo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-July 13

creating a string of arrays and printing them vertically

Posted 21 July 2013 - 05:46 AM

Hi,

I've been doing some challenges in my c# textbook. And one of the problem was to store these names into an array,
string[] names = {"Alan", "Bill", Joe, "Mark"}; and print them out like this:

A B J M
L I O A
A L E R
N L K

string[] names = { "ALAN", "BILL", "JOE", "MARK" };
            string msg = "";
            int MaxLength = 0;

            for (int i = 0; i < names.Length; i++)
            {

                if (names[i].Length > MaxLength) MaxLength = names[i].Length;
            }

            for (int i = 0; i < names.Length; i++)
            {
                while (names[i].Length < MaxLength) 
                {
                    names[i] +=  " ";
                }
            }

            for (int i = 0; i < MaxLength; i++)
            {
                for (int ii = 0; ii < names.Length; ii++)
                {
                    char add = (names[ii])[i];
                    msg += add + ", ";
                }
                msg += "\n";
            }
            lblNames.Text = msg;
        }


I got this solution from the textbook and it works, but I don't really understand the second for-loop and the last for loop within the for-loop that goes, char add = (names[ii])[i];
If you guys can help me out that'd be great.
Or if you know a better easy way of doing it and explaining it to me how it works, that'd be really awesome too.

Thanks for the help!
p.s It's my first time posting in this forum

Is This A Good Question/Topic? 0
  • +

Replies To: creating a string of arrays and printing them vertically

#2 HeeJo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-July 13

Re: creating a string of arrays and printing them vertically

Posted 21 July 2013 - 06:29 AM

Hi,
I've been doing a challenge from C# textbook and one of the challenges was to store a list of names into an array,
string[] names = {"Alan", "Bill", "Joe", "Mark"}; and then print them vertically like this:
A B J M
L I O A
A L E R
N L K

Here is the code I got from the solution:
string[] names = { "ALAN", "BILL", "JOE", "MARK" };
            string msg = "";
            int MaxLength = 0;

            for (int i = 0; i < names.Length; i++)
            {

                if (names[i].Length > MaxLength) MaxLength = names[i].Length;
            }

            for (int i = 0; i < names.Length; i++)
            {
                while (names[i].Length < MaxLength) 
                {
                    names[i] +=  " ";
                }
            }

            for (int i = 0; i < MaxLength; i++)
            {
                for (int ii = 0; ii < names.Length; ii++)
                {
                    char add = (names[ii])[i];
                    msg += add + ", ";
                }
                msg += "\n";
            }
            lblNames.Text = msg;
        }



I don't really understand this line of code, and need someone to explain to me what it is doing.
for (int i = 0; i < names.Length; i++)
            {
                while (names[i].Length < MaxLength) 
                {
                    names[i] +=  " ";
                }
            }



And also this code
for (int ii = 0; ii < names.Length; ii++)
                {
                    char add = (names[ii])[i];
                    msg += add + ", ";
                }


I just started self teaching myself how to program, and now I understand the frustrations most programmers go through. Just wondering if there's an easier logic to solve this program, than the code I posted?

Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: creating a string of arrays and printing them vertically

Posted 21 July 2013 - 06:42 AM

Moved to C#. Please note that the challenges forum is for fun challenges, not help questions. Welcome to DIC!
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: creating a string of arrays and printing them vertically

Posted 21 July 2013 - 06:45 AM

Before we try to explain the code, have you tried simulating the code on paper or stepping through the code with the debugger? You will learn more if you make the observations yourself rather than us telling you what is happening.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: creating a string of arrays and printing them vertically

Posted 21 July 2013 - 06:47 AM

Don't open duplicate topics. Merging...
Was This Post Helpful? 0
  • +
  • -

#6 HeeJo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-July 13

Re: creating a string of arrays and printing them vertically

Posted 21 July 2013 - 07:43 AM

Sorry about duplicate posts, I couldn't find my first post and thought my first post didn't go through. And thanks I'll take your advice and try to do them on paper first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1