Fırat Sakar
Fırat Sakar

Reputation: 13

Print matrix with one for loop

I'm newbie to c#.

I have 2 variables which I get from user named "line" and "column". My goal is write "*" to screen about much lines and columns that I get from user. For example, user entered 5 column and 5 lines:

00000
00000
00000
00000
00000

But when I run my program, It looks like:

00000
00000
00000
00000
0000

(yeah, the last one is missed).

My code is :

int i;
int satir, sutun;
Console.WriteLine("Kaç satır olsun?");
satir = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Kaç sütun olsun peki?");
sutun = Convert.ToInt32(Console.ReadLine());

for (i=1; i < sutun * satir; i++)
{
    if (i%sutun==0)
    {
        Console.Write("\n");
    }
    else
    {
        Console.Write("*");
    }
}
Console.ReadLine();

How can I fix this? Thanks in advice.

Upvotes: 0

Views: 938

Answers (3)

Shar1er80
Shar1er80

Reputation: 9041

Not only should your for loop look like (Changed < to <=):

for (i=1; i <= sutun * satir; i++)

You should print the asterisk no matter what. Take the Console.Write("*") out of the if/else block and place it at the top of the loop, leaving only and if statement to print the newline character

using System;

public class Program
{
    public static void Main()
    {
        int i;
        int satir, sutun;
        Console.WriteLine("Kaç satır olsun?");
        satir = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Kaç sütun olsun peki?");
        sutun = Convert.ToInt32(Console.ReadLine());
        for (i = 1; i <= sutun * satir; i++)
        {
            Console.Write("*");
            if (i % sutun == 0)
            {
                Console.Write("\n");
            }
        }
    }
}

Results:

Kaç satır olsun?
3
Kaç sütun olsun peki?
5
*****
*****
*****

Fiddle Demo

Upvotes: 1

Guffa
Guffa

Reputation: 700562

Actually the output looks like this:

Kaç satır olsun?
5
Kaç sütun olsun peki?
5
****
****
****
****
****

I.e. instead of five columns you get four columns. For the first four lines it's because you are writing out a line break instead of the fifth asterisk, and for the last line it's because you are writing out one too few asterisks.

You should write out one more asterisk, which you can do by using the <= operator instead of < in the loop, and you shouldn't write out a line break instead of the asterisk, but after:

int i;
int satir, sutun;
Console.WriteLine("Kaç satır olsun?");
satir = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Kaç sütun olsun peki?");
sutun = Convert.ToInt32(Console.ReadLine());

for (i = 1; i <= sutun * satir; i++) {
  Console.Write("*");
  if (i % sutun == 0) {
    Console.Write("\n");
  }
}
Console.ReadLine();

Upvotes: 1

CharithJ
CharithJ

Reputation: 47560

i should be less than or equals to sutun.

for (i=1; i <= sutun * satir; i++)

Bonus Note: How to debug my C# code

Set a breakpoint in side your loop and press F10 to go line by line. Then you can find out what's going on.

Upvotes: 2

Related Questions