# Secondary diagonal of a matrix

Page 1 of 1

## 7 Replies - 1016 Views - Last Post: 02 May 2013 - 07:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319786&amp;s=d99ad7235a72ebbcf99ecea1701a7ef2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 giannis13

Reputation: 0
• Posts: 5
• Joined: 28-April 13

# Secondary diagonal of a matrix

Posted 28 April 2013 - 02:52 PM

Hello.. I can't make my code to work . I want to make a matrix 10x10 where the second diagonal will have as value 1 and all other matrix values will be 0.
like this
```0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
```

That's my code

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace deuteri_kathetos
{
class Program
{
static void Main(string[] args)
{
int[,] deuteri_kathetos = new int[10, 10];

int N = 10;

for (int i = 0; i < N; i++)
{

for (int j = 0; j < N; j++)
{

if (j == N - (i + 1))
{

Console.Write(deuteri_kathetos[i, j] = 1);

}

else
{

Console.Write(deuteri_kathetos[i, j] = 0);
}

}
}

}
}
}
```

This post has been edited by Curtis Rutland: 03 May 2013 - 09:18 AM
Reason for edit:: added code tags

Is This A Good Question/Topic? 0

## Replies To: Secondary diagonal of a matrix

### #2 Michael26

Reputation: 378
• Posts: 1,570
• Joined: 08-April 09

## Re: Secondary diagonal of a matrix

Posted 28 April 2013 - 03:05 PM

Some example
```for (int row = 0; row < array.length; row++)
{

array[row][row] = 1;
}
```

if your matrix is diagonal it goes
[0][0]
[1][1]
[2][2] etc ...

This post has been edited by Michael26: 28 April 2013 - 03:08 PM

### #3 giannis13

Reputation: 0
• Posts: 5
• Joined: 28-April 13

## Re: Secondary diagonal of a matrix

Posted 29 April 2013 - 01:47 AM

Michael26, on 28 April 2013 - 03:05 PM, said:

Some example
```for (int row = 0; row < array.length; row++)
{

array[row][row] = 1;
}
```

if your matrix is diagonal it goes
[0][0]
[1][1]
[2][2] etc ...

Thank you michael for your reply. I solved it by your way days before but my professor told me "if you want to be a good developer you must make dynamic code and not static" .. i want to make it works in dynamic way.

### #4 Skydiver

• Code herder

Reputation: 4090
• Posts: 13,052
• Joined: 05-May 12

## Re: Secondary diagonal of a matrix

Posted 29 April 2013 - 05:35 AM

Your teacher maybe referring to the hardcoded size of your array as declared by deuteri_kathetos = new int[10, 10]; and the use of a constant N = 10;. 's code can dynamically handle any size, unlike your code above.

### #5 giannis13

Reputation: 0
• Posts: 5
• Joined: 28-April 13

## Re: Secondary diagonal of a matrix

Posted 01 May 2013 - 10:51 AM

Skydiver, on 29 April 2013 - 05:35 AM, said:

Your teacher maybe referring to the hardcoded size of your array as declared by deuteri_kathetos = new int[10, 10]; and the use of a constant N = 10;. 's code can dynamically handle any size, unlike your code above.

For example,if i understood well, you mean that i can use in place of 10 an other value that user will give its size.. right?

### #6 Michael26

Reputation: 378
• Posts: 1,570
• Joined: 08-April 09

## Re: Secondary diagonal of a matrix

Posted 01 May 2013 - 10:58 AM

you can use Console.Readline() to read from console input and store that into N, you can then use deuteri_kathetos = new int[N, N]; to create array size based on the value that is in N

• MrCupOfT

Reputation: 2290
• Posts: 9,529
• Joined: 29-May 08

## Re: Secondary diagonal of a matrix

Posted 02 May 2013 - 06:13 AM

Console.ReadLine() returns a String so remember to check to if the value is valid for it to be a number.

### #8 Michael26

Reputation: 378
• Posts: 1,570
• Joined: 08-April 09

## Re: Secondary diagonal of a matrix

Posted 02 May 2013 - 07:00 AM

i assumed that the OP already knows that.