7 Replies - 748 Views - Last Post: 02 May 2013 - 07:00 AM Rate Topic: -----

#1 giannis13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
using System.Threading.Tasks;

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);
                    }

                  


                }
            }

                Console.ReadKey();
            }
        }
    }

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  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,534
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 giannis13  Icon User is offline

  • New D.I.C Head

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

Re: Secondary diagonal of a matrix

Posted 29 April 2013 - 01:47 AM

View PostMichael26, 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.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3621
  • View blog
  • Posts: 11,287
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 giannis13  Icon User is offline

  • New D.I.C Head

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

Re: Secondary diagonal of a matrix

Posted 01 May 2013 - 10:51 AM

View PostSkydiver, 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?
Was This Post Helpful? 0
  • +
  • -

#6 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,534
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 362
  • View blog
  • Posts: 1,534
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1