Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there! I'm really close ,but I just can't seem to wrap my head around the last step.
So what I want is to have 2d matrix and print only the rows that have ALL positive ints.

Right now its my code is just taking out the ints that are either 0 or negative,but I need to remove the entire line if said line contains a 0 or negative.

This is what I'm getting right now
Input:

1 2 6 4
1 5 0 9

Output:

1 2 6 4
1 5

So basically it should not have printed the 2nd row b/c it had a non positive integer

What am I doing wrong?

I appreciate you taking the time to give this a look over!

C#
static void Main(string[] args)
        {
            List<string> L = readAllLines();
            int[,] m = convertListToIntMatrix(L);

            int Rows = m.GetLength(0);
            int Cols = m.GetLength(1);

            for (int r = 0; r < Rows; r++)
            {
                for (int c = 0; c < Cols; c++)
                {

                    if (m[r, c] <= 0)
                    {

                        break;

                    }
                    else Console.Write("{0} ", m[r, c]);
                }
                Console.WriteLine();

            }
Posted
Comments
[no name] 31-Mar-14 18:53pm    
You would need to check all of the elements, m[r, c] <= 0, before printing anything to the console.

1 solution

Your program will work as you expected it to if the 0 or negative is happened to be the first element in that row. Otherwise, it will continue to print until it encounters a 0 or negative in that row. Got the idea? Try to fix it yourself first.
The idea is to use a boolean flag to determine if any 0 or negative found while looping a row, if none found than use another loop to print that row.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900