0

I am trying to write a program that reads from the console a positive integer N (N < 20) and prints a matrix like these ones:

N = 3
1 2 3
2 3 4
3 4 5

N = 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9

This is my code:

using System;
namespace _6._12.Matrix
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Please enter N ( N < 20): ");
            int N = int.Parse(Console.ReadLine());
            int row;
            int col;
            for (row = 1; row <= N; row++)
            {
                for (col = row; col <= row + N - 1; )
                {
                    Console.Write(col + " ");
                    col++;
                }
                Console.WriteLine(row);
            }
            Console.WriteLine();
        }
    }
}

The problem is that the console prints one extra column with the number from 1 to N and I dont know how to get rid of it. I have an idea why this might be happening but still can't find a solution.

1
  • First check whether N<20 or else again ask user to put a number < 20 Commented Aug 6, 2012 at 6:14

6 Answers 6

4

simple, change Console.WriteLine(row); for Console.WriteLine();

while your at it;

    static void Main()
    {
        int N;

        do
        {
            Console.Write("Please enter N (N >= 20 || N <= 0): ");
        }
        while (!int.TryParse(Console.ReadLine(), out N) || N >= 20 || N <= 0);

        for (int row = 1; row <= N; row++)
        {
            for (int col = row; col <= row + N - 1; )
            {

                Console.Write(col + " ");
                col++;
            }
            Console.WriteLine();
        } 

        Console.Read();
    }

Please enter N (N >= 20 || N <= 0): 5
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8 
5 6 7 8 9 
Sign up to request clarification or add additional context in comments.

2 Comments

Okey. The extra column disapear, but now a number: N + 1 appear bellow the first column which is not needed.
@AngelElenkov, can you tell me where?
1

Just change this line Console.WriteLine(row); to this Console.WriteLine(); The problem here is that; at the end of each inner loop, you are writing the row value again; which is not needed.

Comments

0

The first question is what do you think Console.WriteLine(row) is doing? It is critical, when you are learning to program, to 'see' what the code is doing and why it is doing it, rather than running it, tweaking it, and then running it again to see if it acts the way you want it to. Once you see, in your head, clearly and concisely what the code is doing, you will notice that the Console.WriteLine(row) is not right and that you just need to write a newline at that point.

Comments

0

Here is another approach using an if statement instead of using a do while, the code looks a little simpler:

 static void Main(string[] args)
 {
     Console.Write("Give a number from 1 to 20: ");
     int n = int.Parse(Console.ReadLine());
     int row,col;
     Console.WriteLine("");
     if (n > 0 && n < 21)
     {
         for (row = 1; row <= n; row++)
         {
             for (col = row; col <= row + n - 1;col++ )
             {
                 Console.Write(col + " ");
             }

             Console.WriteLine();
         }
     }
     else
     {
         Console.WriteLine("This number is greater than 20 or smaller than 1");
     }
 }

Comments

0

// All the above answer i tired are wrong u should once try this and then reply me...

        Console.Write("Enter N: (N < 20) ");
        int n = Int32.Parse(Console.ReadLine());

        for (int row = 1; row <= n;row++)
        {
            Console.Write(row+" ");
            for (int col = row+1; col <= row + n - 1; )
            {
                Console.Write(col + " ");
                col++;
            }
            Console.WriteLine();

        }
        Console.ReadLine();

Comments

-1
using System;
namespace _6._12.Matrix
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Please enter N ( N < 20): ");
            int N = int.Parse(Console.ReadLine());
            int row;
            int col;
            for (row = 1; row <= N; row++)
            {
                for (col = row; col <= row + N - 1; )
                {
                    Console.Write(col + " ");
                    col++;
                }
                Console.WriteLine(row);
            }
            Console.WriteLine();
        }
    }

Comments

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.