0

I'm writing a C# console app. The "app" is a poor mans matrix (1 and 0 fall down the page).

I can't figure out how to get 1 row first, then two rows (the 1st row should now be in 2cnd place) etc.

The closest I get is just the next row in the 2d array...

This prints just the current row:

    public static int col = 0;

    static void PrintSingleLine()
    {
        for (int i = col; i <= pnms.GetLength(0) - 1;)
        {
            for (int j = 0; j <= pnms.GetLength(1) - 1; j++)
            {
                Console.Write(pnms[col, j]);
            }
            break;
        }
        col =+ 1;
    }

I've modified it, and tried to get it to print out all the rows that have been printed so far +1, but I cant get it to work...

    public static int coll = 0;

    static void PrintRelevantLines()
    {
        int cnt = 0;

        for (int i = coll; i <= pnms.GetLength(0) - 1;)
        {
            for (int j = 0; j <= pnms.GetLength(1) - 1; j++)
            {
                for (int k = cnt; k <= coll; k++)
                {
                    Console.Write(pnms[k, j]);
                }
            }
            break;
        }
        coll = +1;
    }

Any help would be much appreciated.

Thank you

Edit 1:

As requested I will show you the wished for result.

Let's say my array has 3 rows. The values are like this:

 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1
 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1
 1 0 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1

I would like the 1st second/or user input to print

(x) 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1

The 2nd user input to print:

(y) 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1
(x) 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1

The 3rd user input to print:

(z) 1 0 1 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1
(y) 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1
(x) 1 0 1 0 0 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 1

Thanks for pointing out what wasn't clear. As I posed the question I though that was clear :)

Let me know if I can provide any additional info

5
  • Maybe a newline is missing and the break; seems odd, just remove it Commented Aug 13, 2020 at 15:27
  • no, if I remove the break; it just keeps looping indefenitly... and if there are more characters then the buffer size it should write in the next line anyway... Commented Aug 13, 2020 at 15:34
  • Do you want to print the rows in descending order? Commented Aug 13, 2020 at 15:40
  • This is not clear, show us the input and the expected output Commented Aug 13, 2020 at 15:42
  • Edited the question Commented Aug 13, 2020 at 16:49

2 Answers 2

1

You've made things hard for yourself by using j, k, i, etc. - you can make the whole thing easier. I'm assuming you're working off a 2 dimensional array of string. Here's a simpler version (presuming I've understood your requirements correctly...)

static void PrintRelevantLines()
        {
            string [,]  pnms = new string[3,5];
            // Setup data
            pnms[0, 0] = "1";
            pnms[0, 1] = "0";
            pnms[0, 2] = "1";
            pnms[0, 3] = "0";
            pnms[0, 4] = "1";

            pnms[1, 0] = "0";
            pnms[1, 1] = "1";
            pnms[1, 2] = "0";
            pnms[1, 3] = "1";
            pnms[1, 4] = "0";

            pnms[2, 0] = "1";
            pnms[2, 1] = "1";
            pnms[2, 2] = "1";
            pnms[2, 3] = "0";
            pnms[2, 4] = "0";

            for (int columnIndex = 0; columnIndex <= pnms.GetLength(0) - 1; columnIndex ++)
            {
                for (int rowIndex = 0; rowIndex <= pnms.GetLength(1) - 1; rowIndex++)
                {
                        Console.Write(pnms[columnIndex, rowIndex]);
                }

                Console.WriteLine(); // (if you wanted a matrix style trickle of lines, you'd probably want some sort of pause here....)                  
            }

                // OUTPUT
                // 10101
                // 01010
                // 11100
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the anwser. I've already figured out how to print out all the lines. But I don't know how to print out 1st line now, lines 1 and 2 next second, lines 1, 2 and 3 the second after that etc.(or on user input or whatever)
0

Well, I figured it out. If it helps anyone...

        public static int coll = 0;
        static void PrintRelevantLines()
        {
            Console.Clear();
            if (coll <= pnms.GetLength(0) - 1)
            {
                for (int k = 0; k <= coll; k++)
                {
                    for (int j = 0; j <= pnms.GetLength(1) - 1; j++)
                    {
                        Console.Write(pnms[k, j]);
                    }
                    Console.WriteLine();
                }
            }
            

4 Comments

Just FYI, your variable naming probably led to some confusion. Your "k" variable is normally referred to as the ROW, while the "j" variable would be the COLUMN. Think about it...you are printing all the values in the "j" loop in ONE line after each other...so they are all different columns in a single row. So the use of the name "coll" is a bit confusing.
You code structure is very counter intuitive, the outer for loop isnt a real one, since the incrementor i never changes and it breaks after the first run. You should replace it by if(coll<= pnms.GetLength(0) - 1)
@Idle_Mind thats true. I've modified this code a bunch of times to get it to do what I want..., one thing led to another and this came out =)
@FrankM thanks. I knew it didn't do anything in the end, but I needed to keep the "reference" to the cols(0) of the 2d array. I will change it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.